Esempio n. 1
0
                      tables=40,
                      talk_time=15)
        event.put()

        # Add some days to the aforementioned event
        day = Day(date=datetime.datetime(year=2011, month=11, day=11, hour=20, minute=00),
                      talks=12,
                      event=event)
        day.put()
        
        guardians = Guardian.all().fetch(99999999)
        samplesize = int(len(guardians)/3)
        guardians = random.sample(guardians, samplesize)
        for guardian in guardians:
            time = TimePreference()
            time.event = event
            time.guardian = guardian
            time.preference = random.randint(0, 2)
            time.save()
            days = event.days.fetch(999)
            random.shuffle(days)
            for i, day in enumerate(days):
                day_pref = DayPreference()
                day_pref.guardian = guardian
                day_pref.day = day
                day_pref.rank = i
                day_pref.save()
            for child in guardian.children:
                subjects = Combination.all().filter('class_id', child.class_id).fetch(999)
                selection = random.sample(subjects, int(random.triangular(0, 4, 0)))
                for choice in selection:
Esempio n. 2
0
    def post(self, eventId, guardianId):
        '''The visitor is not an authenticated guardian'''
        if not SubscriptionLoginHandler.isAuthenticatedGuardian():
            self.redirect('/inschrijven/')
            return
        
        '''The guardian is not authorized to the given'''
        if not self.isAuthorized(eventId, guardianId):
            SubscriptionLoginHandler.redirectToSubscriptionPage(self)
            return
        
        event = Event.get_by_id(int(eventId))
        days = Day.gql("WHERE event = :1", event).fetch(999)
        guardian = Guardian.get_by_key_name(guardianId)
        students = Student.gql("WHERE guardian = :1", guardian).fetch(999, 0)
        students_subjects = self.getStudentsSubjects(students)
        notifications = []
        templVal = {
            'event': event,
            'days': days,
            'guardian': guardian,
            'students': students_subjects,
            'notifications': notifications
        }
           
        if not (event and days and guardian and students):
            notifications.append('U probeert een onmogelijke bewerking uit te voeren.')
            self.showError(templVal)
            return
        
        subscriptionDetailsList = SubscriptionDetails.gql("WHERE event = :1 AND guardian = :2", event, guardian).fetch(1, 0)
        if not subscriptionDetailsList:
            notifications.append('Pagina niet gevonden.')
            self.showError(templVal)
            return           
        subscriptionDetails = subscriptionDetailsList[0]
        if subscriptionDetails and subscriptionDetails.requested:
            notifications.append('U kunt geen verzoeken meer indienen.')
            self.showError(templVal)
            return
        
        studentKeys = [str(k.replace('subject_', '')) for k in self.request.arguments() if re.match("subject_.+", k)]
        requests = []
        dayPrefs = []
        
        for s in students[:]:
            if str(s.key().name()) not in studentKeys:
                students.remove(s)
                
        if not students:
            notifications.append('U kunt geen verzoek indienen als u geen enkel vak geselecteerd heeft. ')
        
        for student in students[:]:
            subjectCodes = [c for c in self.request.get_all("subject_" + str(student.key().name()))]
            subjects = Subject.get_by_key_name(subjectCodes)
            if len(subjectCodes) > 3:
                notifications.append('U kunt maximaal 3 vakken per leerling bespreken.')
            if len(subjectCodes) != len(subjects):
                notifications.append('U probeert een onmogelijke bewerking uit te voeren.')
                
            for subject in subjects:
                combination = Combination.gql("WHERE class_id = :1 AND subject = :2", student.class_id, subject).fetch(1,0)[0]
                if not combination:
                    notifications.append('U probeert een onmogelijke bewerking uit te voeren.')
                    self.showError(templVal)
                    return
                request = Request()
                request.event = event
                request.guardian = guardian
                request.student = student
                request.combination = combination
                requests.append(request)

        '''Process timepreference'''
        timePref = TimePreference()
        timePref.event = event
        timePref.guardian = guardian
        timePref.preference = 0
        if not (self.request.get('time_pref') and (int(self.request.get('time_pref')) in [0,1,2])):
            notifications.append('U moet een voorkeur voor tijd aangeven.')
        else:            
            timePref.preference = int(self.request.get('time_pref'))
        
        '''Check if dates from the form match the dates from the event '''
        dayKeys = [long(k.replace('date_', '')) for k in self.request.arguments() if re.match("date_.+", k)]
        dayKeysFromStore= [day.key().id() for day in days]
        daysOk = True
        for dayKey in dayKeys:
            if dayKey not in dayKeysFromStore:
                daysOk = False
                notifications.append('U probeert een onmogelijke bewerking uit te voeren.')
                self.showError(templVal)
                return

        '''Check if the daypreference are correct filled in'''    
        dayPrefsList = [int(self.request.get(k)) for k in self.request.arguments() if re.match("date_.+", k)]
        dayPrefsList.sort()
        dayPrefsOk = True
        if dayPrefsList != [1,2,3]:
            dayPrefsOk = False
            notifications.append('U moet een eerste, een tweede en een derde voorkeur aangeven')

        '''Process daypreferences'''
        if daysOk and dayPrefsOk:
            for day in days:
                dayPref = DayPreference()
                dayPref.day = day
                dayPref.guardian = guardian
                dayPref.rank = int(self.request.get("date_" + str(day.key().id())))
                dayPrefs.append(dayPref)
        
        if notifications:
            path = os.path.join(os.path.dirname(__file__), '../templates/subscription/subscription.html')
            self.response.out.write(template.render(path, templVal))
            return
        
        '''Store the requests'''
        for request in requests:
            request.put()
        for dayPref in dayPrefs:
            dayPref.put()
        timePref.put()
        subscriptionDetails.requested = True
        subscriptionDetails.put()
        
        SubscriptionLogoutHandler.logoutGuardian()
        path = os.path.join(os.path.dirname(__file__), '../templates/subscription/subscription-success.html')
        self.response.out.write(template.render(path, templVal))
        return