def upload_profile_image(self, files): """ POST only, validates email during registration """ input_file = files['file'] up = UserProfile(photo=input_file, id=self.user_id) create_thumbnail(input_file, up) up.save(update_fields=('photo', 'thumbnail')) up = UserProfile.objects.values('sex', 'thumbnail', 'photo', 'username').get(id=self.user_id) payload = { VarNames.HANDLER_NAME: HandlerNames.WS, VarNames.EVENT: Actions.USER_PROFILE_CHANGED } payload.update( RedisPrefix.set_js_user_structure( self.user_id, up['username'], up['sex'], get_thumbnail_url(up['thumbnail']), )) global_redis.sync_redis.publish(settings.ALL_ROOM_ID, json.dumps(payload)) global_redis.sync_redis.publish( RedisPrefix.generate_user(self.user_id), json.dumps({ VarNames.HANDLER_NAME: HandlerNames.WS, VarNames.EVENT: Actions.SET_PROFILE_IMAGE, VarNames.CONTENT: get_thumbnail_url(up['photo']), })) return settings.VALIDATION_IS_OK
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 create_user_profile(self, user_profile_query, name, surname, picture=None, email=None, fb_id=None, google_id=None): try: user_profile = UserProfile.objects.get(**user_profile_query) self.logger.info("Sign in as %s with id %s", user_profile.username, user_profile.id) except UserProfile.DoesNotExist: if email and UserProfile.objects.filter(email=email).exists(): raise ValidationError( f"User with email {email} already exists. If this is you, please sign in from your account and connect with social auth in settings." ) self.logger.info( "Creating new user with email %s, name %s, surname %s, picture %s", email, name, surname, picture) username = self.get_user_name(email, name, surname) user_profile = UserProfile(name=name, surname=surname, email=email, username=username, facebook_id=fb_id, google_id=google_id) 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 upload_profile_image(self, files): """ POST only, validates email during registration """ up = UserProfile(photo=files['file'], id=self.user_id) up.save(update_fields=('photo',)) url = up.photo.url message = json.dumps(MessagesCreator.set_profile_image(url)) channel = RedisPrefix.generate_user(self.user_id) global_redis.sync_redis.publish(channel, message) return settings.VALIDATION_IS_OK
def upload_profile_image(request): """ POST only, validates email during registration """ up = UserProfile(photo=request.FILES['file'], id=request.user.id) up.save(update_fields=('photo', )) url = up.photo.url message = json.dumps(MessagesCreator.set_profile_image(url)) channel = RedisPrefix.generate_user(request.user.id) global_redis.sync_redis.publish(channel, message) return HttpResponse(settings.VALIDATION_IS_OK, content_type='text/plain')
def test_animals_can_speak(self): user_profile = UserProfile(name='test', surname='test', email='*****@*****.**', username='******') gauth = GoogleAuth() gauth.download_http_photo( 'https://lh4.googleusercontent.com/-CuLSUOTQ4Kw/AAAAAAAAAAI/AAAAAAAAANQ/VlgHrqehE90/s96-c/photo.jpg', user_profile)
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() if email: yield from self.__send_sign_up_email(user_profile) return self.__generate_session__(user_profile.id)
def user_list(request, pk=None): """ List all required messages, or create a new message. """ if request.method == 'GET': if pk: users = infor.objects.filter(user = User.objects.get(id=pk)) else: users = [] for i in Message.objects.all(): if i.sender == request.user or i.receiver == request.user: a = infor.objects.get(user=i.sender) b = infor.objects.get(user=i.receiver) if a not in users: users.append(a) if b not in users: users.append(b) data = [] for i in users: a = {'id':i.user.id,'username':i.user.username} try: a['online'] = UserProfile.objects.get(user=i.user).online() except: obj = UserProfile() obj.user = i.user obj.save() a['online'] = obj.online() if i.profile_check == False: try: profileimg = i.user.socialaccount_set.all()[0].provider profileimg = i.user.socialaccount_set.all()[0].get_avatar_url() except: profileimg = "/static/profile/"+str(i.profile)+".jpg" else: profileimg = "/media/"+str(i.profile_pic) a['profileurl'] = profileimg data.append(a) return JsonResponse(data,safe=False) elif request.method == 'POST': data = JSONParser().parse(request) try: user = User.objects.create_user(username=data['username'], password=data['password']) UserProfile.objects.create(user=user) return JsonResponse(data, status=201) except Exception: return JsonResponse({'error': "Something went wrong"}, status=400)
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))
def reconnect(self): return UserProfile(self.userprofile).rooms.all()