Fakultas Ilmu Komputer UI

Commit 658d59d5 authored by Alfian Fuadi Rafli's avatar Alfian Fuadi Rafli
Browse files

Merge branch '1706028650-51' into 'master'

[#51] Material: Search/Query By Title

See merge request !35
parents 7989fff1 ceacaca2
Pipeline #58129 canceled with stages
in 24 minutes and 1 second
# Generated by Django 3.1 on 2020-10-09 11:19
import django.contrib.postgres.search
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('app', '0018_merge_20201009_0700'),
]
operations = [
migrations.AddField(
model_name='materi',
name='_search_vector',
field=django.contrib.postgres.search.SearchVectorField(editable=False, null=True),
),
]
# Generated by Django 3.1 on 2020-10-09 13:39
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('app', '0019_materi__search_vector'),
('app', '0019_auto_20201009_1829'),
]
operations = [
]
import random
from django.contrib.postgres import search
from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models
......@@ -34,35 +35,59 @@ class Category(models.Model):
return self.name
class MateriManager(models.Manager):
def search(self, search_text):
search_vector = search.SearchVector("title", weight="A")
search_query = search.SearchQuery(search_text)
search_rank = search.SearchRank(search_vector, search_query)
search_result = (
self.get_queryset().filter(_search_vector=search_query).annotate(rank=search_rank).order_by("-rank")
)
return search_result
class Materi(models.Model):
cover = models.ImageField()
content = models.FileField()
title = models.CharField(max_length=50, default='Judul')
author = models.CharField(max_length=30, default='Penyusun')
title = models.CharField(max_length=50, default="Judul")
author = models.CharField(max_length=30, default="Penyusun")
uploader = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
publisher = models.CharField(max_length=30, default="Penerbit")
pages = models.IntegerField(default=0)
descriptions = models.TextField(default="Deskripsi")
status = models.CharField(
max_length=30, choices=VERIFICATION_STATUS, default=VERIFICATION_STATUS[0][0])
status = models.CharField(max_length=30, choices=VERIFICATION_STATUS, default=VERIFICATION_STATUS[0][0])
categories = models.ManyToManyField(Category)
date_created = models.DateTimeField(default=timezone.now)
date_modified = models.DateTimeField(auto_now=True)
_search_vector = search.SearchVectorField(null=True, editable=False)
objects = MateriManager()
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
if "update_fields" not in kwargs or "_search_vector" not in kwargs["update_fields"]:
self._search_vector = search.SearchVector("title", weight="A")
self.save(update_fields=["_search_vector"])
@property
def is_published(self):
published = False
if self.verificationreport_set.exists():
report = self.verificationreport_set.latest('timestamp')
published = True if report.status == 'Diterima' else False
report = self.verificationreport_set.latest("timestamp")
published = True if report.status == "Diterima" else False
return published
@property
def published_date(self):
published_date = None
if self.verificationreport_set.exists():
report = self.verificationreport_set.latest('timestamp')
if report.status == 'Diterima':
report = self.verificationreport_set.latest("timestamp")
if report.status == "Diterima":
published_date = report.timestamp
return published_date
......@@ -71,13 +96,13 @@ class Materi(models.Model):
count = Like.objects.filter(materi=self).count()
return count
class Comment(models.Model):
username = models.CharField(max_length=100)
profile = models.CharField(max_length=100, default=getRandomColor)
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)
user = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True)
timestamp = models.DateTimeField(default=timezone.now)
def __str__(self):
......@@ -89,22 +114,20 @@ class Like(models.Model):
timestamp = models.DateTimeField(default=timezone.now)
session_id = models.CharField(max_length=32, blank=False)
class ReqMaterial(models.Model):
title = models.CharField(max_length=100)
timestamp = models.DateTimeField(default=timezone.now)
class ViewStatistics(models.Model):
materi = models.ForeignKey(
Materi, models.SET_NULL, null=True, related_name="baca")
materi = models.ForeignKey(Materi, models.SET_NULL, null=True, related_name="baca")
timestamp = models.DateTimeField(default=timezone.now)
class DownloadStatistics(models.Model):
materi = models.ForeignKey(
Materi, models.SET_NULL, null=True, related_name="unduh")
downloader = models.ForeignKey(
User, models.SET_NULL, blank=True, null=True, related_name="riwayat_unduh")
materi = models.ForeignKey(Materi, models.SET_NULL, null=True, related_name="unduh")
downloader = models.ForeignKey(User, models.SET_NULL, blank=True, null=True, related_name="riwayat_unduh")
timestamp = models.DateTimeField(default=timezone.now)
......@@ -154,9 +177,10 @@ class RatingContributor(models.Model):
else:
raise ValidationError("Rating score must be integer between 1-5")
class LaporanMateri(models.Model):
materi = models.ForeignKey(Materi, on_delete=models.CASCADE, max_length=120)
user = models.ForeignKey(User, on_delete=models.CASCADE)
laporan = models.TextField(validators=[MinValueValidator(30), MaxValueValidator(120)], default="")
timestamp = models.DateTimeField(default=timezone.now)
is_rejected = models.BooleanField(default=False)
\ No newline at end of file
is_rejected = models.BooleanField(default=False)
This diff is collapsed.
This diff is collapsed.
......@@ -12,5 +12,6 @@ exclude = '''
| buck-out
| build
| dist
| migrations
)/
'''
\ No newline at end of file
Markdown is supported
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