Fakultas Ilmu Komputer UI

Commit 9f170065 authored by Sayid Abyan Rizal Shiddiq's avatar Sayid Abyan Rizal Shiddiq
Browse files

[#60] Material: Notify Admin for Approval when a Contributor Uploads New Material

parent 4e11f0a2
...@@ -9,7 +9,7 @@ from django.utils import timezone ...@@ -9,7 +9,7 @@ from django.utils import timezone
from administration.models import VerificationReport, VerificationSetting, DeletionHistory from administration.models import VerificationReport, VerificationSetting, DeletionHistory
from administration.forms import CategoryForm, VerificationSettingForm, RegistrasiAdminForm, PeriodForm, EditAdminStatusForm, EditKontributorStatusForm from administration.forms import CategoryForm, VerificationSettingForm, RegistrasiAdminForm, PeriodForm, EditAdminStatusForm, EditKontributorStatusForm
from administration.services import StatisticService, DetailVerificationService, LaporanMateriService from administration.services import StatisticService, DetailVerificationService, LaporanMateriService
from app.models import Category, Materi, ViewStatistics, DownloadStatistics, Comment, Like, LaporanMateri from app.models import Category, Materi, ViewStatistics, DownloadStatistics, Comment, Like, LaporanMateri, AdminNotification
from authentication.models import User from authentication.models import User
from datetime import datetime from datetime import datetime
...@@ -87,6 +87,8 @@ class DetailVerificationView(TemplateView): ...@@ -87,6 +87,8 @@ class DetailVerificationView(TemplateView):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs) context = self.get_context_data(**kwargs)
if(request.user.is_admin):
AdminNotification.objects.filter(materi=context["materi_data"]).delete()
return self.render_to_response(context=context) return self.render_to_response(context=context)
......
from django.contrib import admin from django.contrib import admin
from .models import AdminNotification
# Register your models here. # Register your models here.
admin.site.register(AdminNotification)
\ No newline at end of file
# Generated by Django 3.1 on 2020-10-31 14:46
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('app', '0027_readlater'),
]
operations = [
migrations.CreateModel(
name='AdminNotification',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('materi', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.materi')),
],
),
]
...@@ -329,4 +329,7 @@ class ReadLater(models.Model): ...@@ -329,4 +329,7 @@ class ReadLater(models.Model):
timestamp = models.DateTimeField(default=timezone.now) timestamp = models.DateTimeField(default=timezone.now)
class Meta: class Meta:
unique_together = ["materi", "user"] unique_together = ["materi", "user"]
\ No newline at end of file
class AdminNotification(models.Model):
materi = models.ForeignKey(Materi, on_delete=models.CASCADE)
...@@ -19,7 +19,7 @@ from pydrive.drive import GoogleDrive ...@@ -19,7 +19,7 @@ from pydrive.drive import GoogleDrive
from app.forms import SuntingProfilForm from app.forms import SuntingProfilForm
from app.models import (Category, Comment, DislikeComment, DownloadStatistics, from app.models import (Category, Comment, DislikeComment, DownloadStatistics,
Like, LikeComment, Materi, Rating, ReadLater, Like, LikeComment, Materi, Rating, ReadLater,
ViewStatistics) ViewStatistics, AdminNotification)
from app.utils.fileManagementUtil import (get_random_filename, from app.utils.fileManagementUtil import (get_random_filename,
remove_image_exifdata) remove_image_exifdata)
...@@ -353,6 +353,7 @@ class UploadMateriService: ...@@ -353,6 +353,7 @@ class UploadMateriService:
@staticmethod @staticmethod
def upload_materi(form, materi): def upload_materi(form, materi):
materi.save() materi.save()
AdminNotification.objects.create(materi=materi)
kateg = form.cleaned_data["categories"] kateg = form.cleaned_data["categories"]
for i in kateg: for i in kateg:
materi.categories.add(i) materi.categories.add(i)
...@@ -401,6 +402,7 @@ class UploadMateriService: ...@@ -401,6 +402,7 @@ class UploadMateriService:
os.path.join(settings.MEDIA_ROOT,default_cover)) os.path.join(settings.MEDIA_ROOT,default_cover))
materi.save() materi.save()
AdminNotification.objects.create(materi=materi)
for c in excel["Categories"][i].split(","): for c in excel["Categories"][i].split(","):
materi.categories.add(categories.get(name=c)) materi.categories.add(categories.get(name=c))
...@@ -426,6 +428,7 @@ class RevisiMateriService: ...@@ -426,6 +428,7 @@ class RevisiMateriService:
materi.uploader = request.user materi.uploader = request.user
materi.status = "REVISION" materi.status = "REVISION"
materi.save() materi.save()
AdminNotification.objects.create(materi=materi)
kateg = form.cleaned_data["categories"] kateg = form.cleaned_data["categories"]
for i in kateg: for i in kateg:
materi.categories.add(i) materi.categories.add(i)
......
...@@ -47,6 +47,23 @@ ...@@ -47,6 +47,23 @@
<span> <span>
<a class="btn btn-primary main-content" href="/administration/">Halaman Administrasi</a> <a class="btn btn-primary main-content" href="/administration/">Halaman Administrasi</a>
</span> </span>
<div class="profile-margin"></div>
{% if notifications%}
<h3>Materi Baru</h3>
{% endif %}
<table class="table main-content">
<tbody>
{% for i in notifications %}
<tr>
<td>
<a href="/administration/detail-verif/{{i.materi.id}}">
Materi {{i.materi.title}} butuh verifikasi
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div> </div>
</div> </div>
</div> </div>
......
...@@ -45,7 +45,7 @@ from app.views import UploadMateriHTML, add_rating_materi ...@@ -45,7 +45,7 @@ from app.views import UploadMateriHTML, add_rating_materi
from .models import (Category, Comment, DislikeComment, DownloadStatistics, from .models import (Category, Comment, DislikeComment, DownloadStatistics,
Like, LikeComment, Materi, Rating, RatingContributor, Like, LikeComment, Materi, Rating, RatingContributor,
ReadLater, ReqMaterial, Review, ViewStatistics) ReadLater, ReqMaterial, Review, ViewStatistics, AdminNotification)
from .services import DetailMateriService from .services import DetailMateriService
from .views import (DaftarKatalog, DashboardKontributorView, DetailMateri, from .views import (DaftarKatalog, DashboardKontributorView, DetailMateri,
KatalogPerKontributorView, MateriFavorite, KatalogPerKontributorView, MateriFavorite,
...@@ -3947,7 +3947,6 @@ class MateriStatsTest(TestCase): ...@@ -3947,7 +3947,6 @@ class MateriStatsTest(TestCase):
jobj = json.loads(response.content) jobj = json.loads(response.content)
self.assertEqual(len(jobj['labels']), 6) self.assertEqual(len(jobj['labels']), 6)
class UploadMateriTest(TestCase): class UploadMateriTest(TestCase):
def setUp(self): def setUp(self):
self.client = Client() self.client = Client()
...@@ -3985,4 +3984,52 @@ class UploadMateriTest(TestCase): ...@@ -3985,4 +3984,52 @@ class UploadMateriTest(TestCase):
self.client.post("/unggah/", data=data) self.client.post("/unggah/", data=data)
self.assertEqual(Materi.objects.count(), 1) self.assertEqual(Materi.objects.count(), 1)
\ No newline at end of file
class AdminNotificationTest(TestCase):
def setUp(self):
self.client = Client()
self.contributor_credential = {
"email": "kontributor@gov.id",
"password": id_generator()
}
self.admin_credential = {
"email": "admin@gov.id",
"password": id_generator()
}
self.public_credential = {
"email": "public@gov.id",
"password": id_generator()
}
self.kontributor = User.objects.create_contributor(**self.contributor_credential)
self.admin = User.objects.create_admin(**self.admin_credential)
self.public = User.objects.create_user(**self.public_credential)
self.setUpImage()
self.content = SimpleUploadedFile("ExampleFile221.pdf", b"Test file")
self.category = Category.objects.create(id="1", name="medis", description="kategori medis")
@override_settings(MEDIA_ROOT=tempfile.gettempdir())
def setUpImage(self):
self.cover = InMemoryUploadedFile(
BytesIO(base64.b64decode(TEST_IMAGE)),
field_name='tempfile',
name='tempfile.png',
content_type='image/png',
size=len(TEST_IMAGE),
charset='utf-8',
)
def test_notification_admin(self):
self.client.login(**self.contributor_credential)
self.client.post(
"/unggah/", data={"title":"Materi 1", "author":"Agas", "publisher":"Kelas SC", "release_year":"2000",
"descriptions":"Deskripsi Materi 1", 'categories':"1",
"cover":self.cover, "content":self.content,
"yt_video_id":"jNwz4L9MGVY"}
)
materi = Materi.objects.get(title="Materi 1")
self.assertTrue(AdminNotification.objects.get(materi=materi.id))
...@@ -34,7 +34,8 @@ from app.models import ( ...@@ -34,7 +34,8 @@ from app.models import (
ReqMaterial, ReqMaterial,
Rating, RatingContributor, Rating, RatingContributor,
SubmitVisitor, SubmitVisitor,
ReadLater ReadLater,
AdminNotification
) )
from authentication.models import User from authentication.models import User
from .services import DafterKatalogService, DetailMateriService, LikeDislikeService, MateriFieldValidationHelperService, \ from .services import DafterKatalogService, DetailMateriService, LikeDislikeService, MateriFieldValidationHelperService, \
...@@ -593,6 +594,10 @@ class SuksesLoginKontributorView(TemplateView): ...@@ -593,6 +594,10 @@ class SuksesLoginKontributorView(TemplateView):
class SuksesLoginAdminView(TemplateView): class SuksesLoginAdminView(TemplateView):
template_name = "sukses_admin.html" template_name = "sukses_admin.html"
def getAdminNotification(self):
notifications = AdminNotification.objects.all()
return notifications
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
if not request.user.is_admin: if not request.user.is_admin:
raise PermissionDenied(request) raise PermissionDenied(request)
...@@ -608,8 +613,10 @@ class SuksesLoginAdminView(TemplateView): ...@@ -608,8 +613,10 @@ class SuksesLoginAdminView(TemplateView):
current_user = self.request.user current_user = self.request.user
context["user"] = current_user context["user"] = current_user
return self.render_to_response(context) notifications = self.getAdminNotification()
context["notifications"] = notifications
return self.render_to_response(context)
class PostsView(TemplateView): class PostsView(TemplateView):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment