def validate_user(self, username): """ Validates user during registration """ utils.check_user(username) # hardcoded ok check in register.js return settings.VALIDATION_IS_OK
def validate_user(request): """ Validates user during registration """ utils.check_user(request.POST.get('username')) # hardcoded ok check in register.js return HttpResponse(settings.VALIDATION_IS_OK, content_type='text/plain')
def create_user_profile(self, email, name, surname, picture): try: user_profile = UserProfile.objects.get(email=email) self.logger.info("Sign in as %s with id %s", user_profile.username, user_profile.id) except UserProfile.DoesNotExist: try: self.logger.info( "Creating new user with email %s, name %s, surname %s, picture %s", email, name, surname, picture) # replace all characters but a valid one with '-' and cut to 15 chars username = re.sub('[^0-9a-zA-Z-_]+', '-', email.rsplit('@')[0])[:15] check_user(username) except ValidationError as e: self.logger.info("Can't use username %s because %s", username, e) username = id_generator(8) self.logger.debug("Generated username: %s", username) user_profile = UserProfile(name=name, surname=surname, email=email, username=username) self.download_http_photo(picture, user_profile) user_profile.save() RoomUsers(user_id=user_profile.id, room_id=settings.ALL_ROOM_ID, notifications=False).save() return user_profile
def register(self, username, password, email, sex): check_user(username) self.__check_password(password) self.__check_email__(email) user_profile = UserProfile(username=username, email=email, sex_str=sex) user_profile.set_password(password) user_profile.save() RoomUsers(user_id=user_profile.id, room_id=settings.ALL_ROOM_ID, notifications=False).save() if email: yield from self.__send_sign_up_email(user_profile) return self.__generate_session__(user_profile.id)
def validate_user(request): """ Validates user during registration """ try: username = request.POST.get('username') utils.check_user(username) # hardcoded ok check in register.js message = VALIDATION_IS_OK except ValidationError as e: message = e.message return HttpResponse(message, content_type='text/plain')
def get_user_name(self, email, name, surname): try: # replace all characters but a valid one with '-' and cut to 15 chars if email: username = re.sub('[^0-9a-zA-Z-_]+', '-', email.rsplit('@')[0])[:15] else: username = f'{name}_{surname}' check_user(username) except ValidationError as e: self.logger.info("Can't use username %s because %s", username, e) username = id_generator(8) self.logger.debug("Generated username: %s", username) return username
def profile_save_user(self, in_message): message = in_message[VarNames.CONTENT] userprofile = UserProfile.objects.get(id=self.user_id) email_verification_id = userprofile.email_verification_id un = message[UserProfileVarNames.USERNAME] email = message[UserProfileVarNames.EMAIL] if userprofile.username != un: check_user(un) if userprofile.email != email: check_email(email) if userprofile.email and userprofile.email_verification and userprofile.email_verification.verified: verification = Verification( type_enum=Verification.TypeChoices.email, user_id=self.id, email=email) verification.save() send_email_change(self.request, un, userprofile.email, verification, email) self.ws_write( self.default( "In order to change an email please confirm it from you current address. We send you an verification email to {}." .format(userprofile.email), Actions.GROWL_MESSAGE, HandlerNames.WS)) email = userprofile.email # Don't change email, we need to verify it! elif email: new_ver = send_new_email_ver(self.request, userprofile, email) email_verification_id = new_ver.id sex = message[UserProfileVarNames.SEX] UserProfile.objects.filter(id=self.user_id).update( username=un, name=message[UserProfileVarNames.NAME], city=message[UserProfileVarNames.CITY], surname=message[UserProfileVarNames.SURNAME], email=email, birthday=message[UserProfileVarNames.BIRTHDAY], contacts=message[UserProfileVarNames.CONTACTS], sex=settings.GENDERS_STR[sex], email_verification=email_verification_id) self.publish( self.set_user_profile(in_message[VarNames.JS_MESSAGE_ID], message), self.channel) if userprofile.sex_str != sex or userprofile.username != un: self.publish(self.changed_user_profile(sex, self.user_id, un), settings.ALL_ROOM_ID)
def profile_save_user(self, in_message): message = in_message[VarNames.CONTENT] userprofile = UserProfile.objects.get(id=self.user_id) un = message[UserProfileVarNames.USERNAME] if userprofile.username != un: check_user(un) sex = message[UserProfileVarNames.SEX] UserProfile.objects.filter(id=self.user_id).update( username=un, name=message[UserProfileVarNames.NAME], city=message[UserProfileVarNames.CITY], surname=message[UserProfileVarNames.SURNAME], birthday=message[UserProfileVarNames.BIRTHDAY], contacts=message[UserProfileVarNames.CONTACTS], sex=settings.GENDERS_STR[sex], ) self.publish(self.message_creator.set_user_profile(in_message[VarNames.JS_MESSAGE_ID], message), self.channel) if userprofile.sex_str != sex or userprofile.username != un: self.publish(self.message_creator.changed_user_profile(sex, self.user_id, un), settings.ALL_ROOM_ID)
def post(self, request): rp = request.POST logger.info('Got register request %s', hide_fields(rp, ('password', 'repeatpassword'))) (username, password, email) = (rp.get('username'), rp.get('password'), rp.get('email')) check_user(username) check_password(password) check_email(email) user_profile = UserProfile(username=username, email=email, sex_str=rp.get('sex')) user_profile.set_password(password) create_user_model(user_profile) # You must call authenticate before you can call login auth_user = authenticate(username=username, password=password) if email: send_sign_up_email(user_profile, request.get_host(), request) djangologin(request, auth_user) return HttpResponse(settings.VALIDATION_IS_OK, content_type='text/plain')
def post(self, request): try: rp = request.POST logger.info('Got register request %s', hide_fields(rp, ('password', 'repeatpassword'))) (username, password, email) = (rp.get('username'), rp.get('password'), rp.get('email')) check_user(username) check_password(password) check_email(email) user_profile = UserProfile(username=username, email=email, sex_str=rp.get('sex')) user_profile.set_password(password) create_user_model(user_profile) # You must call authenticate before you can call login auth_user = authenticate(username=username, password=password) message = VALIDATION_IS_OK # redirect if email: send_email_verification(user_profile, request.get_host()) djangologin(request, auth_user) except ValidationError as e: message = e.message logger.debug('Rejecting request because "%s"', message) return HttpResponse(message, content_type='text/plain')
def register(self, username, password, email, sex): check_user(username) self.__check_password(password) self.__check_email__(email) user_profile = UserProfile(username=username, email=email, sex_str=sex) user_profile.set_password(password) user_profile.save() RoomUsers(user_id=user_profile.id, room_id=settings.ALL_ROOM_ID, notifications=False).save() user_data = { VarNames.ROOMS: [{ VarNames.ROOM_ID: settings.ALL_ROOM_ID, VarNames.ROOM_USERS: list( RoomUsers.objects.filter(room_id=ALL_ROOM_ID).values_list( 'user_id', flat=True)) }], VarNames.EVENT: Actions.CREATE_NEW_USER, VarNames.HANDLER_NAME: HandlerNames.ROOM, } user_data.update( RedisPrefix.set_js_user_structure(user_profile.id, user_profile.username, user_profile.sex, None)) global_redis.async_redis_publisher.publish( settings.ALL_ROOM_ID, json.dumps(user_data), ) if email: yield from self.__send_sign_up_email(user_profile) return MessagesCreator.get_session( self.__generate_session__(user_profile.id))