{% if user.is_authenticated %}
<div class="userblock">
<a href="{% url 'users:profile' %}">
<div class="userblock">
<div class="userblock_pic">
< img src="{{user.avatar.url}}" draggable="false">
</div>
</div>
</a>
</div>
{% else %}
<div class="login">
<span class="login_text">Войти</span>
</div>
{% endif %}
class UserProfile(AbstractUser):
bio = models.TextField(blank=True, null=True, max_length=250, default=None)
avatar = models.ImageField(upload_to='avatars', default='avatars/no-avatar.png')
profile_hidden = models.BooleanField('Приватность', default=False, db_index=True, help_text='Не показывать мои комментарии среди лучших и скрыть аккаунт от посторонних глаз')
comment_karma = models.IntegerField(default=0)
link_karma = models.IntegerField(default=0)
class Meta:
verbose_name = 'Аккаунт'
verbose_name_plural = 'Аккаунты'
def __str__(self):
return self.username
def my_profile(request, pk):
profile = get_object_or_404(UserProfile, pk=pk)
return render(request, 'users/profile.html', {'profile': profile})
class UserProfile(AbstractUser):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
bio = models.TextField(blank=True, null=True, max_length=250, default=None)
def my_profile(request, pk):
user = get_object_or_404(UserProfile, pk=pk)
profile = UserProfile.objects.get(user=user)
return render(request, 'users/profile.html', {'profile': profile})
class PostList(generic.ListView):
template_name = 'posts/post_list.html'
context_object_name = 'post_list'
def get_queryset(self):
return Post.objects.filter(date_add__lte = timezone.now(), cheers__gte = 2).order_by('-date_add')
@login_required
def add_cheer(request, pk):
post = Post.objects.get(pk=pk)
post.cheers.add(request.user)
post.save()
return redirect('posts:fullstory', pk=pk)
@login_required
def remove_cheer(request, pk):
post = Post.objects.get(pk=pk)
post.cheers.remove(request.user)
post.save()
return redirect('posts:fullstory', pk=pk)
class Post(models.Model):
title = models.CharField(max_length=100, verbose_name='Заголовок')
text = models.TextField('Содержание')
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name='Автор')
date_add = models.DateTimeField(default=timezone.now, verbose_name='Дата создания')
date_public = models.DateTimeField(blank=True, null=True, verbose_name='Дата публикации')
category = models.ForeignKey(Category, verbose_name="Категория", on_delete=models.SET_NULL, null=True)
cheers = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name="post_cheers", symmetrical=False, blank=True)
click_count = models.PositiveIntegerField(default=0, verbose_name='Просмотров')
def __str__(self):
return self.title
def author_string(self):
return str(self.author)
def all_comments(self):
return self.comments.all().order_by('-date_add')
def publish(self):
self.date_public = timezone.now()
self.save()
def click_increase(self):
self.click_count += 1
self.save(update_fields=['click_count'])
def get_absolute_url(self):
return reverse('posts:fullstory', kwargs={"pk": self.pk})