This is very impressive Villas, you see, this is what I meant by a better 
way of doing this, thank you.

On Thursday, July 16, 2020 at 11:56:01 AM UTC+2, villas wrote:
>
> 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])
>
>
>
>
>
> On Thursday, 16 July 2020 07:53:51 UTC+1, mostwanted wrote:
>>
>> 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.
>>
>> On Wednesday, July 15, 2020 at 11:20:01 PM UTC+2, villas wrote:
>>>
>>> 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!  
>>>
>>>
>>>
>>> On Wednesday, 15 July 2020 19:26:33 UTC+1, mostwanted wrote:
>>>>
>>>> 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
>>>>
>>>> On Wednesday, July 15, 2020 at 5:47:18 PM UTC+2, villas wrote:
>>>>>
>>>>> 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
>>>>>
>>>>> On Wednesday, 15 July 2020 13:58:28 UTC+1, mostwanted wrote:
>>>>>>
>>>>>> 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
>>>>>>
>>>>>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/edcfc0dc-6de7-47d3-be0f-fe8f184821a7o%40googlegroups.com.

Reply via email to