Fakultas Ilmu Komputer UI

Commit c320f26c authored by Ahmad Fauzan Amirul Isnain's avatar Ahmad Fauzan Amirul Isnain
Browse files

Merge branch '1706979152-130' into 'master'

[#130] QA: Fix Code Smells Detected by Sonarqube (Part 2)

Closes #130

See merge request !94
parents 60a0b340 2c530f59
Pipeline #60095 passed with stages
in 20 minutes and 8 seconds
......@@ -8,7 +8,7 @@ from django.core.files.uploadedfile import SimpleUploadedFile
from django.utils import timezone
from app.models import Materi, Comment, Like, DownloadStatistics, ViewStatistics, DummyLike, DummyViewStatistics, DummyDownloadStatistics, DummyComment
from app.management.commands.utils import SECONDS_IN_DAY, get_time_before, get_random_datetime, generate_list_of_random_datetime, generate_random_string, getRandomColor, getLoremWithLength
from app.management.commands.utils import SECONDS_IN_DAY, get_time_before, get_random_datetime, generate_list_of_random_datetime, generate_random_string, get_random_color, get_lorem_with_length
class Command(BaseCommand):
......@@ -50,7 +50,7 @@ class Command(BaseCommand):
def _comment_materi(self, timestamp, materi):
item = Comment(materi=materi, timestamp=timestamp,
profile=getRandomColor(), comment=getLoremWithLength(240))
profile=get_random_color(), comment=get_lorem_with_length(240))
item.save()
DummyComment(item=item).save()
......@@ -64,16 +64,7 @@ class Command(BaseCommand):
s_date = materi_published_date[0]
s_date = s_date.replace(day=s_date.day+1, hour=0,
minute=0, second=0, microsecond=0)
# reports = []
while (s_date < today):
# report = {
# "s_date": s_date,
# "visit": 0,
# "view": 0,
# "download": 0,
# "like": 0,
# "comment": 0,
# }
today_materi = [i for i in materi if i.published_date < s_date]
visiting_user = options["baseline"]
visiting_user += int(options["coef_time"] * r_day) + \
......@@ -82,8 +73,6 @@ class Command(BaseCommand):
(1 + uniform(-options["coef_visit_range"],
options["coef_visit_range"])))
active_user = int(visiting_user * options["coef_read"])
# report["visit"] = visiting_user
# report["view"] = active_user
times = generate_list_of_random_datetime(
s_date, s_date+timedelta(days=1), active_user)
for timestamp in times:
......@@ -91,23 +80,9 @@ class Command(BaseCommand):
self._view_materi(timestamp, selected_materi)
if random() < options["coef_download"]:
self._download_materi(timestamp, selected_materi)
# report["download"] += 1
if random() < options["coef_like"]:
self._like_materi(timestamp, selected_materi)
# report["like"] += 1
if random() < options["coef_comment"]:
self._comment_materi(timestamp, selected_materi)
# report["comment"] += 1
s_date = s_date + timedelta(days=1)
r_day += 1
# reports.append(report)
# for i in range
# for i in reports:
# self.stdout.write(self.style.SUCCESS(f"Today is {i['s_date']}"))
# self.stdout.write(self.style.SUCCESS(f"User visit {i['visit']}"))
# self.stdout.write(self.style.SUCCESS(f"User view {i['view']}"))
# self.stdout.write(self.style.SUCCESS(
# f"User download {i['download']}"))
# self.stdout.write(self.style.SUCCESS(f"User like {i['like']}"))
# self.stdout.write(self.style.SUCCESS(
# f"User comment {i['comment']}"))
......@@ -8,7 +8,7 @@ from django.core.files.uploadedfile import SimpleUploadedFile
from django.utils import timezone, lorem_ipsum
from app.models import DummyLike, DummyViewStatistics, DummyDownloadStatistics, DummyComment
from app.management.commands.utils import SECONDS_IN_DAY, get_time_before, get_random_datetime, generate_list_of_random_datetime, generate_random_string, getRandomColor
from app.management.commands.utils import SECONDS_IN_DAY, get_time_before, get_random_datetime, generate_list_of_random_datetime, generate_random_string, get_random_color
class Command(BaseCommand):
......
......@@ -9,12 +9,12 @@ from django.utils import timezone, lorem_ipsum
SECONDS_IN_DAY = 86400
def getRandomColor():
def get_random_color():
color = "%06x" % randint(0, 0xFFFFFF)
return color
def getLoremWithLength(n):
def get_lorem_with_length(n):
while True:
s = lorem_ipsum.sentence()
if len(s) < n:
......@@ -43,7 +43,7 @@ def get_random_datetime(start_date, end_date, max_delta_seconds=None, min_delta_
def generate_list_of_random_datetime(start, end, n):
res = []
for i in range(n):
for _ in range(n):
res.append(get_random_datetime(start, end))
res.sort()
return res
......@@ -56,4 +56,4 @@ def get_last_year():
def generate_random_string(n):
return(''.join(choice(ascii_letters) for i in range(n)))
return(''.join(choice(ascii_letters) for _ in range(n)))
......@@ -14,6 +14,6 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='comment',
name='profile',
field=models.CharField(default=app.models.getRandomColor, max_length=100),
field=models.CharField(default=app.models.get_random_color, max_length=100),
),
]
......@@ -20,7 +20,7 @@ class Migration(migrations.Migration):
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('username', models.CharField(max_length=100)),
('profile', models.CharField(default=app.models.getRandomColor, max_length=100)),
('profile', models.CharField(default=app.models.get_random_color, max_length=100)),
('review', models.TextField(default='review')),
('timestamp', models.DateTimeField(default=django.utils.timezone.now)),
('materi', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='app.materi')),
......
......@@ -21,7 +21,7 @@ VERIFICATION_STATUS = [
# Create your models here.
def getRandomColor():
def get_random_color():
color = "%06x" % random.randint(0, 0xFFFFFF)
return color
......@@ -140,11 +140,12 @@ class Materi(SoftDeleteModel):
like = False
if Like.objects.filter(materi=self).exists():
like = True
return like
class Comment(models.Model):
username = models.CharField(max_length=100)
profile = models.CharField(max_length=100, default=getRandomColor)
profile = models.CharField(max_length=100, default=get_random_color)
comment = models.CharField(max_length=240, default="comments")
materi = models.ForeignKey(Materi, models.SET_NULL, null=True)
user = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True)
......@@ -165,7 +166,7 @@ class Comment(models.Model):
class Review(models.Model):
username = models.CharField(max_length=100)
profile = models.CharField(max_length=100, default=getRandomColor)
profile = models.CharField(max_length=100, default=get_random_color)
review = models.TextField(default="review")
materi = models.ForeignKey(Materi, models.SET_NULL, null=True)
user = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True)
......
......@@ -343,7 +343,6 @@ class UploadMateriService:
@staticmethod
def validate_yt_video_url(value):
is_yt_id_valid = True
r = requests.get('http://www.youtube.com/watch?v='+value)
if "\"playabilityStatus\":{\"status\":\"ERROR\"" in r.text:
raise ValidationError("Invalid Youtube video ID")
......
......@@ -55,13 +55,6 @@
<span>Sunting Profil</span></a>
</li>
<!--
<li class="nav-item">
<a class="nav-link" href="/sunting/">
<span>Sunting Profil</span></a>
</li>
-->
</ul>
<!-- End of Sidebar -->
......
......@@ -22,7 +22,8 @@ div.review {
{% block verification %} {% endblock verification %}
<div id="fb-root"></div>
<div class="container-fluid p-0 bg detail-materi-color">
<nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">
<span class="modal-title black-text" id="navbarLabel" style="display: none;">Navbar</span>
<nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow" aria-labelledby="navbarLabel">
<!-- Sidebar Toggle (Topbar) -->
<button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
......@@ -287,7 +288,8 @@ div.review {
</div>
</div>
<div class="row menu-wrapper mr-4 ml-4 p-3">
<nav class="navbar navbar-expand-sm border-top border-bottom p-0 mt-3 mb-3">
<span class="modal-title black-text" id="navbarLabelTwo" style="display: none;">Navbar</span>
<nav class="navbar navbar-expand-sm border-top border-bottom p-0 mt-3 mb-3" aria-labelledby="navbarLabelTwo">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#deskripsi">Deskripsi</a>
......
......@@ -160,7 +160,7 @@
{% for item in materi_list %}
<div class="card book">
<img src={{item.cover.url}} class="card-img-top" alt="cover"
style="height:200px; widows: 200px;; overflow: hidden;"></img>
style="height:200px; widows: 200px; overflow: hidden;"></img>
<div class="card-body">
<h5 class="card-title">{{item.title}}</h5>
<p class="card-text">{{item.author}}</p>
......
......@@ -12,6 +12,5 @@ def get_random_filename(f_name):
def remove_image_exifdata(f_path):
img = Image.open(f_path)
img.save(f_path)
return
......@@ -39,6 +39,13 @@ from .services import DafterKatalogService, DetailMateriService, LikeDislikeServ
DownloadViewMateriHelperService, UploadMateriService, EditProfileService, RevisiMateriService, \
DownloadHistoryService, GoogleDriveUploadService, ReadLaterService
MISSING_PARAMETER_MESSAGE = "Missing parameter"
UNSUPPORTED_MESSAGE = "Unsuported method"
FILE_NOT_FOUND_MESSAGE = "File tidak dapat ditemukan."
UNGGAH_HTML = "unggah.html"
UNGGAH_URL = "/unggah/"
UNGGAH_EXCEL_URL = "/unggah_excel/"
LOGIN_URL = "/login/"
def permission_denied(request, exception, template_name="error_403.html"):
return defaults.permission_denied(request, exception, template_name)
......@@ -55,13 +62,13 @@ class DaftarKatalog(TemplateView):
context = self.get_context_data(**kwargs)
context["kategori_list"] = Category.objects.all()
lstMateri = Materi.objects.filter(status="APPROVE").order_by("date_modified")
lst_materi = Materi.objects.filter(status="APPROVE").order_by("date_modified")
url = ""
lstMateri, url = DafterKatalogService.apply_options(lstMateri, request, url)
lst_materi, url = DafterKatalogService.apply_options(lst_materi, request, url)
context["materi_list"] = lstMateri
context["materi_list"] = lst_materi
paginator = Paginator(context["materi_list"], 15)
page_number = request.GET.get("page")
page_obj = paginator.get_page(page_number)
......@@ -226,10 +233,10 @@ def toggle_like(request):
materi_id = request.POST.get("materi_id", None)
session_id = request.POST.get("session_id", None)
if materi_id is None or session_id is None:
return JsonResponse({"success": False, "msg": "Missing parameter"})
return JsonResponse({"success": False, "msg": MISSING_PARAMETER_MESSAGE})
return JsonResponse(LikeDislikeService.apply_like_materi(materi_id, session_id))
else:
return JsonResponse({"success": False, "msg": "Unsuported method"})
return JsonResponse({"success": False, "msg": UNSUPPORTED_MESSAGE})
def delete_comment(request, pk_materi, pk_comment):
......@@ -246,10 +253,10 @@ def toggle_like_comment(request):
comment_id = request.POST.get("comment_id", None)
session_id = request.POST.get("session_id", None)
if comment_id is None or session_id is None:
return JsonResponse({"success": False, "msg": "Missing parameter", "comment_id": comment_id})
return JsonResponse({"success": False, "msg": MISSING_PARAMETER_MESSAGE, "comment_id": comment_id})
return JsonResponse(LikeDislikeService.apply_comment_like(comment_id, session_id))
else:
return JsonResponse({"success": False, "msg": "Unsuported method", "comment_id": comment_id})
return JsonResponse({"success": False, "msg": UNSUPPORTED_MESSAGE, "comment_id": comment_id})
def toggle_dislike_comment(request):
......@@ -258,10 +265,10 @@ def toggle_dislike_comment(request):
comment_id = request.POST.get("comment_id", None)
session_id = request.POST.get("session_id", None)
if comment_id is None or session_id is None:
return JsonResponse({"success": False, "msg": "Missing parameter", "comment_id": comment_id})
return JsonResponse({"success": False, "msg": MISSING_PARAMETER_MESSAGE, "comment_id": comment_id})
return JsonResponse(LikeDislikeService.apply_comment_dislike(comment_id, session_id))
else:
return JsonResponse({"success": False, "msg": "Unsuported method", "comment_id": comment_id})
return JsonResponse({"success": False, "msg": UNSUPPORTED_MESSAGE, "comment_id": comment_id})
def add_rating_materi(request):
......@@ -305,10 +312,10 @@ def download_materi(request, pk):
return DownloadViewMateriHelperService.build_materi_response(fh, file_path,
materi, mimetype, request,
HttpResponse(fh.read(), content_type=mimetype[0]))
except Exception as e:
raise Http404("File tidak dapat ditemukan.")
except Exception:
raise Http404(FILE_NOT_FOUND_MESSAGE)
else:
raise Http404("File tidak dapat ditemukan.")
raise Http404(FILE_NOT_FOUND_MESSAGE)
def view_materi(request, pk):
......@@ -322,10 +329,10 @@ def view_materi(request, pk):
response = HttpResponse(fh.read(), content_type=mimetype[0])
DownloadViewMateriHelperService.build_view_materi_response(file_path, materi, response)
return response
except Exception as e:
raise Http404("File tidak dapat ditemukan.")
except Exception:
raise Http404(FILE_NOT_FOUND_MESSAGE)
else:
raise Http404("File tidak dapat ditemukan.")
raise Http404(FILE_NOT_FOUND_MESSAGE)
def delete_materi(request, pk):
......@@ -337,7 +344,7 @@ def delete_materi(request, pk):
return HttpResponseRedirect("/dashboard/")
class UploadMateriView(TemplateView):
template_name = "unggah.html"
template_name = UNGGAH_HTML
context = {}
def get_context_data(self, **kwargs):
......@@ -354,10 +361,10 @@ class UploadMateriView(TemplateView):
konten = form.cleaned_data["content"]
yt_url_id = form.cleaned_data['yt_video_id']
if not UploadMateriService.validate_file_extension(konten, request, yt_url_id):
return HttpResponseRedirect("/unggah/")
return HttpResponseRedirect(UNGGAH_URL)
UploadMateriService.upload_materi(form, materi)
messages.success(request, "Materi berhasil diunggah, periksa riwayat unggah anda")
return HttpResponseRedirect("/unggah/")
return HttpResponseRedirect(UNGGAH_URL)
else:
context = self.get_context_data(**kwargs)
context["form"] = form
......@@ -376,12 +383,12 @@ class UploadMateriView(TemplateView):
class UploadMateriHTML(TemplateView):
template_name = "unggah.html"
template_name = UNGGAH_HTML
context = {}
def get_template_names(self):
if self.request.path == "/unggah/":
template_name = "unggah.html"
if self.request.path == UNGGAH_URL:
template_name = UNGGAH_HTML
return template_name
......@@ -390,7 +397,7 @@ class UploadMateriExcelView(TemplateView):
context = {}
def get_template_names(self):
if self.request.path == "/unggah_excel/":
if self.request.path == UNGGAH_EXCEL_URL:
template_name = "unggah_excel.html"
return template_name
......@@ -446,7 +453,7 @@ class UploadMateriExcelView(TemplateView):
if message != None:
messages.error(request, message)
return HttpResponseRedirect("/unggah_excel/")
return HttpResponseRedirect(UNGGAH_EXCEL_URL)
# Second pass, save data
with django.db.transaction.atomic():
......@@ -454,7 +461,7 @@ class UploadMateriExcelView(TemplateView):
messages.success(request, "Materi berhasil diunggah")
return HttpResponseRedirect("/unggah_excel/")
return HttpResponseRedirect(UNGGAH_EXCEL_URL)
......@@ -536,7 +543,7 @@ class ReqMateriView(TemplateView):
def dispatch(self, request, *args, **kwargs):
if request.user.is_authenticated == False:
return HttpResponseRedirect("/login/")
return HttpResponseRedirect(LOGIN_URL)
return super(ReqMateriView, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
......@@ -551,7 +558,7 @@ class ReqMateriView(TemplateView):
def post(self, request, *args, **kwargs):
title = request.POST.get("title", None)
if title is None:
return JsonResponse({"success": False, "msg": "Missing parameter"})
return JsonResponse({"success": False, "msg": MISSING_PARAMETER_MESSAGE})
ReqMaterial(title=title).save()
return JsonResponse({"success": True, "msg": "Permintaan materi berhasil dikirimkan"})
......@@ -669,7 +676,7 @@ def pages(request):
template = loader.get_template(load_template)
return HttpResponse(template.render(context, request))
except Exception as e:
except Exception:
template = loader.get_template("error-404.html")
return HttpResponse(template.render(context, request))
......@@ -699,7 +706,7 @@ def save_to_gdrive(request, pk):
if os.path.exists(file_path):
GoogleDriveUploadService.upload_to_gdrive(file_path, materi.title)
else:
raise Http404("File tidak dapat ditemukan.")
raise Http404(FILE_NOT_FOUND_MESSAGE)
return HttpResponseRedirect(reverse('detail-materi', kwargs={'pk': pk}))
......@@ -768,7 +775,7 @@ class SubmitVisitorView(TemplateView):
def dispatch(self, request, *args, **kwargs):
if request.user.is_authenticated == False:
return HttpResponseRedirect("/login/")
return HttpResponseRedirect(LOGIN_URL)
return super(SubmitVisitorView, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
......@@ -784,7 +791,7 @@ class SubmitVisitorView(TemplateView):
email = request.POST.get("email", None)
user_id = request.POST.get("user_id", None)
if title is None:
return JsonResponse({"success": False, "msg": "Missing parameter"})
return JsonResponse({"success": False, "msg": MISSING_PARAMETER_MESSAGE})
SubmitVisitor(msg=title, user_id=user_id, email=email).save()
return JsonResponse({"success": True, "msg": "Buku tamu berhasil ditambahkan"})
......@@ -810,18 +817,18 @@ def toggle_readlater(request):
if request.method == "POST":
materi_id = request.POST.get("materi_id", None)
if materi_id is None:
return JsonResponse({"success": False, "msg": "Missing parameter"})
return JsonResponse({"success": False, "msg": MISSING_PARAMETER_MESSAGE})
return JsonResponse(ReadLaterService.toggle_read_later(materi_id, request.user))
else:
return JsonResponse({"success": False, "msg": "Unsuported method"})
return JsonResponse({"success": False, "msg": UNSUPPORTED_MESSAGE})
class StatisticsView(TemplateView):
template_name = "statistik.html"
def dispatch(self, request, *args, **kwargs):
if request.user.is_authenticated == False:
return HttpResponseRedirect("/login/")
return HttpResponseRedirect(LOGIN_URL)
return super(StatisticsView, self).dispatch(request, *args, **kwargs)
def get_stat_json(self):
......
......@@ -6,15 +6,15 @@ services:
env_file:
- .env
ports:
- 8000:8000
- 8000:8000
depends_on:
- db
db:
image: postgres:12
volumes:
- postgres_data:/var/lib/postgresql/data/
- static_volume:/home/digipus/staticfiles
- media_volume:/home/digipus/mediafiles
- postgres_data:/var/lib/postgresql/data/
- static_volume:/home/digipus/staticfiles
- media_volume:/home/digipus/mediafiles
env_file:
- .env.db
......
#!/bin/bash
# Run autopep8
git ls-files | grep -v 'migrations' | grep -v 'settings.py' | grep -v 'manage.py' | grep -E '.py$' | xargs autopep8 --in-place --recursive
# Run pylint
......
......@@ -46,9 +46,9 @@ class UserForm(forms.ModelForm):
nomor_telpon = self.cleaned_data.get("nomor_telpon")
if not User.objects.filter(nomor_telpon=nomor_telpon).exists():
try:
no_telp = int(nomor_telpon)
int(nomor_telpon)
return nomor_telpon
except:
except ValueError:
raise forms.ValidationError("Hanya masukkan angka")
raise forms.ValidationError("Nomor telepon sudah digunakan untuk mendaftar akun")
......
......@@ -45,53 +45,45 @@
Registrasi Admin
</div>
<div class="wrap-input100 validate-input" data-validate="Valid email is required: ex@abc.xyz">
<!-- <input class="input100" type="text" name="nama"> -->
{{ form.name }}
<span class="focus-input100"></span>
<span class="label-input100">Nama</span>
</div>
<div class="wrap-input100 validate-input" data-validate="Password is required">
<!-- <input class="input100" type="text" name="pekerjaan"> -->
{{ form.instansi }}
<span class="focus-input100"></span>
<span class="label-input100">Instansi/Pekerjaan</span>
</div>
<div class="wrap-input100 validate-input" data-validate="Valid email is required: ex@abc.xyz">
<!-- <input class="input100" type="text" name="nama"> -->
{{ form.nik.errors }}
{{ form.nik }}
<span class="focus-input100"></span>
<span class="label-input100">NIK</span>
</div>
<div class="wrap-input100 validate-input" data-validate="Valid email is required: ex@abc.xyz">
<!-- <input class="input100" type="text" name="nama"> -->
{{ form.alamat }}
<span class="focus-input100"></span>
<span class="label-input100">Alamat</span>
</div>
<div class="wrap-input100 validate-input" data-validate="Valid email is required: ex@abc.xyz">
<!-- <input class="input100" type="text" name="nama"> -->
{{ form.email.errors }}
{{ form.email }}
<span class="focus-input100"></span>
<span class="label-input100">Email</span>
</div>
<div class="wrap-input100 validate-input" data-validate="Valid email is required: ex@abc.xyz">
<!-- <input class="input100" type="text" name="nama"> -->
{{ form.nomor_telpon.errors }}
{{ form.nomor_telpon }}
<span class="focus-input100"></span>
<span class="label-input100">Nomor Telepon</span>
</div>
<div class="wrap-input100 validate-input" data-validate="Valid email is required: ex@abc.xyz">
<!-- <input class="input100" type="text" name="nama"> -->
{{ form.password.errors }}
{{ form.password }}
<span class="focus-input100"></span>
<span class="label-input100">Kata Sandi</span>
</div>
<div class="wrap-input100 validate-input" data-validate="Valid email is required: ex@abc.xyz">
<!-- <input class="input100" type="text" name="nama"> -->
{{ form.password2 }}
<span class="focus-input100"></span>
<span class="label-input100">Ketik Ulang Kata Sandi</span>
......
......@@ -47,7 +47,6 @@
<span class="label-input100">Nama</span>
</div>
<div class="wrap-input100 validate-input" data-validate="Password is required">
<!-- <input class="input100" type="text" name="pekerjaan"> -->
{{ form.instansi }}
<span class="focus-input100"></span>
<span class="label-input100">Instansi/Pekerjaan</span>
......
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