def to_model(self): if not self.is_valid(): return dict(self.errors) username = self.cleaned_data['username'] cur = User.objects.filter(username=username) if cur: return {'username':['Пользователь с таким именем уже существует!']} n = randint(0,1000000) password = md5.md5(str(n) + self.cleaned_data['password']).hexdigest() u = User(username=username, first_name=self.cleaned_data['first_name'], last_name=self.cleaned_data['last_name'], email=self.cleaned_data['email'], salt=n, password=password, ) u.save() return None
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')
# Initialize App Engine and import the default settings (DB backend, etc.). # If you want to use a different backend you have to remove all occurences # of "djangoappengine" from this file. from djangoappengine.settings_base import * from my_auth.models import User cur = User.objects.filter(username='******') if not cur: u=User(username="******",password="******",salt=10) u.save() import os PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__)) # Activate django-dbindexer for the default database DATABASES['native'] = DATABASES['default'] DATABASES['default'] = {'ENGINE': 'dbindexer', 'TARGET': 'native'} AUTOLOAD_SITECONF = 'indexes' SECRET_KEY = '=r-$b*8hglm+858&9t043hlm6-&6-3d3vfc4((7yd0dbrakhvi' TIME_ZONE = 'Europe/Minsk' SITE_ID = 1 USE_I18N = True USE_L10N = True MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media') MEDIA_URL = '/media/' INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.contenttypes',