Esempio n. 1
0
    def save(self):
        # Define what needs to be saved
        # use validated_data as the input from the API

        # Create a User with the variable from validated data
        user = User(
            username    = self.validated_data['username'],
            first_name  = self.validated_data['first_name'],
            last_name   =  self.validated_data['last_name'],
            # It is possible to change the data. Then make sure to also update the validated_data
            # otherwise the old value will be shown in the API reponse
            # last_name   =  'XXX', 
            email       = self.validated_data['email']
        )

        # validate the password using the default password validators
        password = self.validated_data['password']
        # password = data.get('password')

        # When validating the password using the validate_password then 
        # the error must be catched, otherwise this whole serializer will result in an error

        errors = dict() 
        try:
            # validate the password and catch the exception
            validate_password(password=password)

        # the exception raised here is different than serializers.ValidationError
        except exceptions.ValidationError as e:
            errors['password'] = list(e.messages)

        if errors:
            raise serializers.ValidationError(errors)
        else:
            # Add the password to the user and save to the database
            user.set_password(password)
            user.save()

        # Log the registration of a new user
        now = timezone.now()
        dt_string = now.strftime("%Y-%m-%d %H:%M:%S")

        # f = open("/apps/Klaverjassen/log/klaverjas_registration.txt", "a")
        # log_text = '*** ' + dt_string + ', Username: '******'username'] \
        #             + ', Naam: ' + self.validated_data['first_name'] + ' ' + self.validated_data['last_name'] \
        #             + ', Email: ' + self.validated_data['email'] +  "\n"  
        # f.write(log_text)
        # f.close()

        # log the registration 
        logger_text = 'Username: '******'username'] \
                    + ', Naam: ' + self.validated_data['first_name'] + ' ' + self.validated_data['last_name'] \
                    + ', Email: ' + self.validated_data['email']  
        logger('registration').info(f'{logger_text}')

        try:
            # send a mail
            subject='New user registered'
            message_text = '*** ' + dt_string + ', Username: '******'username'] \
                        + ', Naam: ' + self.validated_data['first_name'] + ' ' + self.validated_data['last_name'] \
                        + ', Email: ' + self.validated_data['email'] 
            mailfrom='*****@*****.**'  
            mailto='*****@*****.**'
            send_mail(
                subject,
                message_text,
                mailfrom,
                [mailto],
                fail_silently=False,
            )
        except:
            logger('errors').exception('Mail could not be sent')