Fakultas Ilmu Komputer UI

Commit 72640851 authored by Muhamad Lutfi Arif's avatar Muhamad Lutfi Arif
Browse files

[105] Request: Material

parent 4e7f5921
......@@ -108,7 +108,7 @@ $('#btn_req_submit').click(function () {
$.ajax({
type: 'POST',
url: "{% url 'post-req-materi' %}",
url: "{% url 'req-materi' %}",
data: {
'title': title.value,
},
......
......@@ -15,12 +15,11 @@ from administration.utils import id_generator
from app.forms import SuntingProfilForm
from app.views import UploadMateriView, add_rating_materi
from authentication.models import User
from .models import Category, Comment, Materi, Like, Rating
from .models import Category, Comment, Materi, Like, Rating, ReqMaterial
from .views import (DaftarKatalog, DashboardKontributorView, DetailMateri,
ProfilKontributorView, SuksesLoginAdminView,
SuksesLoginKontributorView, SuntingProfilView,
ProfilAdminView, PostsView, SuntingProfilAdminView,
RevisiMateriView, KatalogPerKontributorView)
ProfilAdminView, PostsView, SuntingProfilAdminView, RevisiMateriView, ReqMateriView, KatalogPerKontributorView)
from app.forms import SuntingProfilForm
from app.utils.fileManagementUtil import get_random_filename, remove_image_exifdata
......@@ -52,12 +51,12 @@ class DaftarKatalogTest(TestCase):
class DaftarKatalogPerKontributorTest(TestCase):
def setUp(self):
self.client = Client()
self.contributor_credential = {
"email": "kontributor@gov.id",
"password": "passwordtest"
}
self.contributor_credential_2 = {
"email": "kontributor2@gov.id",
"password": "passwordtest"
......@@ -67,7 +66,7 @@ class DaftarKatalogPerKontributorTest(TestCase):
**self.contributor_credential, name="Kontributor 1", is_contributor=True)
self.contributor2 = get_user_model().objects.create_user(
**self.contributor_credential_2, name="Kontributor 2", is_contributor=True)
self.cover = SimpleUploadedFile(
"Cherprang_Areekul40_nJM9dGt.jpg", b"Test file")
self.content = SimpleUploadedFile("Bahan_PA_RKK.pdf", b"Test file")
......@@ -81,16 +80,16 @@ class DaftarKatalogPerKontributorTest(TestCase):
Materi(title="Materi 3", author="Agas", uploader=self.contributor2,
publisher="Kelas SC", descriptions="Deskripsi Materi 3",
status="APPROVE", cover=self.cover, content=self.content).save()
self.url = f"/profil/{self.contributor.email}/"
def test_katalog_per_kontributor_url_exist(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
def test_katalog_per_kontributor_using_katalog_kontri_template(self):
response = self.client.get(self.url)
self.assertTemplateUsed(response, "app/katalog_kontri.html")
self.assertTemplateUsed(response, "app/katalog_kontri.html")
def test_katalog_per_kontributor_using_katalog_per_kontributor_func(self):
found = resolve(self.url)
......@@ -98,10 +97,10 @@ class DaftarKatalogPerKontributorTest(TestCase):
def test_katalog_per_kontributor_show_daftar_materi_kontributor(self):
response = self.client.get(self.url)
list_materi = Materi.objects.filter(uploader=self.contributor)
list_materi = Materi.objects.filter(uploader=self.contributor)
data = response.context_data['materi_list']
self.assertEqual(len(list_materi), len(data))
self.assertEqual(len(list_materi), len(data))
class DetailMateriTest(TestCase):
......@@ -1319,3 +1318,74 @@ class fileManagementUtilTest(TestCase):
self.assertTrue(len(sanitized_img) < len(self.file_content))
self.assertTrue(b'<exif:' not in sanitized_img)
class RequestMateriTest(TestCase):
def setUp(self):
self.client = Client()
self.admin_credential = {
"email": "admin@gov.id",
"password": "passwordtest"
}
self.contributor_credential = {
"email": "kontributor@gov.id",
"password": "passwordtest"
}
self.anonymous_credential = {
"email": "anonymous@gov.id",
"password": "passwordtest"
}
self.admin = get_user_model().objects.create_user(
**self.admin_credential, name="Admin", is_admin=True)
self.contributor = get_user_model().objects.create_user(
**self.contributor_credential, name="Kontributor", is_contributor=True)
self.anonymous = get_user_model().objects.create_user(
**self.anonymous_credential, name="Anonymous"
)
self.url = "/req-materi/"
self.template_name = "req_materi.html"
def test_req_materi_url_resolves_to_get_req_materi_view(self):
found = resolve(self.url)
self.assertEqual(found.func.__name__, ReqMateriView.as_view().__name__)
def test_uses_req_material_template(self):
self.client.login(**self.contributor_credential)
response = self.client.get(self.url)
self.assertTemplateUsed(response, self.template_name)
self.client.logout()
def test_redirect_to_login_page_is_not_authenticated(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], '/login/')
def test_saving_and_retrieving_material_requests(self):
first_material_request = ReqMaterial(title="Material 1").save()
second_material_request = ReqMaterial(title="Material 2").save()
saved_material_request = ReqMaterial.objects.all()
self.assertEqual(saved_material_request.count(), 2)
def test_can_save_a_POST_request_and_return_correct_response_message(self):
self.client.login(**self.contributor_credential)
response = self.client.post(self.url,
data={
'title': 'Requested Material'
})
self.assertEqual(ReqMaterial.objects.count(), 1)
new_material_request = ReqMaterial.objects.first()
self.assertEqual(new_material_request.title, 'Requested Material')
self.assertIn('Permintaan materi berhasil dikirimkan', response.content.decode())
self.client.logout()
def test_given_no_title_should_not_save_request_and_return_correct_response_message(self):
self.client.login(**self.contributor_credential)
response = self.client.post(self.url)
self.assertEqual(ReqMaterial.objects.count(), 0)
self.assertIn('Missing parameter', response.content.decode())
self.client.logout()
......@@ -28,7 +28,6 @@ urlpatterns = [
path("posts/", PostsView.as_view(), name='posts'),
path("sunting-admin/", SuntingProfilAdminView.as_view(), name="sunting-admin"),
path("req-materi/", ReqMateriView.as_view(), name="req-materi"),
path("post-req-materi/", views.post_req_materi, name="post-req-materi"),
path("profil/<str:email>/", KatalogPerKontributorView.as_view(),
name="katalog-per-kontributor"),
path("materi/rate/", views.add_rating_materi, name="rate-materi"),
......
......@@ -486,9 +486,6 @@ class ReqMateriView(TemplateView):
def dispatch(self, request, *args, **kwargs):
if request.user.is_authenticated == False:
return HttpResponseRedirect("/login/")
#raise PermissionDenied(request)
# else if not request.user.is_admin:
# raise PermissionDenied(request)
return super(ReqMateriView, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
......@@ -498,39 +495,14 @@ class ReqMateriView(TemplateView):
def get(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)
current_user = self.request.user
context["user"] = current_user
context["form"] = SuntingProfilForm(instance=current_user)
return self.render_to_response(context)
def post(self, request, *args, **kwargs):
if request.user.is_authenticated == False:
raise PermissionDenied(request)
current_user = self.request.user
form = SuntingProfilForm(
request.POST, request.FILES, instance=current_user)
if form.is_valid():
current_user.default_profile_picture = True
form.save()
return HttpResponseRedirect("/profil-admin/")
else:
context = self.get_context_data(**kwargs)
context["form"] = form
return self.render_to_response(context)
def post_req_materi(request):
if request.method == 'POST':
#return JsonResponse({"success": True, "msg": "Permintaan materi berhasil dikirimkan"})
title = request.POST.get('title', None)
if title is None:
return JsonResponse({"success": False, "msg": "Missing parameter"})
ReqMaterial(title=title).save()
return JsonResponse({"success": True, "msg": "Permintaan materi berhasil dikirimkan"})
else:
return JsonResponse({"success": False, "msg": "Unsuported method"})
class SuksesLoginKontributorView(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