refactor project structure
Run Tests / Run Tests (push) Failing after 31s Details

This commit is contained in:
James Ravenscroft 2024-12-20 09:47:00 +00:00
parent ee6d7c26a1
commit c82cb00013
46 changed files with 141 additions and 123 deletions

View File

@ -0,0 +1,3 @@
from .celery import app as celery_app
__all__ = ('celery_app',)

View File

@ -42,7 +42,7 @@ INSTALLED_APPS = [
"django.contrib.sessions", "django.contrib.sessions",
"django.contrib.messages", "django.contrib.messages",
"django.contrib.staticfiles", "django.contrib.staticfiles",
"webui", "penparse",
"markdown_deux", "markdown_deux",
"markdownify.apps.MarkdownifyConfig", "markdownify.apps.MarkdownifyConfig",
"rest_framework", "rest_framework",
@ -129,9 +129,9 @@ AUTH_PASSWORD_VALIDATORS = [
LOGIN_REDIRECT_URL = "/dashboard" LOGIN_REDIRECT_URL = "/dashboard"
AUTH_USER_MODEL = "webui.User" AUTH_USER_MODEL = "penparse.User"
AUTHENTICATION_BACKENDS = ["webui.auth.EmailBackend"] AUTHENTICATION_BACKENDS = ["penparse.auth.EmailBackend"]
# Internationalization # Internationalization
# https://docs.djangoproject.com/en/4.2/topics/i18n/ # https://docs.djangoproject.com/en/4.2/topics/i18n/
@ -156,7 +156,7 @@ STATIC_URL = "static/"
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
CELERY_BROKER_URL = "amqp://guest:guest@localhost/" CELERY_BROKER_URL = os.getenv("CELERY_BROKER_URL")
OPENAI_API_BASE = os.getenv("OPENAI_API_BASE") OPENAI_API_BASE = os.getenv("OPENAI_API_BASE")

View File

@ -37,7 +37,7 @@ INSTALLED_APPS = [
"django.contrib.sessions", "django.contrib.sessions",
"django.contrib.messages", "django.contrib.messages",
"django.contrib.staticfiles", "django.contrib.staticfiles",
"webui", "penparse",
"markdown_deux", "markdown_deux",
"markdownify.apps.MarkdownifyConfig", "markdownify.apps.MarkdownifyConfig",
"rest_framework", "rest_framework",
@ -115,9 +115,9 @@ AUTH_PASSWORD_VALIDATORS = [
LOGIN_REDIRECT_URL = "/dashboard" LOGIN_REDIRECT_URL = "/dashboard"
AUTH_USER_MODEL = "webui.User" AUTH_USER_MODEL = "penparse.User"
AUTHENTICATION_BACKENDS = ["webui.auth.EmailBackend"] AUTHENTICATION_BACKENDS = ["penparse.auth.EmailBackend"]
# Internationalization # Internationalization
# https://docs.djangoproject.com/en/4.2/topics/i18n/ # https://docs.djangoproject.com/en/4.2/topics/i18n/

26
penparse/config/urls.py Normal file
View File

@ -0,0 +1,26 @@
"""
URL configuration for penparse project.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.urls import include, path
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("", include("penparse.urls")),
path('admin/', admin.site.urls),
path("accounts/", include("django.contrib.auth.urls")),
]

View File

@ -6,7 +6,7 @@ import sys
def main(): def main():
"""Run administrative tasks.""" """Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'penparse.settings') os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
try: try:
from django.core.management import execute_from_command_line from django.core.management import execute_from_command_line
except ImportError as exc: except ImportError as exc:

View File

@ -1,3 +0,0 @@
from .celery import app as celery_app
__all__ = ('celery_app',)

View File

@ -1,6 +1,6 @@
from django.apps import AppConfig from django.apps import AppConfig
class WebuiConfig(AppConfig): class PenParseConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField' default_auto_field = 'django.db.models.BigAutoField'
name = 'webui' name = 'penparse'

View File

@ -0,0 +1,51 @@
# Generated by Django 4.2.16 on 2024-11-30 06:31
from django.db import migrations, models
import django.utils.timezone
import penparse.models
class Migration(migrations.Migration):
initial = True
dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
]
operations = [
migrations.CreateModel(
name='User',
fields=[
('id', models.BigAutoField(auto_created=True,
primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(
max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(
blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False,
help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('is_staff', models.BooleanField(default=False,
help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(
default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(
default=django.utils.timezone.now, verbose_name='date joined')),
('email', models.EmailField(max_length=254, unique=True)),
('first_name', models.CharField(max_length=150)),
('last_name', models.CharField(max_length=150)),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.',
related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.',
related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')),
],
options={
'verbose_name': 'user',
'verbose_name_plural': 'users',
'abstract': False,
},
managers=[
('objects', penparse.models.UserManager()),
],
),
]

View File

@ -9,19 +9,21 @@ import uuid
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
('webui', '0001_initial'), ('penparse', '0001_initial'),
] ]
operations = [ operations = [
migrations.CreateModel( migrations.CreateModel(
name='ImageMemo', name='ImageMemo',
fields=[ fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), ('id', models.UUIDField(default=uuid.uuid4,
editable=False, primary_key=True, serialize=False)),
('image', models.ImageField(upload_to='')), ('image', models.ImageField(upload_to='')),
('content', models.TextField()), ('content', models.TextField()),
('created_at', models.DateTimeField(auto_now_add=True)), ('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)), ('updated_at', models.DateTimeField(auto_now=True)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='memos', to=settings.AUTH_USER_MODEL)), ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE,
related_name='memos', to=settings.AUTH_USER_MODEL)),
], ],
options={ options={
'ordering': ['-created_at'], 'ordering': ['-created_at'],

View File

@ -6,7 +6,7 @@ from django.db import migrations, models
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
('webui', '0002_imagememo'), ('penparse', '0002_imagememo'),
] ]
operations = [ operations = [

View File

@ -6,13 +6,14 @@ from django.db import migrations, models
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
('webui', '0003_imagememo_image_mimetype_alter_imagememo_image'), ('penparse', '0003_imagememo_image_mimetype_alter_imagememo_image'),
] ]
operations = [ operations = [
migrations.AddField( migrations.AddField(
model_name='imagememo', model_name='imagememo',
name='status', name='status',
field=models.CharField(choices=[('pending', 'Pending'), ('processing', 'Processing'), ('done', 'Done'), ('error', 'Error')], default='pending', max_length=10), field=models.CharField(choices=[('pending', 'Pending'), ('processing', 'Processing'), (
'done', 'Done'), ('error', 'Error')], default='pending', max_length=10),
), ),
] ]

View File

@ -6,7 +6,7 @@ from django.db import migrations, models
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
('webui', '0004_imagememo_status'), ('penparse', '0004_imagememo_status'),
] ]
operations = [ operations = [

View File

@ -6,7 +6,7 @@ from django.db import migrations, models
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
('webui', '0005_imagememo_error_message'), ('penparse', '0005_imagememo_error_message'),
] ]
operations = [ operations = [

View File

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 27 KiB

View File

Before

Width:  |  Height:  |  Size: 751 KiB

After

Width:  |  Height:  |  Size: 751 KiB

View File

Before

Width:  |  Height:  |  Size: 145 KiB

After

Width:  |  Height:  |  Size: 145 KiB

View File

@ -13,7 +13,8 @@ class TestProcessMemo:
@pytest.fixture @pytest.fixture
def sample_image_memo(self, db): def sample_image_memo(self, db):
user1 = User.objects.create_user(email="user1@test.com", password="password1") user1 = User.objects.create_user(
email="user1@test.com", password="password1")
memo = ImageMemo.objects.create( memo = ImageMemo.objects.create(
author=user1, author=user1,
status=MemoStatus.Pending, status=MemoStatus.Pending,
@ -23,9 +24,10 @@ class TestProcessMemo:
return memo return memo
def test_process_memo_success(self, sample_image_memo): def test_process_memo_success(self, sample_image_memo):
with patch("webui.tasks.litellm") as mock_litellm: with patch("penparse.tasks.litellm") as mock_litellm:
mock_response = MagicMock() mock_response = MagicMock()
mock_response.choices[0].message = {"content": "Transcribed content"} mock_response.choices[0].message = {
"content": "Transcribed content"}
mock_litellm.completion.return_value = mock_response mock_litellm.completion.return_value = mock_response
process_memo(sample_image_memo.id) process_memo(sample_image_memo.id)
@ -45,10 +47,12 @@ class TestProcessMemo:
assert "Image file" in processed_memo.error_message assert "Image file" in processed_memo.error_message
def test_process_memo_api_error(self, sample_image_memo): def test_process_memo_api_error(self, sample_image_memo):
with patch("webui.tasks.litellm") as mock_litellm: with patch("penparse.tasks.litellm") as mock_litellm:
mock_response = MagicMock() mock_response = MagicMock()
mock_response.choices[0].message = {"content": "Transcribed content"} mock_response.choices[0].message = {
mock_litellm.completion.side_effect = litellm.APIError(400, "API Error", "openai", "any") "content": "Transcribed content"}
mock_litellm.completion.side_effect = litellm.APIError(
400, "API Error", "openai", "any")
process_memo(sample_image_memo.id) process_memo(sample_image_memo.id)
@ -58,11 +62,12 @@ class TestProcessMemo:
def test_process_memo_sets_model_name(self, sample_image_memo): def test_process_memo_sets_model_name(self, sample_image_memo):
with ( with (
patch("webui.tasks.litellm") as mock_litellm, patch("penparse.tasks.litellm") as mock_litellm,
patch("webui.tasks.settings") as mock_settings, patch("penparse.tasks.settings") as mock_settings,
): ):
mock_response = MagicMock() mock_response = MagicMock()
mock_response.choices[0].message = {"content": "Transcribed content"} mock_response.choices[0].message = {
"content": "Transcribed content"}
mock_litellm.completion.return_value = mock_response mock_litellm.completion.return_value = mock_response
mock_settings.OPENAI_MODEL = "test-model" mock_settings.OPENAI_MODEL = "test-model"
@ -73,11 +78,12 @@ class TestProcessMemo:
def test_process_memo_uses_correct_api_settings(self, sample_image_memo): def test_process_memo_uses_correct_api_settings(self, sample_image_memo):
with ( with (
patch("webui.tasks.litellm") as mock_litellm, patch("penparse.tasks.litellm") as mock_litellm,
patch("webui.tasks.settings") as mock_settings, patch("penparse.tasks.settings") as mock_settings,
): ):
mock_response = MagicMock() mock_response = MagicMock()
mock_response.choices[0].message = {"content": "Transcribed content"} mock_response.choices[0].message = {
"content": "Transcribed content"}
mock_litellm.completion.return_value = mock_response mock_litellm.completion.return_value = mock_response
mock_settings.OPENAI_API_BASE = "https://test-api-base.com" mock_settings.OPENAI_API_BASE = "https://test-api-base.com"
mock_settings.OPENAI_API_KEY = "test-api-key" mock_settings.OPENAI_API_KEY = "test-api-key"

View File

@ -1,26 +1,26 @@
""" from django.urls import path
URL configuration for penparse project.
The `urlpatterns` list routes URLs to views. For more information please see: from . import views
https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.urls import include, path
from django.contrib import admin
from django.urls import path, include
urlpatterns = [ urlpatterns = [
path("", include("webui.urls")), path("", views.index, name="index"),
path('admin/', admin.site.urls), path("dashboard", views.dashboard, name="dashboard"),
path("accounts/", include("django.contrib.auth.urls")), path("settings", views.settings, name="settings"),
path("documents/upload", views.upload_document, name="upload_document"),
path("documents/<str:pk>", views.view_document, name="view_document"),
path(
"documents/<str:pk>/thumbnail",
views.document_thumbnail,
name="document_thumbnail",
),
path(
"documents/<str:pk>/image",
views.document_image,
name="document_image",
),
path(
"documents/<str:pk>/download", views.download_document, name="download_document"
),
path("documents/<str:pk>/delete", views.delete_document, name="delete_document"),
path("auth/register", views.register, name="register"),
] ]

View File

@ -1,42 +0,0 @@
# Generated by Django 4.2.16 on 2024-11-30 06:31
from django.db import migrations, models
import django.utils.timezone
import webui.models
class Migration(migrations.Migration):
initial = True
dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
]
operations = [
migrations.CreateModel(
name='User',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('email', models.EmailField(max_length=254, unique=True)),
('first_name', models.CharField(max_length=150)),
('last_name', models.CharField(max_length=150)),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')),
],
options={
'verbose_name': 'user',
'verbose_name_plural': 'users',
'abstract': False,
},
managers=[
('objects', webui.models.UserManager()),
],
),
]

View File

@ -1,26 +0,0 @@
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("dashboard", views.dashboard, name="dashboard"),
path("settings", views.settings, name="settings"),
path("documents/upload", views.upload_document, name="upload_document"),
path("documents/<str:pk>", views.view_document, name="view_document"),
path(
"documents/<str:pk>/thumbnail",
views.document_thumbnail,
name="document_thumbnail",
),
path(
"documents/<str:pk>/image",
views.document_image,
name="document_image",
),
path(
"documents/<str:pk>/download", views.download_document, name="download_document"
),
path("documents/<str:pk>/delete", views.delete_document, name="delete_document"),
path("auth/register", views.register, name="register"),
]