friendship

This commit is contained in:
Egor Matveev 2021-12-19 18:17:46 +03:00
parent a4d272bdfe
commit 52b09cd692
7 changed files with 124 additions and 2 deletions

View File

@ -0,0 +1,47 @@
# Generated by Django 3.2.4 on 2021-12-19 14:52
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("Main", "0006_solution_test"),
]
operations = [
migrations.CreateModel(
name="Friendship",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("verified", models.BooleanField(default=False)),
(
"from_user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="from_friendship",
to=settings.AUTH_USER_MODEL,
),
),
(
"to_user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="to_friendship",
to=settings.AUTH_USER_MODEL,
),
),
],
),
]

View File

@ -8,3 +8,4 @@ from Main.models.solution import Solution
from Main.models.extrafile import ExtraFile
from Main.models.progress import Progress
from Main.models.solution_file import SolutionFile
from Main.models.friendship import Friendship

View File

@ -0,0 +1,8 @@
from django.contrib.auth.models import User
from django.db import models
class Friendship(models.Model):
from_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="from_friendship")
to_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="to_friendship")
verified = models.BooleanField(default=False)

View File

@ -1,5 +1,8 @@
from functools import cached_property
from django.contrib.auth.models import User
from django.db import models
from django.db.models import Q
from django.utils import timezone
from Main.models.set import Set
@ -28,6 +31,13 @@ class UserInfo(models.Model):
def has_favourite_language(self):
return self.favourite_language_id is not None
@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)
)
@property
def favourite_language(self):
if not self.has_favourite_language:

View File

@ -1,5 +1,8 @@
from django.contrib.auth.models import User
from django.db.models import Q
from Main.management.commands.bot import bot
from Main.models import Friendship
from SprintLib.BaseView import BaseView
from SprintLib.utils import delete_file, write_bytes
@ -9,7 +12,7 @@ class AccountView(BaseView):
required_login = True
endpoint = "account"
def get(self):
def pre_handle(self):
if "username" in self.request.GET.keys():
self.context["account"] = User.objects.get(
username=self.request.GET["username"]
@ -17,7 +20,45 @@ class AccountView(BaseView):
else:
self.context["account"] = self.request.user
self.context["owner"] = self.context["account"] == self.request.user
def get(self):
self.context["error_message"] = self.request.GET.get("error_message", "")
friendship = Friendship.objects.filter(
Q(from_user=self.request.user, to_user=self.context["account"])
| Q(to_user=self.request.user, from_user=self.context["account"])
).first()
if friendship is None:
self.context["friendship_status"] = 0
elif friendship.verified:
self.context["friendship_status"] = 1
elif friendship.from_user == self.request.user:
self.context["friendship_status"] = 2
else:
self.context["friendship_status"] = 3
def post_friendship(self):
if "to_do" in self.request.POST:
friendship = Friendship.objects.filter(
Q(from_user=self.request.user, to_user=self.context["account"])
| Q(to_user=self.request.user, from_user=self.context["account"])
).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} хочет добавить тебя в друзья")
elif friendship.verified or friendship.from_user == self.request.user:
friendship.delete()
else:
if self.request.POST["todo"] == "yes":
friendship.verified = True
friendship.save()
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} отклонил твою заявку")
return "/account?username=" + self.request.GET["username"]
def post_upload_photo(self):
if self.request.user.userinfo.has_profile_pic:

View File

@ -24,6 +24,21 @@
<h3>
{{ 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>
<form method="POST">
{% csrf_token %}
<input type="hidden" name="action" value="friendship">
{% if not owner %}
{% if friendship_status == 0 %}
<button type="submit" class="btn btn-primary" name="to_do" value="add">Добавить в друзья</button>
{% 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>
{% else %}{% if friendship_status == 2 %}
<button class="btn btn-info">Приглашение отправлено</button> <button class="btn btn-danger" type="submit" name="to_do" value="delete">Отменить</button>
{% 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>
{% endif %}{% endif %}{% endif %}
{% endif %}
</form>
{% if user.is_superuser and owner %}
<a style="margin-left: 15px;" href="/admin/" class="badge badge-secondary">Админ</a>
{% endif %}

View File

@ -24,7 +24,7 @@
{% block main %}
<h2>Решения</h2>
<h4>Фильтр</h4>
<h4>Фильтр (пока недоступная опция)</h4>
<div>
<select name="set_id" style="width: 33%">
<option value="0">Все сеты</option>