how to update a database field with a random value selected from a list using random.choice()

40 views
Skip to first unread message

mostwanted

unread,
Jul 15, 2020, 8:58:28 AM7/15/20
to web2py-users
I thought it would be easy but its not, wrong values are being selected from wrong lists! After submitting a form I wanna check to see if the session field is either a single_session, double_session, evening_single_session or evening_double_session, depending on what it is I want time to be selected randomly from an appropriate list and inputted into the session_time field but wrong values from wrong lists are entered, information gets mixed up!

model code:
db.define_table('lecture',
               
Field('subject_name', 'reference subject'),
               
Field('department', 'reference departments'), #HERE
               
Field('theLevels', label=SPAN('Levels'), requires=IS_IN_SET(['1.1', '1.2', '2.1', '2.2', '3.1', '3.2', '4.1', '4.2'], zero='---Select A Level---')), #HERE
               
Field('lecturer', 'reference lecturer'),
               
Field('class_session', requires=IS_IN_SET(['single session', 'double session', 'evening single session', 'evening double session'], zero='----Select A Session Period----')),
               
Field('session_time', readable=False, writable=False),
               
Field('class_room', readable=False, writable=False),
               
Field('controller', readable=False, writable=False)
               
)

single_session_times
=['0830-0930hrs', '0930-1030hrs', '1100-1200','1200-1300', '1400-1500', '1500-1600', '1600-1700']
double_session_times
=['0830-1030', '1100-1300', '1400-1700']

evening_single_session_times
=['1730-1830', '1830-1930']
evening_double_session_times
=['1730-1930']
'''
After submitting the form, have system scheck the session, if the session is any of whats defined give it a proper time
'''

if db.lecture.class_session=='single session':
    the_time
=random.choice(single_session_times)
    db
.lecture.session_time.default=the_time

   
elif db.lecture.class_session=='evening single session':
    the_time
=random.choice(evening_single_session_times)
    db
.lecture.session_time.default=the_time

   
elif db.lecture.class_session=='double session':
    the_time
=random.choice(double_session_times)
    db
.lecture.session_time.default=the_time

   
elif db.lecture.class_session=='evening double session':
    the_time
=random.choice(evening_double_session_times)
    db
.lecture.session_time.default=the_time



How can i achieve this with proper results from the intended list

Mostwanted

villas

unread,
Jul 15, 2020, 11:47:18 AM7/15/20
to web2py-users
Before submission of form, use JS.
or
After submission,  allocate the time in the controller using onvalidation.  See  http://www.web2py.com/books/default/chapter/29/07/forms-and-validators#onvalidation

mostwanted

unread,
Jul 15, 2020, 2:26:33 PM7/15/20
to web2py-users
Thank you Villas, i updated my controller like this & its giving results. I a sure there is a better way to do this, a ore cleaner way & if anyone cares to improve my code please dont hesitate so i could update mine & learn more:

def my_form_processing(form):
   
if form.vars.class_session=='single session':

        single_session_times
=['0830-0930hrs', '0930-1030hrs', '1100-1200','1200-1300', '1400-1500', '1500-1600', '1600-1700']

        the_time
=random.choice(single_session_times)
        form
.vars.the_time2=the_time

   
if form.vars.class_session=='evening single session':
        evening_single_session_times
=['1730-1830', '1830-1930']
        the_time
=random.choice(evening_single_session_times)
        form
.vars.the_time2=the_time

   
if form.vars.class_session=='double session':

        double_session_times
=['0830-1030', '1100-1300', '1400-1700']

        the_time2
=random.choice(double_session_times)
        form
.vars.the_time2=the_time2

   
if form.vars.class_session=='evening double session':
        evening_double_session_times
=['1730-1930']
        the_time
=random.choice(evening_double_session_times)
        form
.vars.the_time2=the_time

@auth.requires_login()
def index():
    form
=SQLFORM(db.lecture)
   
if form.process(onvalidation=my_form_processing).accepted:
        response
.flash=T('Lecture Entered')
   
return locals()
Regards;

Mostwanted

villas

unread,
Jul 15, 2020, 5:20:01 PM7/15/20
to web2py-users
We all have our styles of programming.  I would probably create a dict of times.
But any idea which works is fine. Everyone looks at their old code and sees a better way.
My motto is:  get something working and move on! 

AGRogers

unread,
Jul 15, 2020, 8:22:45 PM7/15/20
to web...@googlegroups.com
>>>My motto is:  get something working and move on! 

Haha. A modified version I need to remember: Get something working and don't go back and break it! 

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/8ae4d738-3954-441e-807c-6957229ebb56o%40googlegroups.com.

mostwanted

unread,
Jul 16, 2020, 2:51:52 AM7/16/20
to web2py-users
Hahahaha sometimes you put in a single delimeter & it all comes down crumbling!
To unsubscribe from this group and stop receiving emails from it, send an email to web...@googlegroups.com.

mostwanted

unread,
Jul 16, 2020, 2:53:51 AM7/16/20
to web2py-users
The dictionary makes it less cubersome:
def my_form_processing(form):
    times
=dict({'single_session_times':['0830-0930hrs', '0930-1030hrs', '1100-1200','1200-1300', '1400-1500', '1500-1600', '1600-1700'], 'evening_single_session_times':['1730-1830', '1830-1930'],'double_session_times':['0830-1030', '1100-1300', '1400-1700'],'evening_double_session_times':['1730-1930']})


   
if form.vars.class_session=='single session':
        the_time
=random.choice(times['single_session_times'])

        form
.vars.the_time2=the_time

   
if form.vars.class_session=='evening single session':

        the_time
=random.choice(times['evening_single_session_times'])

        form
.vars.the_time2=the_time

   
if form.vars.class_session=='double session':

        the_time2
=random.choice(times['double_session_times'])

        form
.vars.the_time2=the_time2

   
if form.vars.class_session=='evening double session':

        the_time
=random.choice(times['evening_double_session_times'])

        form
.vars.the_time2=the_time

@auth.requires_login()
def index():
    form
=SQLFORM(db.lecture)
   
if form.process(onvalidation=my_form_processing).accepted:
        response
.flash=T('Lecture Entered')
   
return locals()

Thank you guys, much appreiated.

villas

unread,
Jul 16, 2020, 5:56:01 AM7/16/20
to web2py-users
Hi Mostwanted
We always can optimise our code.
The dict could have keys for each possible session. Then you only need one line to produce a random time.
Maybe your onvalidation only requires two lines of code!
Please look at this...

def my_form_processing(form):
    times
= {
'single session':['0830-0930hrs', '0930-1030hrs', '1100-1200','1200-1300', '1400-1500', '1500-1600', '1600-1700'],
'evening single':['1730-1830', '1830-1930'],
'double session':['0830-1030', '1100-1300', '1400-1700'],
'evening double':['1730-1930']
}
   
form.vars.the_time2 = random.choice(times[form.vars.class_session])

mostwanted

unread,
Jul 16, 2020, 9:36:18 AM7/16/20
to web2py-users
This is very impressive Villas, you see, this is what I meant by a better way of doing this, thank you.
Reply all
Reply to author
Forward
0 new messages