[Tutor] Validation loop

bob gailer bgailer at gmail.com
Sat Sep 13 14:19:09 CEST 2008


Norman Khine wrote:
> Hello,
> I am writting a little module that validates if a member has completed 
> all the previous exams before they can access the current exam.
>
> The way, I thought it may work is to:
>
> Search all previous exams from the previous topics and check to see if 
> the user has passed them, if not, then they cannot access this exam.
>
> This works to a point, in that, I only make a check in the topic 
> before the current topic if the user has passed the exam.
>
> The problem is if, for example, I have this:
>
> topic-1
> + exam1
> topic-2
> + no-exam
> topic3
> + exam3
>
> In topic-2 I don't have an exam, so my function returns true and the 
> user can access both exam-1 and exam-3
>
> Whereas I don't want them to access exam-3.
>
> Can you please look at my function and point me in the right direction.
>
>     # Exam Access Control
>     def can_take_exam(self, user, object):
>         '''
>         Who is not allowed to take the exam:
>         1) If the user has not passed the exam from all the previous 
> topics.
>         '''
>         if self.is_admin(user, object):
>             return True
>         if not self.is_member(user, object):
>             return False
>
>         # Has the user already passed this exam?
>         passed = object.get_result()[0]
>         if passed:
>             return False
>         topic = object.parent
>         prev_topic = topic.get_prev_topic()
>
>         # First topic, user can take exam
>         if prev_topic is None:
>             return True
>
>         # Previous topic has no exam
>         exam = prev_topic.get_exam()
>         if exam is None:
Seems that here you must recurse to the topic before prev_topic rather 
than returning True
>             return True
>
>         # Has the user passed the previous exam?
>         passed = exam.get_result()[0]
>         return bool(passed)
>
> I was thinking maybe to put in the list all the previous topics and 
> then loop
> through this for each exam and if all are passed then the user can 
> take the
> current exam, if not then no.
>
> So I tried this:
>
>         topics = object.parent.get_topics()
>         for i, topic in enumerate(topics):
>             all_previous_topics = topics[:i]
>             if all_previous_topics is not None:
>                 return False
>                 for x in all_previous_topics:
>                     exam = x.get_exam(user.name)
>                     if exam is not None:
>                         passed = exam.get_result(user.name)[0]
>                         if passed:
>                             return bool(passed)
>
>
> But this, makes all exams non accessable.
>
> Any pointers much appreciated.
>
> Cheers
>
> Norman
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
Bob Gailer
Chapel Hill NC 
919-636-4239

When we take the time to be aware of our feelings and 
needs we have more satisfying interatctions with others.

Nonviolent Communication provides tools for this awareness.

As a coach and trainer I can assist you in learning this process.

What is YOUR biggest relationship challenge?



More information about the Tutor mailing list