From b28490697f56c39d3fd8754e563195c0ed8955d8 Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Sun, 19 Dec 2021 18:46:26 +0300 Subject: [PATCH] notifications --- .../0008_userinfo_notification_friends.py | 18 +++++++ Main/models/userinfo.py | 7 ++- Main/views/AccountView.py | 13 +++-- templates/account.html | 47 ++++++++++++------- 4 files changed, 60 insertions(+), 25 deletions(-) create mode 100644 Main/migrations/0008_userinfo_notification_friends.py diff --git a/Main/migrations/0008_userinfo_notification_friends.py b/Main/migrations/0008_userinfo_notification_friends.py new file mode 100644 index 0000000..876d475 --- /dev/null +++ b/Main/migrations/0008_userinfo_notification_friends.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.4 on 2021-12-19 15:44 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('Main', '0007_friendship'), + ] + + operations = [ + migrations.AddField( + model_name='userinfo', + name='notification_friends', + field=models.BooleanField(default=False), + ), + ] diff --git a/Main/models/userinfo.py b/Main/models/userinfo.py index d80b177..25a8637 100644 --- a/Main/models/userinfo.py +++ b/Main/models/userinfo.py @@ -5,6 +5,7 @@ from django.db import models from django.db.models import Q from django.utils import timezone +from Main.models.friendship import Friendship from Main.models.set import Set from Main.models.group import Group from Main.models.settask import SetTask @@ -24,6 +25,7 @@ class UserInfo(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE, null=True) telegram_chat_id = models.TextField(default="") notification_solution_result = models.BooleanField(default=False) + notification_friends = models.BooleanField(default=False) code = models.IntegerField(null=True) verified = models.BooleanField(default=False) @@ -33,10 +35,7 @@ class UserInfo(models.Model): @cached_property def friends(self): - return User.objects.filter( - Q(to_friendship__to_user=self, to_friendship__verified=True) - | Q(from_friendship__from_user=self, from_friendship__verified=True) - ) + return Friendship.objects.filter(Q(to_user=self.user) | Q(from_user=self.user)) @property def favourite_language(self): diff --git a/Main/views/AccountView.py b/Main/views/AccountView.py index bc664ff..2a89320 100644 --- a/Main/views/AccountView.py +++ b/Main/views/AccountView.py @@ -44,19 +44,22 @@ class AccountView(BaseView): ).first() if friendship is None: Friendship.objects.create(from_user=self.request.user, to_user=self.context["account"]) - bot.send_message(self.context["account"].userinfo.telegram_chat_id, f"Пользователь {self.request.user.username} хочет добавить тебя в друзья") + if self.context["account"].userinfo.notification_friends: + bot.send_message(self.context["account"].userinfo.telegram_chat_id, f"Пользователь {self.request.user.username} хочет добавить тебя в друзья") elif friendship.verified or friendship.from_user == self.request.user: friendship.delete() else: if self.request.POST["to_do"] == "yes": friendship.verified = True friendship.save() - bot.send_message(self.context["account"].userinfo.telegram_chat_id, - f"Пользователь {self.request.user.username} добавил тебя в друзья") + if self.context["account"].userinfo.notification_friends: + bot.send_message(self.context["account"].userinfo.telegram_chat_id, + f"Пользователь {self.request.user.username} добавил тебя в друзья") else: friendship.delete() - bot.send_message(self.context["account"].userinfo.telegram_chat_id, - f"Пользователь {self.request.user.username} отклонил твою заявку") + if self.context["account"].userinfo.notification_friends: + bot.send_message(self.context["account"].userinfo.telegram_chat_id, + f"Пользователь {self.request.user.username} отклонил твою заявку") return "/account?username=" + self.request.GET["username"] diff --git a/templates/account.html b/templates/account.html index d76638a..ef1784e 100644 --- a/templates/account.html +++ b/templates/account.html @@ -23,22 +23,22 @@

{{ account.userinfo.surname }} {{ account.userinfo.name }} - {{ account.userinfo.activity_status }} -
- {% csrf_token %} - - {% if not owner %} - {% if friendship_status == 0 %} - - {% else %}{% if friendship_status == 1 %} - - {% else %}{% if friendship_status == 2 %} - - {% else %} - - {% endif %}{% endif %}{% endif %} - {% endif %} -
+ {{ account.userinfo.activity_status }} + {% if not owner %} +
+ {% csrf_token %} + + {% if friendship_status == 0 %} + + {% else %}{% if friendship_status == 1 %} + + {% else %}{% if friendship_status == 2 %} + + {% else %} + + {% endif %}{% endif %}{% endif %} +
+ {% endif %} {% if user.is_superuser and owner %} Админ {% endif %} @@ -125,9 +125,24 @@ + + + Заявки в друзья + + + + + +

+

Друзья

+
+ {% for friendship in user.userinfo.friends %} + {% if friendship.to_user == user %}{{ friendship.from_user.username }}{% else %}{{ friendship.to_user.username }}{% endif %}
+ {% endfor %} +
{% endif %} {% endblock %} \ No newline at end of file