Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add violations backend code #447

Merged
merged 11 commits into from
May 10, 2022
64 changes: 43 additions & 21 deletions zubhub_backend/zubhub/projects/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from treebeard.admin import TreeAdmin
from treebeard.forms import movenodeform_factory
from django.conf import settings
from .models import (Project, Comment, Image, StaffPick,
Category, Tag, PublishingRule)
from .models import (Project, Comment, Image, StaffPick, Category, Tag,
PublishingRule, Violation, ViolationReason)
from .utils import project_changed, send_staff_pick_notification
from projects.tasks import delete_file_task
from creators.utils import activity_notification
Expand All @@ -24,20 +24,22 @@ class InlineProjectComments(admin.StackedInline):
class InlineProject(admin.StackedInline):
model = Project.staff_picks.through


class ImageAdmin(admin.ModelAdmin):
search_fields = ["project__title", "image_url"]
list_display = ["image_url"]
exclude = ["id"]
list_per_page = 50 ## paginate when more than 50 items
list_per_page = 50 ## paginate when more than 50 items


class CommentAdmin(TreeAdmin):
list_display = ["creator", "text", "created_on", "publish"]

search_fields = ["project__tite",
"creator__username", "text", "created_on"]

search_fields = [
"project__tite", "creator__username", "text", "created_on"
]
list_filter = ["created_on"]
list_per_page = 50 ## paginate when more than 50 items
list_per_page = 50 ## paginate when more than 50 items

form = movenodeform_factory(Comment)

Expand All @@ -51,18 +53,24 @@ def get_readonly_fields(self, request, obj=None):


class ProjectAdmin(admin.ModelAdmin):
list_display = ["title", "creator", "views_count", "likes_count",
"comments_count", "created_on", "publish"]

search_fields = ["title", 'creator__username', 'creator__email',
"created_on"]
list_display = [
"title", "creator", "views_count", "likes_count", "comments_count",
"created_on", "publish"
]

search_fields = [
"title", 'creator__username', 'creator__email', "created_on"
]
list_filter = ['created_on']
inlines = [InlineProjectImages, InlineProjectComments]
exclude = ["search_vector"]
list_per_page = 50 ## paginate when more than 50 items
list_per_page = 50 ## paginate when more than 50 items

def get_readonly_fields(self, request, obj=None):
return ["id", "slug", "views_count", "likes_count", "comments_count", "created_on"]
return [
"id", "slug", "views_count", "likes_count", "comments_count",
"created_on"
]

def save_model(self, request, obj, form, change):
if change:
Expand All @@ -72,10 +80,12 @@ def save_model(self, request, obj, form, change):

if change:
new = Project.objects.get(pk=obj.pk)
if old.video.find("cloudinary.com") > -1 and old.video != new.video:
if old.video.find(
"cloudinary.com") > -1 and old.video != new.video:
delete_file_task.delay(old.video)
elif old.video.startswith("{0}://{1}".format(settings.DEFAULT_MEDIA_SERVER_PROTOCOL,
settings.DEFAULT_MEDIA_SERVER_DOMAIN)) and old.video != new.video:
elif old.video.startswith("{0}://{1}".format(
settings.DEFAULT_MEDIA_SERVER_PROTOCOL, settings.
DEFAULT_MEDIA_SERVER_DOMAIN)) and old.video != new.video:
delete_file_task.delay(old.video)
if project_changed(old, new):
info = {
Expand All @@ -85,8 +95,6 @@ def save_model(self, request, obj, form, change):
activity_notification(["edited_project"], **info)




def projects_count(obj):
if obj:
return obj.projects.count()
Expand Down Expand Up @@ -136,7 +144,19 @@ class tagAdmin(admin.ModelAdmin):
search_fields = ["name"]
readonly_fields = ["slug"]
exclude = ["id", "search_vector"]
list_per_page = 50 ## paginate when more than 50 items
list_per_page = 50 ## paginate when more than 50 items


class ViolationReasonsAdmin(admin.ModelAdmin):
search_fields = ['description']
exclude = ["id"]
list_per_page = 50


class ViolationAdmin(admin.ModelAdmin):
search_fields = ["name"]
exclude = ["id"]
list_per_page = 50


admin.site.register(Project, ProjectAdmin)
Expand All @@ -145,3 +165,5 @@ class tagAdmin(admin.ModelAdmin):
admin.site.register(Category, categoryAdmin)
admin.site.register(Tag, tagAdmin)
admin.site.register(StaffPick, StaffPickAdmin)
admin.site.register(ViolationReason, ViolationReasonsAdmin)
admin.site.register(Violation, ViolationAdmin)
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Generated by Django 3.2 on 2022-04-23 02:17

from django.conf import settings
import django.contrib.postgres.indexes
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import uuid


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('projects', '0006_auto_20220409_1917'),
]

operations = [
migrations.CreateModel(
name='Violation',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, unique=True)),
('date', models.DateTimeField(default=django.utils.timezone.now)),
],
),
migrations.CreateModel(
name='ViolationReason',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, unique=True)),
('description', models.CharField(max_length=250)),
],
),
migrations.AddIndex(
model_name='tag',
index=django.contrib.postgres.indexes.GinIndex(fields=['name'], name='tag_name_gin_idx', opclasses=['gin_trgm_ops']),
),
migrations.AddField(
model_name='violation',
name='creator',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='violation',
name='project',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='violations', to='projects.project'),
),
migrations.AddField(
model_name='violation',
name='reasons',
field=models.ManyToManyField(blank=True, null=True, related_name='violations', to='projects.ViolationReason'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2 on 2022-04-23 02:18

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('projects', '0007_auto_20220423_0217'),
]

operations = [
migrations.AlterField(
model_name='violation',
name='reasons',
field=models.ManyToManyField(blank=True, related_name='violations', to='projects.ViolationReason'),
),
]
28 changes: 28 additions & 0 deletions zubhub_backend/zubhub/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,31 @@ def save(self, *args, **kwargs):
uid = uid[0:floor(len(uid) / 6)]
self.slug = slugify(self.title) + "-" + uid
super().save(*args, **kwargs)


class ViolationReason(models.Model):
id = models.UUIDField(primary_key=True,
default=uuid.uuid4,
editable=False,
unique=True)
description = models.CharField(max_length=250)


class Violation(models.Model):
id = models.UUIDField(primary_key=True,
default=uuid.uuid4,
editable=False,
unique=True)
reasons = models.ManyToManyField(ViolationReason,
blank=True,
related_name='violations')
project = models.ForeignKey(Project,
null=True,
blank=True,
on_delete=models.CASCADE,
related_name='violations')
creator = models.ForeignKey(Creator,
null=True,
blank=True,
on_delete=models.SET_NULL)
date = models.DateTimeField(default=timezone.now)
Loading