Ejemplo n.º 1
0
    def save(self):
        course_number = self.cleaned_data.get('course_number', None)
        course_title = self.cleaned_data.get('course_title', None)
        meeting_days = self.cleaned_data.get('meeting_days', None)
        hour = self.cleaned_data.get('hour', None)
        min = self.cleaned_data.get('min', None)
        discipline_areas = self.cleaned_data.get('discipline_areas', None)

        try:
            new_class = base_models.Class(
                course_number=course_number,
                course_title=course_title,
                meeting_days=meeting_days,
            )
            new_class.save()

            for i in discipline_areas:
                discipline_area = base_models.DisciplinesAreas.objects.get(
                    disciplines_area=i)
                new_class.disciplines_area.add(discipline_area)
            # calculates the end time of the class to be 75 minutes after the start time
            new_class.start_time = dt.datetime.strptime(
                hour + ':' + min + ':00', '%H:%M:%S').time()
            new_class.end_time = (
                dt.datetime.strptime(hour + ':' + min + ':00', '%H:%M:%S') +
                dt.timedelta(minutes=75)).time()
            new_class.save()

            return new_class
        except Exception as e:
            # We would never expect an error here because this function should only be called after checking to make
            # sure that the form is valid.
            raise_unexpected_error(e)
Ejemplo n.º 2
0
    def save(self):

        username = self.cleaned_data.get('email', None)
        password = self.cleaned_data.get('password', None)
        question1 = self.cleaned_data.get('question1', None)
        question2 = self.cleaned_data.get('question2', None)
        user_type = self.cleaned_data.get('user_type', None)

        # this function gets the inputted values and creates a new user in our BaseUser model
        # important to note that isApproved is set to 'no' as it will require root user to be switched to 'yes'
        try:
            new_user = base_models.BaseUser(
                username=username,
                sec_question1=question1,
                sec_question2=question2,
                user_type=user_type,
                isApproved="no",
            )
            new_user.set_password(
                password
            )  # set the chosen password as the password for that requested user
            new_user.save()  # save the new user to the database

            return new_user
        except Exception as e:
            # We would never expect an error here because this function should only be called after checking to make
            # sure that the form is valid.
            raise_unexpected_error(e)
Ejemplo n.º 3
0
    def save(self):
        instructor_id = self.cleaned_data.get('instructor_id', None)
        last_name = self.cleaned_data.get('last_name', None)
        maximum_class_load = self.cleaned_data.get('maximum_class_load', None)
        discipline_areas = self.cleaned_data.get('discipline_areas', None)

        try:
            new_instructor = base_models.Instructor(
                instructor_id=instructor_id,
                last_name=last_name,
                maximum_class_load=maximum_class_load,
            )
            new_instructor.save()

            for i in discipline_areas:
                discipline_area = base_models.DisciplinesAreas.objects.get(
                    disciplines_area=i)
                new_instructor.disciplines_area.add(discipline_area)
            new_instructor.save()

            return new_instructor
        except Exception as e:
            # We would never expect an error here because this function should only be called after checking to make
            # sure that the form is valid.
            raise_unexpected_error(e)
Ejemplo n.º 4
0
    def check_username(request):
        """
        This function checks that the session variable 'new_username' is set to a user that has an unusable password
        :param request: Django Request Object
        :return: On Success: the WordyUser instance where username=request.session['new_username']
                 On Failure: None
        """
        username = request.session.get('new_username', None)

        if not username:
            # TODO: Implement Django messaging service and then redirect to a site_error page that can consume
            # the messages passed to it. This will be much more useful than the simple not_found page that
            # has already been implemented.
            return None

        try:
            user_object = ADTAA_models.BaseUser.objects.get(
                username=username, )

            # The if/else statement below could be reduced to the following one-liner:
            # return user_object if not user_object.has_unusable_password() else None

            if user_object.has_usable_password():
                return None
            else:
                return user_object
        except ADTAA_models.BaseUser.DoesNotExist:
            # This exception isn't really an error. It just means that the username passed in does not exist in the
            # database. We don't need to stop the server for this issue, though we would probably want to log the
            # event in a production app.
            return None
        except Exception as e:
            raise_unexpected_error(e)
Ejemplo n.º 5
0
    def validate(self, username):
        try:
            validate_email(username)
        except ValidationError:
            raise ValidationError(
                'Username %(username)s is invalid.',
                code='invalid',
                params={'username': username},
            )
        except Exception as e:
            raise_unexpected_error(e)

        # At this point, the username *might* be valid. The check above doesn't ensure that the name is available-
        # it only ensures that the name has the correct form (it doesn't have whitespace and only uses allowed
        # characters).

        # Let's count the number of WordyUser objects that have this username. Since usernames must be unique,
        # this value will either be 0 or 1. That's super nice because we can just return the result of our
        # WordyUser count and Django will treat a 0 as "falsy" and any other number as "truthy."
        username_in_use = base_models.BaseUser.objects.all().filter(
            username=username).count()
        if not username_in_use:
            raise ValidationError(
                'Username %(username)s do not exist. Please choose another.',
                params={'username': username},
                code='invalid',
            )

        # If we get to this point, we know the username is valid and available. Validate functions need to return
        # the validated value on success
        return username
Ejemplo n.º 6
0
 def validate(self, password):
     try:
         for validator in self.default_validators:
             validator().validate(password)
     except ValidationError as e:
         raise e
     except Exception as e:
         raise_unexpected_error(e)
Ejemplo n.º 7
0
    def save(self):

        username = self.cleaned_data.get('email', None)
        question1 = self.cleaned_data.get('question1', None)
        question2 = self.cleaned_data.get('question2', None)
        password = self.cleaned_data.get('new_password', None)

        try:
            user = base_models.BaseUser.objects.all().filter(
                username=username,
                sec_question1=question1,
                sec_question2=question2).count()
            if user:
                # if a match is found in the database then it gets the user's username and sets the entered new password
                # as the password for that user and saves it
                user = base_models.BaseUser.objects.get(username=username)
                user.set_password(password)
                user.save()
            return user
        except Exception as e:
            # We would never expect an error here because this function should only be called after checking to make
            # sure that the form is valid.
            raise_unexpected_error(e)