Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add content model #16

Merged
merged 10 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__pycache__/
*.py[cod]
*$py.class

assets/
# C extensions
*.so

Expand Down
Empty file added common/__init__.py
Empty file.
62 changes: 62 additions & 0 deletions common/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from django.contrib import admin
from reversion.admin import VersionAdmin as OgVersionAdmin

from .models import UserResource


class VersionAdmin(OgVersionAdmin):
history_latest_first = True


class UserResourceAdmin(admin.ModelAdmin):
def get_readonly_fields(self, *args, **kwargs):
readonly_fields = super().get_readonly_fields(*args, **kwargs) # type: ignore[reportAttributeAccessIssue]
return [
# To maintain order
*dict.fromkeys(
[
*readonly_fields,
"created_at",
"created_by",
"modified_at",
"modified_by",
]
)
]

def save_model(self, request, obj, form, change):
if not change:
obj.created_by = request.user
obj.modified_by = request.user
return super().save_model(request, obj, form, change) # type: ignore[reportAttributeAccessIssue]

def save_formset(self, request, form, formset, change):
if not issubclass(formset.model, UserResource):
return super().save_formset(request, form, formset, change)
# https://docs.djangoproject.com/en/4.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_formset
instances = formset.save(commit=False)
for obj in formset.deleted_objects:
obj.delete()
for instance in instances:
# UserResource changes
if instance.pk is None:
instance.created_by = request.user
instance.modified_by = request.user
instance.save()


class UserResourceTabularInline(admin.TabularInline):
def get_readonly_fields(self, *args, **kwargs):
readonly_fields = super().get_readonly_fields(*args, **kwargs) # type: ignore[reportAttributeAccessIssue]
return [
# To maintain order
*dict.fromkeys(
[
*readonly_fields,
"created_at",
"created_by",
"modified_at",
"modified_by",
]
)
]
6 changes: 6 additions & 0 deletions common/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class CommonConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "common"
Empty file added common/migrations/__init__.py
Empty file.
28 changes: 28 additions & 0 deletions common/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from django.db import models

from user.models import User


class UserResource(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(
User,
related_name="%(class)s_created",
on_delete=models.PROTECT,
)
modified_by = models.ForeignKey(
User,
related_name="%(class)s_modified",
on_delete=models.PROTECT,
)

# Typing
id: int
pk: int
created_by_id: int
modified_by_id: int

class Meta:
abstract = True
ordering = ["-id"]
1 change: 1 addition & 0 deletions common/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your tests here.
1 change: 1 addition & 0 deletions common/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your views here.
Empty file added content/__init__.py
Empty file.
18 changes: 18 additions & 0 deletions content/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.contrib import admin

from common.admin import UserResourceAdmin
from content.models import Content, Tag

# Register your models here.


@admin.register(Tag)
class TagAdmin(admin.ModelAdmin):
list_display = ["name"]
search_fields = ["name"]


@admin.register(Content)
class ContentAdmin(UserResourceAdmin):
list_display = ["title", "content_id"]
thenav56 marked this conversation as resolved.
Show resolved Hide resolved
autocomplete_fields = ["deleted_by", "tag"]
6 changes: 6 additions & 0 deletions content/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class ContentConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "content"
82 changes: 82 additions & 0 deletions content/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Generated by Django 5.1.1 on 2024-09-18 05:10

import uuid

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="Tag",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("name", models.CharField(max_length=20)),
("description", models.CharField(blank=True, max_length=50, null=True)),
],
),
migrations.CreateModel(
name="Content",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("created_at", models.DateTimeField(auto_now_add=True)),
("modified_at", models.DateTimeField(auto_now=True)),
("title", models.CharField(max_length=100)),
("document_type", models.IntegerField(choices=[(1, "Word"), (2, "PDF"), (3, "Text")], default=1)),
("document_file", models.FileField(upload_to="documents")),
("extracted_file", models.FileField(blank=True, null=True, upload_to="documents-extracts")),
("content_id", models.UUIDField(default=uuid.uuid4, editable=False)),
(
"document_status",
models.PositiveSmallIntegerField(
choices=[
(1, "Pending"),
(2, "Text extracted"),
(3, "Added to vector"),
(4, "Deleted from vector"),
(5, "Failure"),
],
default=1,
),
),
("is_deleted", models.BooleanField(default=False)),
("deleted_at", models.DateTimeField(blank=True, null=True)),
(
"created_by",
models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
related_name="%(class)s_created",
to=settings.AUTH_USER_MODEL,
),
),
(
"deleted_by",
models.ForeignKey(
blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL
),
),
(
"modified_by",
models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
related_name="%(class)s_modified",
to=settings.AUTH_USER_MODEL,
),
),
("tag", models.ManyToManyField(blank=True, to="content.tag")),
],
options={
"ordering": ["-id"],
"abstract": False,
},
),
]
18 changes: 18 additions & 0 deletions content/migrations/0002_alter_content_document_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.1.1 on 2024-09-18 05:28

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("content", "0001_initial"),
]

operations = [
migrations.AlterField(
model_name="content",
name="document_type",
field=models.IntegerField(choices=[(1, "Word"), (2, "PDF"), (3, "Text")], default=3),
),
]
Empty file added content/migrations/__init__.py
Empty file.
49 changes: 49 additions & 0 deletions content/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import uuid

from django.db import models
from django.utils.translation import gettext_lazy as _

from common.models import UserResource


class Tag(models.Model):
name = models.CharField(max_length=20)
description = models.CharField(max_length=50, null=True, blank=True)

def __str__(self):
return self.name


class Content(UserResource):
class DocumentType(models.IntegerChoices):
WORD = 1, _("Word")
PDF = 2, _("PDF")
thenav56 marked this conversation as resolved.
Show resolved Hide resolved
TEXT = 3, _("Text")

class DocumentStatus(models.IntegerChoices):
PENDING = 1, _("Pending")
TEXT_EXTRACTED = 2, _("Text extracted")
ADDED_TO_VECTOR = 3, _("Added to vector")
DELETED_FROM_VECTOR = 4, _("Deleted from vector")
FAILURE = 5, _("Failure")

title = models.CharField(max_length=100)
document_type = models.IntegerField(choices=DocumentType.choices, default=DocumentType.TEXT)
document_file = models.FileField(upload_to="documents")
extracted_file = models.FileField(upload_to="documents-extracts", null=True, blank=True)
content_id = models.UUIDField(default=uuid.uuid4, editable=False)
document_status = models.PositiveSmallIntegerField(choices=DocumentStatus.choices, default=DocumentStatus.PENDING)
tag = models.ManyToManyField("Tag", blank=True)
is_deleted = models.BooleanField(default=False)
deleted_at = models.DateTimeField(null=True, blank=True)
deleted_by = models.ForeignKey("user.User", null=True, blank=True, on_delete=models.PROTECT)

def __str__(self):
return self.title

def save(self, *args, **kwargs):
"""Save the content to the database."""
if self.pk is None:
if self.document_type == self.DocumentType.TEXT:
self.extracted_file = self.document_file
super().save(*args, **kwargs)
1 change: 1 addition & 0 deletions content/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your tests here.
1 change: 1 addition & 0 deletions content/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your views here.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ services:
DJANGO_DEBUG: ${DJANGO_DEBUG:-True}
DJANGO_ALLOWED_HOST: ${DJANGO_ALLOWED_HOST:-localhost}
DJNAGO_SECRET_KEY: ${DJANGO_SECRET_KEY}
DATABASE_NAME: ${DATABASE_NAME:-chatbot}
DATABASE_NAME: ${DATABASE_NAME:-postgres}
DATABASE_USER: ${DATABASE_USER:-postgres}
DATABASE_PASSWORD: ${DATABASE_PASSWORD:-postgres}
DATABASE_PORT: ${DATABASE_PORT:-5432}
Expand Down
2 changes: 2 additions & 0 deletions main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@
"django.contrib.messages",
"django.contrib.staticfiles",
"user",
"common",
"content",
]

MIDDLEWARE = [
Expand Down
Loading
Loading