notifications

This commit is contained in:
Egor Matveev 2021-12-19 18:46:26 +03:00
parent 5a3e696eac
commit b28490697f
4 changed files with 60 additions and 25 deletions

View File

@ -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),
),
]

View File

@ -5,6 +5,7 @@ from django.db import models
from django.db.models import Q from django.db.models import Q
from django.utils import timezone from django.utils import timezone
from Main.models.friendship import Friendship
from Main.models.set import Set from Main.models.set import Set
from Main.models.group import Group from Main.models.group import Group
from Main.models.settask import SetTask from Main.models.settask import SetTask
@ -24,6 +25,7 @@ class UserInfo(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True) user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
telegram_chat_id = models.TextField(default="") telegram_chat_id = models.TextField(default="")
notification_solution_result = models.BooleanField(default=False) notification_solution_result = models.BooleanField(default=False)
notification_friends = models.BooleanField(default=False)
code = models.IntegerField(null=True) code = models.IntegerField(null=True)
verified = models.BooleanField(default=False) verified = models.BooleanField(default=False)
@ -33,10 +35,7 @@ class UserInfo(models.Model):
@cached_property @cached_property
def friends(self): def friends(self):
return User.objects.filter( return Friendship.objects.filter(Q(to_user=self.user) | Q(from_user=self.user))
Q(to_friendship__to_user=self, to_friendship__verified=True)
| Q(from_friendship__from_user=self, from_friendship__verified=True)
)
@property @property
def favourite_language(self): def favourite_language(self):

View File

@ -44,19 +44,22 @@ class AccountView(BaseView):
).first() ).first()
if friendship is None: if friendship is None:
Friendship.objects.create(from_user=self.request.user, to_user=self.context["account"]) 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: elif friendship.verified or friendship.from_user == self.request.user:
friendship.delete() friendship.delete()
else: else:
if self.request.POST["to_do"] == "yes": if self.request.POST["to_do"] == "yes":
friendship.verified = True friendship.verified = True
friendship.save() friendship.save()
bot.send_message(self.context["account"].userinfo.telegram_chat_id, if self.context["account"].userinfo.notification_friends:
f"Пользователь {self.request.user.username} добавил тебя в друзья") bot.send_message(self.context["account"].userinfo.telegram_chat_id,
f"Пользователь {self.request.user.username} добавил тебя в друзья")
else: else:
friendship.delete() friendship.delete()
bot.send_message(self.context["account"].userinfo.telegram_chat_id, if self.context["account"].userinfo.notification_friends:
f"Пользователь {self.request.user.username} отклонил твою заявку") bot.send_message(self.context["account"].userinfo.telegram_chat_id,
f"Пользователь {self.request.user.username} отклонил твою заявку")
return "/account?username=" + self.request.GET["username"] return "/account?username=" + self.request.GET["username"]

View File

@ -23,22 +23,22 @@
<div class="col-9"> <div class="col-9">
<h3> <h3>
{{ account.userinfo.surname }} {{ account.userinfo.name }} {{ account.userinfo.surname }} {{ account.userinfo.name }}
<span style="margin-left: 15px;" class="badge badge-{% if account.userinfo.activity_status == online_status %}success{% else %}secondary{% endif %}">{{ account.userinfo.activity_status }}</span> <span style="margin-left: 15px; margin-bottom: 20px;" class="badge badge-{% if account.userinfo.activity_status == online_status %}success{% else %}secondary{% endif %}">{{ account.userinfo.activity_status }}</span>
<form method="POST"> {% if not owner %}
{% csrf_token %} <form method="POST">
<input type="hidden" name="action" value="friendship"> {% csrf_token %}
{% if not owner %} <input type="hidden" name="action" value="friendship">
{% if friendship_status == 0 %} {% if friendship_status == 0 %}
<button type="submit" class="btn btn-primary" name="to_do" value="add">Добавить в друзья</button> <button type="submit" class="btn btn-primary" name="to_do" value="add">Добавить в друзья</button>
{% else %}{% if friendship_status == 1 %} {% else %}{% if friendship_status == 1 %}
<button class="btn btn-success"><i class="fa fa-check"></i> Друзья</button> <button class="btn btn-danger" type="submit" name="to_do" value="delete"><i class="fa fa-times"></i> Удалить</button> <button class="btn btn-success"><i class="fa fa-check"></i> Друзья</button> <button class="btn btn-danger" type="submit" name="to_do" value="delete"><i class="fa fa-times"></i> Удалить</button>
{% else %}{% if friendship_status == 2 %} {% else %}{% if friendship_status == 2 %}
<button class="btn btn-info">Приглашение отправлено</button> <button class="btn btn-danger" type="submit" name="to_do" value="delete">Отменить</button> <button class="btn btn-info">Приглашение отправлено</button> <button class="btn btn-danger" type="submit" name="to_do" value="delete">Отменить</button>
{% else %} {% else %}
<button class="btn btn-info" type="submit" name="to_do" value="yes">Принять</button> <button class="btn btn-danger" type="submit" name="to_do" value="no">Отклонить</button> <button class="btn btn-info" type="submit" name="to_do" value="yes">Принять</button> <button class="btn btn-danger" type="submit" name="to_do" value="no">Отклонить</button>
{% endif %}{% endif %}{% endif %} {% endif %}{% endif %}{% endif %}
{% endif %} </form>
</form> {% endif %}
{% if user.is_superuser and owner %} {% if user.is_superuser and owner %}
<a style="margin-left: 15px;" href="/admin/" class="badge badge-secondary">Админ</a> <a style="margin-left: 15px;" href="/admin/" class="badge badge-secondary">Админ</a>
{% endif %} {% endif %}
@ -125,9 +125,24 @@
<input type="checkbox" name="notification_solution_result" {% if user.userinfo.notification_solution_result %}checked{% endif %}> <input type="checkbox" name="notification_solution_result" {% if user.userinfo.notification_solution_result %}checked{% endif %}>
</td> </td>
</tr> </tr>
<tr>
<td style="width: 200px;">
Заявки в друзья
</td>
<td>
<input type="checkbox" name="notification_friends" {% if user.userinfo.notification_friends %}checked{% endif %}>
</td>
</tr>
</table> </table>
<button type="submit" class="btn btn-light" style="margin-top: 15px;"><i class="fa fa-save"></i> Сохранить</button> <button type="submit" class="btn btn-light" style="margin-top: 15px;"><i class="fa fa-save"></i> Сохранить</button>
</form> </form>
<hr><hr>
<h2 style="margin-bottom: 20px;">Друзья</h2>
<h5>
{% for friendship in user.userinfo.friends %}
<i class="fa fa-user"></i> <a href="/account?username={% if friendship.to_user == user %}{{ friendship.from_user.username }}{% else %}{{ friendship.to_user.username }}{% endif %}">{% if friendship.to_user == user %}{{ friendship.from_user.username }}{% else %}{{ friendship.to_user.username }}{% endif %}</a><br>
{% endfor %}
</h5>
{% endif %} {% endif %}
{% endblock %} {% endblock %}