def erase_duplicate_sessions(username, session_key, cache_key): user = User.get_user_obj(username=username) previous_session = user.session_key Session.objects.get(session_key=previous_session).delete() cache.delete(f"django.contrib.sessions.cached_{cache_key}") user.session_key = session_key return "complete :)"
def send_notifications(username, reaction, send_to_username=None, post_id=None): if reaction == 'Liked' or reaction == 'Commented': try: post = PostModel.objects.get_post(post_id=post_id) except ObjectDoesNotExist: return "Task aborted, post not found(del?)" send_to = post.user if send_to.username == username: return "User liked/commented_on his/her own post :|" if UserNotification.create_notify_obj(to_notify=send_to, by=username, reaction=reaction, post_obj=post): if reaction == 'Liked': return "like_notif sent successfully :)" else: return "comment_notif sent successfully :)" elif reaction == 'Sent Follow Request' or reaction == 'Replied': send_to = User.get_user_obj(username=send_to_username) if send_to_username == username and reaction == 'Replied': #return f"({username}, {send_to_username})" return "User replied to his/her own comment :|" if UserNotification.create_notify_obj(to_notify=send_to, by=username, reaction=reaction): if reaction == 'Sent Follow Request': return "follow_notif sent successfully :)" else: return "reply_notif sent successfully :)"
def update_user_activity_on_logout(username): user = User.get_user_obj(username=username) user.active = False user.session_key = '' tz = pytz.timezone('Asia/Kolkata') user.last_login = datetime.now().astimezone(tz=tz) user.save() return "complete :)"
def manage_relation(request, username, option=None): current_user = request.user follow_unfollow_user = User.get_user_obj(username=username) if option == 'follow': Friends.follow(current_user, follow_unfollow_user) # Notify the user to whom this follow request is being sent send_notifications.delay( username=current_user.username, reaction="Sent Follow Request", send_to_username=follow_unfollow_user.username) else: Friends.unfollow(current_user, follow_unfollow_user) del_notifications.delay(username=current_user.username, reaction="Sent Follow Request", send_to_username=follow_unfollow_user.username) return redirect(reverse('view_profile', kwargs={'username': username}))
def save(self): account = User(email=self.validated_data['email'], username=self.validated_data['username']) password = self.validated_data['password'] password2 = self.validated_data['password2'] if password != password2: raise serializers.ValidationError( {'password': '******'}) account.set_password(password) account.save() return account
def register(): if current_user.is_authenticated: return redirect(url_for("Main")) form = Registration() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode("utf-8") user = User(username=form.username.data, email=form.email.data, password=hashed_password) db.session.add(user) db.session.commit() flash( f"Account created for {form.username.data}! \n You can now log in!", 'success') # flash(f"You can now log in!",'success') return redirect(url_for('login')) return render_template("register.html", form=form, title="Registration")
def share_posts(username, post_id): try: post = PostModel.objects.get_post(post_id=post_id) except ObjectDoesNotExist: return "Task aborted, post not found(del?)" request_user = User.get_user_obj(username=username) following_list, created = Friends.objects.get_or_create( current_user=request_user) if not created: users = following_list.followers.all() if len(users) > 0: for user in users: post.send_to.add(user) return "complete :)" else: return "user is lonely :(" else: return "user is lonely :("
def create(self, validated_data): user = User(username=validated_data.get('username', None)) user.set_password(validated_data.get('password', None)) user.save() return user
def update_user_activity_on_login(username, session_key=None): user = User.get_user_obj(username=username) user.active = True user.session_key = session_key user.save() return "complete :)"
def create_notify_obj(cls, to_notify, by, reaction, post_obj=None): poked_user = User.get_user_obj(username=by) obj = cls.objects.create(user_to_notify=to_notify, poked_by=poked_user, post=post_obj, reaction=reaction) return obj