Example #1
0
class UserSerializer(ModelSerializer):
    delegate = DelegateNestedSerializer(required=False)
    school = SchoolSerializer(required=False)

    class Meta:
        model = User
        fields = ('id',
                  'username',
                  'first_name',
                  'last_name',
                  'user_type',
                  'school',
                  'committee',
                  'delegate', )
        read_only_fields = ('id',
                            'username',
                            'user_type',
                            'committee', )

    def update(self, instance, validated_data):
        if 'school' in validated_data:
            school_data = validated_data.pop('school')
            School.objects.filter(id=instance.school.id).update(**school_data)
            send_mail('{0} has updated its information'.format(instance.school),
                      'New information for {0}: \n\n'.format(instance.school) \
                      + 'Advisor: \n' \
                      + '\n'.join(['{0}: {1}'.format(field, validated_data[field]) for field in validated_data]) \
                      + '\n\nSchool: \n' \
                      + '\n'.join(['{0}: {1}'.format(field, school_data[field]) for field in school_data]),
                      '*****@*****.**',
                      ['*****@*****.**', '*****@*****.**'], fail_silently=False)

        return super(UserSerializer, self).update(instance, validated_data)
Example #2
0
class UserSerializer(ModelSerializer):
    school = SchoolSerializer(required=False, read_only=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'first_name', 'last_name', 'user_type',
                  'school', 'committee',)
        read_only_fields = ('id', 'username', 'first_name', 'last_name',
                            'user_type', 'committee',)
Example #3
0
class CreateUserSerializer(ModelSerializer):
    school = SchoolSerializer(required=False)  # TODO: CreateSchoolSerializer

    class Meta:
        model = HuxleyUser
        fields = (
            'id',
            'username',
            'password',
            'first_name',
            'last_name',
            'user_type',
            'school',
        )
        read_only_fields = ('user_type', )
        write_only_fields = ('password', )

    def restore_object(self, attrs, instance=None):
        original_attrs = attrs.copy()
        if 'password' in attrs:
            del attrs['password']

        user = super(CreateUserSerializer,
                     self).restore_object(attrs, instance)
        if 'password' in original_attrs:
            user.set_password(original_attrs['password'])
        if 'school' in original_attrs:
            user.user_type = HuxleyUser.TYPE_ADVISOR

        return user

    def validate_username(self, attrs, source):
        username = attrs[source]

        if re.match("^[A-Za-z0-9\_\-]+$", username) is None:
            raise ValidationError('Usernames may contain alphanumerics, '
                                  'underscores, and/or hyphens only.')

        if HuxleyUser.objects.filter(username=username).exists():
            raise ValidationError('This username is already taken.')

        return attrs

    def validate_password(self, attrs, source):
        password = attrs[source]

        match = re.match("^[A-Za-z0-9\_\.!@#\$%\^&\*\(\)~\-=\+`\?]+$",
                         password)
        if match is None:
            raise ValidationError('Password contains invalid characters.')

        return attrs
Example #4
0
class DelegateNestedSerializer(serializers.ModelSerializer):
    assignment = AssignmentNestedSerializer(read_only=True)
    school = SchoolSerializer(read_only=True)

    class Meta:
        model = Delegate
        fields = (
            'id',
            'assignment',
            'name',
            'email',
            'created_at',
            'summary',
            'published_summary',
            'school',
            'voting',
            'session_one',
            'session_two',
            'session_three',
            'session_four',
        )
Example #5
0
class CreateUserSerializer(ModelSerializer):
    school = SchoolSerializer(required=False)

    class Meta:
        model = User
        fields = ('id', 'username', 'password', 'first_name', 'last_name',
                  'user_type', 'school', 'email')
        extra_kwargs = {
            'password': {'write_only': True},
            'user_type': {'read_only': True}
        }

    def create(self, validated_data):
        original_validated_data = validated_data.copy()

        if 'password' in validated_data:
            del validated_data['password']

        if validated_data.get('school'):
            school_data = validated_data.pop('school')

            school = School.objects.create(**school_data)
            school.save()

            user = User.objects.create(
                school=school, last_login=datetime.now(), **validated_data)
        else:
            user = User.objects.create(
                last_login=datetime.now(), **validated_data)

        if 'password' in original_validated_data:
            user.set_password(original_validated_data['password'])
            user.save()
        if 'school' in original_validated_data:
            user.user_type = User.TYPE_ADVISOR
            user.save()

        return user

    def validate_username(self, value):
        # Django's User model already handles character and uniqueness
        # validation, so we only worry about length.
        if len(value) < 5:
            raise ValidationError('Username must be at least 5 characters.')

        return value

    def validate_password(self, value):
        password = value

        match = re.match("^[A-Za-z0-9\_\.!@#\$%\^&\*\(\)~\-=\+`\?]+$",
                         password)
        if match is None:
            raise ValidationError('Password contains invalid characters.')

        if len(password) < 6:
            raise ValidationError('Password must be at least 6 characters.')

        return value

    def validate_first_name(self, value):
        first_name = value
        validators.nonempty(first_name)
        return value

    def validate_last_name(self, value):
        last_name = value
        validators.nonempty(last_name)
        return value