From dee74d7ea481bba5e7800a4e2c05cded894a076a Mon Sep 17 00:00:00 2001 From: James Ravenscroft Date: Wed, 18 Dec 2024 21:35:31 +0000 Subject: [PATCH] enable user tokens --- penparse/penparse/settings.py | 15 +++++++++++++-- penparse/webui/models.py | 15 +++++++++++++-- penparse/webui/signals.py | 11 +++++++++++ pyproject.toml | 2 ++ uv.lock | 28 ++++++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 penparse/webui/signals.py diff --git a/penparse/penparse/settings.py b/penparse/penparse/settings.py index dd9d93c..8d61310 100644 --- a/penparse/penparse/settings.py +++ b/penparse/penparse/settings.py @@ -45,6 +45,9 @@ INSTALLED_APPS = [ "webui", "markdown_deux", "markdownify.apps.MarkdownifyConfig", + "rest_framework", + "rest_framework.authtoken", + "django_filters", ] MIDDLEWARE = [ @@ -57,6 +60,14 @@ MIDDLEWARE = [ "django.middleware.clickjacking.XFrameOptionsMiddleware", ] +REST_FRAMEWORK = { + # Use Django's standard `django.contrib.auth` permissions, + # or allow read-only access for unauthenticated users. + 'DEFAULT_PERMISSION_CLASSES': [ + 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly' + ] +} + ROOT_URLCONF = "penparse.urls" TEMPLATES = [ @@ -150,6 +161,6 @@ CELERY_BROKER_URL = "amqp://guest:guest@localhost/" OPENAI_API_BASE = os.getenv("OPENAI_API_BASE") OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") -#OPENAI_API_KEY = "test" +# OPENAI_API_KEY = "test" OPENAI_MODEL = os.getenv("OPENAI_MODEL", "openai/gpt-4o") -#OPENAI_MODEL="ollama/llama3.2-vision" +# OPENAI_MODEL="ollama/llama3.2-vision" diff --git a/penparse/webui/models.py b/penparse/webui/models.py index b512eb4..1d3e493 100644 --- a/penparse/webui/models.py +++ b/penparse/webui/models.py @@ -3,7 +3,10 @@ from django.contrib.auth.base_user import BaseUserManager from django.db import models from uuid import uuid4 -from email.header import Charset +from django.conf import settings +from django.db.models.signals import post_save +from django.dispatch import receiver +from rest_framework.authtoken.models import Token class UserManager(BaseUserManager): @@ -57,7 +60,8 @@ class ImageMemo(models.Model): image = models.ImageField(upload_to="uploads/%Y/%m/%d") content = models.TextField() - author = models.ForeignKey("User", on_delete=models.CASCADE, related_name="memos") + author = models.ForeignKey( + "User", on_delete=models.CASCADE, related_name="memos") model_name = models.CharField(max_length=255, null=True) @@ -89,3 +93,10 @@ class User(AbstractUser): def __str__(self): """Return string representation of our user""" return self.email + + +@receiver(post_save, sender=settings.AUTH_USER_MODEL) +def create_auth_token(sender, instance=None, created=False, **kwargs): + """Create and save a token when a user is created.""" + if created: + Token.objects.create(user=instance) diff --git a/penparse/webui/signals.py b/penparse/webui/signals.py new file mode 100644 index 0000000..e5d6edf --- /dev/null +++ b/penparse/webui/signals.py @@ -0,0 +1,11 @@ + +from django.conf import settings +from django.db.models.signals import post_save +from django.dispatch import receiver +from rest_framework.authtoken.models import Token + + +@receiver(post_save, sender=settings.AUTH_USER_MODEL) +def create_auth_token(sender, instance=None, created=False, **kwargs): + if created: + Token.objects.create(user=instance) diff --git a/pyproject.toml b/pyproject.toml index 7ce2847..2064ecb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,9 +6,11 @@ readme = "README.md" requires-python = ">=3.9" dependencies = [ "celery>=5.4.0", + "django-filter>=24.3", "django-markdown-deux>=1.0.6", "django-markdownify>=0.9.5", "django>=4.2.16", + "djangorestframework>=3.15.2", "litellm>=1.54.1", "loguru>=0.7.3", "markdown>=3.7", diff --git a/uv.lock b/uv.lock index 7492e59..31aa427 100644 --- a/uv.lock +++ b/uv.lock @@ -406,6 +406,18 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/94/2c/6b6c7e493d5ea789416918658ebfa16be7a64c77610307497ed09a93c8c4/Django-4.2.16-py3-none-any.whl", hash = "sha256:1ddc333a16fc139fd253035a1606bb24261951bbc3a6ca256717fa06cc41a898", size = 7992936 }, ] +[[package]] +name = "django-filter" +version = "24.3" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "django" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/50/bc/dc19ae39c235332926dd0efe0951f663fa1a9fc6be8430737ff7fd566b20/django_filter-24.3.tar.gz", hash = "sha256:d8ccaf6732afd21ca0542f6733b11591030fa98669f8d15599b358e24a2cd9c3", size = 144444 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/09/b1/92f1c30b47c1ebf510c35a2ccad9448f73437e5891bbd2b4febe357cc3de/django_filter-24.3-py3-none-any.whl", hash = "sha256:c4852822928ce17fb699bcfccd644b3574f1a2d80aeb2b4ff4f16b02dd49dc64", size = 95011 }, +] + [[package]] name = "django-markdown-deux" version = "1.0.6" @@ -429,6 +441,18 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/1b/35/c7a4bd957b279a8e7c808116bed399b73874ed3da78689993ee76f30d9f6/django_markdownify-0.9.5-py3-none-any.whl", hash = "sha256:2c4ae44e386c209453caf5e9ea1b74f64535985d338ad2d5ad5e7089cc94be86", size = 10342 }, ] +[[package]] +name = "djangorestframework" +version = "3.15.2" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "django" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/2c/ce/31482eb688bdb4e271027076199e1aa8d02507e530b6d272ab8b4481557c/djangorestframework-3.15.2.tar.gz", hash = "sha256:36fe88cd2d6c6bec23dca9804bab2ba5517a8bb9d8f47ebc68981b56840107ad", size = 1067420 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/7c/b6/fa99d8f05eff3a9310286ae84c4059b08c301ae4ab33ae32e46e8ef76491/djangorestframework-3.15.2-py3-none-any.whl", hash = "sha256:2b8871b062ba1aefc2de01f773875441a961fefbf79f5eed1e32b2f096944b20", size = 1071235 }, +] + [[package]] name = "exceptiongroup" version = "1.2.2" @@ -998,8 +1022,10 @@ source = { virtual = "." } dependencies = [ { name = "celery" }, { name = "django" }, + { name = "django-filter" }, { name = "django-markdown-deux" }, { name = "django-markdownify" }, + { name = "djangorestframework" }, { name = "litellm" }, { name = "loguru" }, { name = "markdown" }, @@ -1015,8 +1041,10 @@ dependencies = [ requires-dist = [ { name = "celery", specifier = ">=5.4.0" }, { name = "django", specifier = ">=4.2.16" }, + { name = "django-filter", specifier = ">=24.3" }, { name = "django-markdown-deux", specifier = ">=1.0.6" }, { name = "django-markdownify", specifier = ">=0.9.5" }, + { name = "djangorestframework", specifier = ">=3.15.2" }, { name = "litellm", specifier = ">=1.54.1" }, { name = "loguru", specifier = ">=0.7.3" }, { name = "markdown", specifier = ">=3.7" },