Skip to content

Commit 870e8e2

Browse files
committed
connecting to postgresql
1 parent fb7e319 commit 870e8e2

File tree

12 files changed

+161
-18
lines changed

12 files changed

+161
-18
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Generated by Django 4.2.10 on 2025-03-27 01:32
2+
3+
from django.conf import settings
4+
import django.contrib.auth.models
5+
import django.contrib.auth.validators
6+
from django.db import migrations, models
7+
import django.db.models.deletion
8+
import django.utils.timezone
9+
10+
11+
class Migration(migrations.Migration):
12+
13+
initial = True
14+
15+
dependencies = [
16+
('auth', '0012_alter_user_first_name_max_length'),
17+
]
18+
19+
operations = [
20+
migrations.CreateModel(
21+
name='CustomUser',
22+
fields=[
23+
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
24+
('password', models.CharField(max_length=128, verbose_name='password')),
25+
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
26+
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
27+
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
28+
('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')),
29+
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
30+
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
31+
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
32+
('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')),
33+
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
34+
('supabase_uid', models.CharField(blank=True, max_length=255, null=True, unique=True)),
35+
('oauth_provider', models.CharField(blank=True, max_length=50, null=True)),
36+
('metadata', models.JSONField(blank=True, default=dict, null=True)),
37+
('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')),
38+
('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')),
39+
],
40+
options={
41+
'verbose_name': 'User',
42+
'verbose_name_plural': 'Users',
43+
'swappable': 'AUTH_USER_MODEL',
44+
},
45+
managers=[
46+
('objects', django.contrib.auth.models.UserManager()),
47+
],
48+
),
49+
migrations.CreateModel(
50+
name='UserData',
51+
fields=[
52+
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
53+
('profile_data', models.JSONField(blank=True, default=dict, null=True)),
54+
('preferences', models.JSONField(blank=True, default=dict, null=True)),
55+
('created_at', models.DateTimeField(auto_now_add=True)),
56+
('updated_at', models.DateTimeField(auto_now=True)),
57+
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='user_data', to=settings.AUTH_USER_MODEL)),
58+
],
59+
),
60+
]

backend/apps/authentication/migrations/__init__.py

Whitespace-only changes.

backend/apps/credits/models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from django.db import models, transaction
2-
from django.contrib.auth.models import User
2+
from django.conf import settings
33
from django.utils.translation import gettext_lazy as _
44
import logging
55
import uuid
@@ -27,7 +27,7 @@ class CreditTransaction(models.Model):
2727
editable=False
2828
)
2929
user = models.ForeignKey(
30-
User,
30+
settings.AUTH_USER_MODEL,
3131
on_delete=models.CASCADE,
3232
related_name='credit_transactions'
3333
)
@@ -113,7 +113,7 @@ class CreditHold(models.Model):
113113
editable=False
114114
)
115115
user = models.ForeignKey(
116-
User,
116+
settings.AUTH_USER_MODEL,
117117
on_delete=models.CASCADE,
118118
related_name='credit_holds'
119119
)

backend/apps/credits/throttling.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def _get_required_credits(self, request: Request) -> int:
105105
pattern = rate.endpoint_path
106106
# Convert the endpoint pattern to a regex pattern
107107
# Replace {id} or similar placeholders with regex groups
108-
regex_pattern = pattern.replace('/', '\/')
108+
regex_pattern = pattern.replace('/', r'\/')
109109
regex_pattern = re.sub(r'\{[^}]+\}', r'[^\/]+', regex_pattern)
110110

111111
if re.match(regex_pattern, path):

backend/apps/credits/urls.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from django.urls import path, include
22
from rest_framework.routers import DefaultRouter
33

4-
from . import base
4+
from . import views
55

66
router = DefaultRouter()
77
router.register(
8-
r"transactions", base.CreditTransactionViewSet, basename="credit-transaction"
8+
r"transactions", views.CreditTransactionViewSet, basename="credit-transaction"
99
)
10-
router.register(r"rates", base.CreditUsageRateViewSet, basename="credit-rate")
10+
router.register(r"rates", views.CreditUsageRateViewSet, basename="credit-rate")
1111

1212
urlpatterns = [
13-
path("credits/", base.get_credit_balance, name="credit-balance"),
13+
path("credits/", views.get_credit_balance, name="credit-balance"),
1414
path("credits/", include(router.urls)),
1515
]

backend/apps/users/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import json
55

66
from django.conf import settings
7-
from django.contrib.auth.models import User
7+
from django.contrib.auth import get_user_model
88
from rest_framework import viewsets, status, permissions
99
from rest_framework.decorators import action, api_view, permission_classes
1010
from rest_framework.request import Request
@@ -20,6 +20,9 @@
2020
# Initialize the Supabase Auth Service
2121
auth_service = SupabaseAuthService()
2222

23+
# Get the custom user model
24+
User = get_user_model()
25+
2326

2427
class IsAdminOrSelf(permissions.BasePermission):
2528
"""

backend/apps/users/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from django.db import models, transaction
2-
from django.contrib.auth.models import User
2+
from django.conf import settings
33
from django.utils.translation import gettext_lazy as _
44

55
class UserProfile(models.Model):
@@ -10,7 +10,7 @@ class UserProfile(models.Model):
1010
that are specific to our application, including Supabase-related information.
1111
"""
1212
user = models.OneToOneField(
13-
User,
13+
settings.AUTH_USER_MODEL,
1414
on_delete=models.CASCADE,
1515
related_name='profile'
1616
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from django.conf import settings
2+
from django.db import models
3+
4+
5+
6+
# The Profile model extends the default user model with additional fields for AI-enabled features
7+
# and credit management. This ensures that each user can have unique AI-related settings and credit balance.
8+
9+
class Profile(models.Model):
10+
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='profile')
11+
first_name = models.CharField(max_length=50, blank=True, null=True, help_text="User's first name")
12+
last_name = models.CharField(max_length=50, blank=True, null=True, help_text="User's last name")
13+
email = models.EmailField(blank=True, null=True, help_text="User's email address")
14+
phone_number = models.CharField(max_length=20, blank=True, null=True, help_text="User's contact phone number")
15+
source = models.CharField(max_length=100, blank=True, null=True, help_text="Signup source or context")
16+
contextual_info = models.TextField(blank=True, null=True, help_text="Additional contextual information for business prompting")
17+
ai_enabled = models.BooleanField(default=False, help_text='Enable AI features for the user')
18+
credits = models.PositiveIntegerField(default=0, help_text='Credit balance for AI execution')
19+
created_at = models.DateTimeField(auto_now_add=True)
20+
updated_at = models.DateTimeField(auto_now=True)
21+
22+
def __str__(self):
23+
return f"Profile for {self.user.username}"
24+
25+
26+
27+
# The CreditTransaction model records credit transactions related to AI usage.
28+
# This allows for tracking and auditing credit usages or additions over time.
29+
30+
class CreditTransaction(models.Model):
31+
profile = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name='transactions')
32+
amount = models.IntegerField(help_text='Amount of credits deducted (negative) or added (positive)')
33+
description = models.TextField(blank=True, null=True, help_text='Reason for this transaction')
34+
created_at = models.DateTimeField(auto_now_add=True)
35+
36+
def __str__(self):
37+
return f"Transaction of {self.amount} for {self.profile.user.username} at {self.created_at}"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from django.conf import settings
2+
from django.db import models
3+
from .profile import Profile
4+
5+
6+
# The UserContext model captures contextual business flow information for agentive operations.
7+
# This model supports dynamic business operations by storing relevant context data in a JSON field,
8+
# which can be used to tailor AI-driven business processes.
9+
10+
11+
class UserContext(models.Model):
12+
profile = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name='user_contexts')
13+
business_flow = models.CharField(max_length=100, help_text='Name or type of the business flow')
14+
context_data = models.JSONField(blank=True, null=True, help_text='Detailed context information for agentive business flows')
15+
created_at = models.DateTimeField(auto_now_add=True)
16+
updated_at = models.DateTimeField(auto_now=True)
17+
18+
def __str__(self):
19+
return f"Context for {self.profile.user.username} - {self.business_flow}"

backend/apps/users/urls.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
from rest_framework.routers import DefaultRouter
44

55
from . import base
6-
from .base import auth_view, client_view, database_view, edge_functions_view, health_check, realtime_view, storage_view, script_view
6+
from .views import auth_view, client_view, database_view, edge_functions_view, realtime_view, storage_view
7+
from .views.creditable_views import utility_view
78

9+
# Import health check from base if it exists there, otherwise we'll need to find it
10+
from ..authentication.views import health_check
811
router = DefaultRouter()
912
router.register(r'users', base.UserViewSet)
1013

0 commit comments

Comments
 (0)