Fakultas Ilmu Komputer UI

Commit ceacaca2 authored by Alfian Fuadi Rafli's avatar Alfian Fuadi Rafli
Browse files

[#51] Material: Search/Query By Title

parent 7989fff1
# 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 import random
from django.contrib.postgres import search
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator, MaxValueValidator from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models from django.db import models
...@@ -34,35 +35,59 @@ class Category(models.Model): ...@@ -34,35 +35,59 @@ class Category(models.Model):
return self.name 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): class Materi(models.Model):
cover = models.ImageField() cover = models.ImageField()
content = models.FileField() content = models.FileField()
title = models.CharField(max_length=50, default='Judul') title = models.CharField(max_length=50, default="Judul")
author = models.CharField(max_length=30, default='Penyusun') author = models.CharField(max_length=30, default="Penyusun")
uploader = models.ForeignKey(User, on_delete=models.SET_NULL, null=True) uploader = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
publisher = models.CharField(max_length=30, default="Penerbit") publisher = models.CharField(max_length=30, default="Penerbit")
pages = models.IntegerField(default=0) pages = models.IntegerField(default=0)
descriptions = models.TextField(default="Deskripsi") descriptions = models.TextField(default="Deskripsi")
status = models.CharField( status = models.CharField(max_length=30, choices=VERIFICATION_STATUS, default=VERIFICATION_STATUS[0][0])
max_length=30, choices=VERIFICATION_STATUS, default=VERIFICATION_STATUS[0][0])
categories = models.ManyToManyField(Category) categories = models.ManyToManyField(Category)
date_created = models.DateTimeField(default=timezone.now) date_created = models.DateTimeField(default=timezone.now)
date_modified = models.DateTimeField(auto_now=True) 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 @property
def is_published(self): def is_published(self):
published = False published = False
if self.verificationreport_set.exists(): if self.verificationreport_set.exists():
report = self.verificationreport_set.latest('timestamp') report = self.verificationreport_set.latest("timestamp")
published = True if report.status == 'Diterima' else False published = True if report.status == "Diterima" else False
return published return published
@property @property
def published_date(self): def published_date(self):
published_date = None published_date = None
if self.verificationreport_set.exists(): if self.verificationreport_set.exists():
report = self.verificationreport_set.latest('timestamp') report = self.verificationreport_set.latest("timestamp")
if report.status == 'Diterima': if report.status == "Diterima":
published_date = report.timestamp published_date = report.timestamp
return published_date return published_date
...@@ -71,13 +96,13 @@ class Materi(models.Model): ...@@ -71,13 +96,13 @@ class Materi(models.Model):
count = Like.objects.filter(materi=self).count() count = Like.objects.filter(materi=self).count()
return count return count
class Comment(models.Model): class Comment(models.Model):
username = models.CharField(max_length=100) username = models.CharField(max_length=100)
profile = models.CharField(max_length=100, default=getRandomColor) profile = models.CharField(max_length=100, default=getRandomColor)
comment = models.CharField(max_length=240, default="comments") comment = models.CharField(max_length=240, default="comments")
materi = models.ForeignKey(Materi, models.SET_NULL, null=True) materi = models.ForeignKey(Materi, models.SET_NULL, null=True)
user = models.ForeignKey( user = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True)
User, on_delete=models.SET_NULL, blank=True, null=True)
timestamp = models.DateTimeField(default=timezone.now) timestamp = models.DateTimeField(default=timezone.now)
def __str__(self): def __str__(self):
...@@ -89,22 +114,20 @@ class Like(models.Model): ...@@ -89,22 +114,20 @@ class Like(models.Model):
timestamp = models.DateTimeField(default=timezone.now) timestamp = models.DateTimeField(default=timezone.now)
session_id = models.CharField(max_length=32, blank=False) session_id = models.CharField(max_length=32, blank=False)
class ReqMaterial(models.Model): class ReqMaterial(models.Model):
title = models.CharField(max_length=100) title = models.CharField(max_length=100)
timestamp = models.DateTimeField(default=timezone.now) timestamp = models.DateTimeField(default=timezone.now)
class ViewStatistics(models.Model): class ViewStatistics(models.Model):
materi = models.ForeignKey( materi = models.ForeignKey(Materi, models.SET_NULL, null=True, related_name="baca")
Materi, models.SET_NULL, null=True, related_name="baca")
timestamp = models.DateTimeField(default=timezone.now) timestamp = models.DateTimeField(default=timezone.now)
class DownloadStatistics(models.Model): class DownloadStatistics(models.Model):
materi = models.ForeignKey( materi = models.ForeignKey(Materi, models.SET_NULL, null=True, related_name="unduh")
Materi, models.SET_NULL, null=True, related_name="unduh") downloader = models.ForeignKey(User, models.SET_NULL, blank=True, null=True, related_name="riwayat_unduh")
downloader = models.ForeignKey(
User, models.SET_NULL, blank=True, null=True, related_name="riwayat_unduh")
timestamp = models.DateTimeField(default=timezone.now) timestamp = models.DateTimeField(default=timezone.now)
...@@ -154,6 +177,7 @@ class RatingContributor(models.Model): ...@@ -154,6 +177,7 @@ class RatingContributor(models.Model):
else: else:
raise ValidationError("Rating score must be integer between 1-5") raise ValidationError("Rating score must be integer between 1-5")
class LaporanMateri(models.Model): class LaporanMateri(models.Model):
materi = models.ForeignKey(Materi, on_delete=models.CASCADE, max_length=120) materi = models.ForeignKey(Materi, on_delete=models.CASCADE, max_length=120)
user = models.ForeignKey(User, on_delete=models.CASCADE) user = models.ForeignKey(User, on_delete=models.CASCADE)
......
This diff is collapsed.
This diff is collapsed.
...@@ -12,5 +12,6 @@ exclude = ''' ...@@ -12,5 +12,6 @@ exclude = '''
| buck-out | buck-out
| build | build
| dist | dist
| migrations
)/ )/
''' '''
\ No newline at end of file
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