Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def item(request, item_id):
post = get_object_or_404(
Post.objects.select_related().filter(id=item_id, status=1)[:1])
Post.objects.filter(id=item_id).update(view=F('view') + 1)
post.tag = post.tags.all()
if request.user.is_superuser and request.GET.get('ip', None):
post.comments = Comments.objects.filter(object_pk=post)
post.likes = Likes.objects.filter(post=post).order_by('ip')[:10]
else:
post.comments = Comments.objects.filter(object_pk=post, is_public=True)
post.likes = Likes.objects.filter(post=post)[:10]
try:
post.prev = Post.objects.filter(status=1)\
.extra(where=['id<%s'], params=[post.id]).order_by('-id')[:1][0]
post.next = Post.objects.filter(status=1)\
.extra(where=['id>%s'], params=[post.id]).order_by('id')[:1][0]
except:
pass
follow_status = Follow.objects.filter(follower=request.user.id,
following=post.user.id).count()
if request.is_ajax():
return render(request, 'pin/item_inner.html',
{'post': post, 'follow_status': follow_status})
def delete_all_user_comments(self, request, queryset):
for obj in queryset:
user = obj.user
user.is_active = False
user.save()
Comments.objects.filter(user=obj.user).delete()
def item(request, item_id):
post = get_object_or_404(
Post.objects.select_related().filter(id=item_id, status=1)[:1])
Post.objects.filter(id=item_id).update(view=F('view') + 1)
post.tag = post.tags.all()
if request.user.is_superuser and request.GET.get('ip', None):
post.comments = Comments.objects.filter(object_pk=post)
post.likes = Likes.objects.filter(post=post).order_by('ip')[:10]
else:
post.comments = Comments.objects.filter(object_pk=post, is_public=True)
post.likes = Likes.objects.filter(post=post)[:10]
try:
post.prev = Post.objects.filter(status=1)\
.extra(where=['id<%s'], params=[post.id]).order_by('-id')[:1][0]
post.next = Post.objects.filter(status=1)\
.extra(where=['id>%s'], params=[post.id]).order_by('id')[:1][0]
except:
pass
follow_status = Follow.objects.filter(follower=request.user.id,
following=post.user.id).count()
def dehydrate(self, bundle):
user = bundle.data['user_url']
bundle.data['user_avatar'] = userdata_cache(user, CACHE_AVATAR)
bundle.data['user_name'] = userdata_cache(user, CACHE_USERNAME)
return bundle
class CommentResource(ModelResource):
user_url = fields.IntegerField(attribute='user_id', null=True)
object_pk = fields.IntegerField(attribute='object_pk_id', null=True)
class Meta:
allowed_methods = ['get']
queryset = Comments.objects.filter(is_public=True)
resource_name = "comments"
paginator_class = Paginator
#fields = ['id', 'comment', 'object_pk', 'user_id', 'score', 'submit_date']
excludes = ['ip_address', 'is_public', 'object_pk', 'reported']
cache = SimpleCache(timeout=120)
#limit = 1000
filtering = {
"object_pk": ('exact',),
}
def dehydrate(self, bundle):
user = bundle.data['user_url']
bundle.data['user_avatar'] = userdata_cache(user, CACHE_AVATAR)
bundle.data['user_name'] = userdata_cache(user, CACHE_USERNAME)
return bundle
def cnt_comments(self):
if self.cnt_comment == -1:
cnt = Comments.objects.filter(object_pk_id=self.id).count()
Post.objects.filter(pk=self.id).update(cnt_comment=cnt)
else:
cnt = self.cnt_comment
return cnt
def delete(self, *args, **kwargs):
Post.objects.filter(pk=self.object_pk.id).update(cnt_comment=F('cnt_comment')-1)
super(Comments, self).delete(*args, **kwargs)
delete_and_deactive_user.short_description = 'حذف و غیر فعال کردن کاربر'
def delete_all_user_comments(self, request, queryset):
for obj in queryset:
user = obj.user
user.is_active = False
user.save()
Comments.objects.filter(user=obj.user).delete()
delete_all_user_comments.short_description = 'حذف تمام کامنت های این کاربر و غیر فعال کردن کاربر'
admin.site.register(Post, PinAdmin)
admin.site.register(Category, CategoryAdmin)
admin.site.register(Profile, ProfileAdmin)
admin.site.register(App_data, AppAdmin)
admin.site.register(Comments, CommentsAdmin)
def delete(self, *args, **kwargs):
Post.objects.filter(pk=self.object_pk.id).update(cnt_comment=F('cnt_comment')-1)
super(Comments, self).delete(*args, **kwargs)
@models.permalink
def get_absolute_url(self):
return ('pin-item', [str(self.object_pk_id)])
def admin_link(self):
return '<a href="%s">مشاهده</a>' % (self.get_absolute_url())
admin_link.allow_tags = True
class Comments_score(models.Model):
comment = models.ForeignKey(Comments)
user = models.ForeignKey(User, related_name='comment_like_user')
score = models.IntegerField(default=0, blank=True)
class Report(models.Model):
user = models.ForeignKey(User, related_name='report_user')
post = models.ForeignKey(Post, related_name='report_post')
class Meta:
unique_together = (("post", "user"),)
post_save.connect(Stream.add_post, sender=Post)
post_save.connect(Likes.user_like_post, sender=Likes)
#post_delete.connect(Likes.user_unlike_post, sender=Likes)
post_save.connect(Post.change_tag_slug, sender=Tag)
post_save.connect(Notif.add_comment, sender=Comments)