Skip to content

Commit

Permalink
Merge branch 'master' into save-for-anon-users
Browse files Browse the repository at this point in the history
  • Loading branch information
nikochiko committed Sep 16, 2024
2 parents 44298a4 + 0541624 commit b5a0a7d
Show file tree
Hide file tree
Showing 113 changed files with 4,287 additions and 1,750 deletions.
3 changes: 3 additions & 0 deletions .gitleaksignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
4749e3ef005e8ddc6562d1bd82a00e752a7e94e3:explore.py:aws-access-token:16
ede582d5a859726ff04fe371e9066f9dffb393f7:explore.py:aws-access-token:16
4749e3ef005e8ddc6562d1bd82a00e752a7e94e3:explore.py:private-key:23
ede582d5a859726ff04fe371e9066f9dffb393f7:explore.py:private-key:23
4749e3ef005e8ddc6562d1bd82a00e752a7e94e3:explore.py:generic-api-key:32
ede582d5a859726ff04fe371e9066f9dffb393f7:explore.py:generic-api-key:32
b0c80dac8e22faafa319d5466947df8723dfaa4a:daras_ai_v2/img_model_settings_widgets.py:generic-api-key:372
8670036e722f40530dbff3e0e7573e9b5aac85c9:routers/slack.py:slack-webhook-url:73
b6ad1fc0168832711adcff07287907660f3305fb:bots/location.py:generic-api-key:12
Expand Down
81 changes: 44 additions & 37 deletions app_users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,47 @@
from django.db.models import Sum

from app_users import models
from bots.admin_links import open_in_new_tab, list_related_html_url
from bots.admin_links import open_in_new_tab, list_related_html_url, change_obj_url
from bots.models import SavedRun, PublishedRun
from embeddings.models import EmbeddedFile
from usage_costs.models import UsageCost


# Register your models here.
from workspaces.admin import WorkspaceAdmin, WorkspaceMembershipInline


@admin.register(models.AppUser)
class AppUserAdmin(admin.ModelAdmin):
deprecated_fields = (
"balance",
"is_paying",
"stripe_customer_id",
"subscription",
"low_balance_email_sent_at",
)
fieldsets = [
(
None,
{
"fields": [
"uid",
("email", "phone_number"),
"balance",
"subscription",
"stripe_customer_id",
"total_payments",
"total_charged",
"total_usage_cost",
"is_disabled",
"is_anonymous",
"is_paying",
"disable_safety_checker",
"disable_rate_limits",
"personal_workspace",
("total_payments", "total_charged", "total_usage_cost"),
(
"is_disabled",
"is_anonymous",
),
(
"disable_safety_checker",
"disable_rate_limits",
),
(
"view_saved_runs",
"view_published_runs",
"view_embedded_files",
"view_transactions",
),
"created_at",
"upgraded_from_anonymous_at",
("created_at", "updated_at", "upgraded_from_anonymous_at"),
("open_in_firebase", "open_in_stripe"),
"low_balance_email_sent_at",
],
},
),
Expand All @@ -59,30 +61,32 @@ class AppUserAdmin(admin.ModelAdmin):
]
},
),
(
"Deprecated",
{
"fields": (deprecated_fields,),
},
),
]
list_display = [
"uid",
"handle",
"subscription",
"display_name",
"email",
"phone_number",
"balance",
"is_paying",
"personal_workspace",
"created_at",
]
search_fields = [
"uid",
"display_name",
"email",
"phone_number",
"stripe_customer_id",
"handle__name",
]
list_filter = [
"is_anonymous",
"is_disabled",
"is_paying",
"created_at",
"upgraded_from_anonymous_at",
]
Expand All @@ -91,16 +95,18 @@ class AppUserAdmin(admin.ModelAdmin):
"total_charged",
"total_usage_cost",
"created_at",
"updated_at",
"upgraded_from_anonymous_at",
"view_saved_runs",
"view_published_runs",
"view_embedded_files",
"view_transactions",
"open_in_firebase",
"open_in_stripe",
"low_balance_email_sent_at",
"personal_workspace",
]
autocomplete_fields = ["handle", "subscription"]
autocomplete_fields = ["handle"]
inlines = [WorkspaceMembershipInline]

@admin.display(description="User Runs")
def view_saved_runs(self, user: models.AppUser):
Expand Down Expand Up @@ -170,24 +176,24 @@ def open_in_firebase(self, user: models.AppUser):

open_in_firebase.short_description = "Open in Firebase"

@admin.display(description="Open in Stripe")
def open_in_stripe(self, user: models.AppUser):
if not user.stripe_customer_id:
# Try to find the customer ID.
user.search_stripe_customer()
if not user.stripe_customer_id:
# If we still don't have a customer ID, return None.
raise AttributeError("No Stripe customer ID found.")
return open_in_new_tab(
f"https://dashboard.stripe.com/customers/{user.stripe_customer_id}",
label=user.stripe_customer_id,
return WorkspaceAdmin.open_in_stripe(
self, user.get_or_create_personal_workspace()[0]
)

open_in_stripe.short_description = "Open in Stripe"

@admin.display(description="View transactions")
def view_transactions(self, user: models.AppUser):
return list_related_html_url(user.transactions, show_add=False)

@admin.display(description="Personal Workspace")
def personal_workspace(self, user: models.AppUser):
workspace = user.get_or_create_personal_workspace()[0]
return change_obj_url(
workspace,
label=f"Bal = {workspace.balance} | Paid = {workspace.is_paying} | Sub = {workspace.subscription}",
)


class SavedRunInline(admin.StackedInline):
model = SavedRun
Expand All @@ -213,9 +219,10 @@ def has_delete_permission(self, request, obj=None):

@admin.register(models.AppUserTransaction)
class AppUserTransactionAdmin(admin.ModelAdmin):
autocomplete_fields = ["user"]
autocomplete_fields = ["user", "workspace"]
list_display = [
"invoice_id",
"workspace",
"user",
"amount",
"dollar_amount",
Expand Down
18 changes: 18 additions & 0 deletions app_users/migrations/0020_appuser_updated_at.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2024-09-10 12:46

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('app_users', '0019_alter_appusertransaction_reason'),
]

operations = [
migrations.AddField(
model_name='appuser',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 5.1.1 on 2024-09-12 14:18

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('app_users', '0020_appuser_updated_at'),
]

operations = [
migrations.RemoveIndex(
model_name='appusertransaction',
name='app_users_a_user_id_9b2e8d_idx',
),
migrations.RemoveIndex(
model_name='appusertransaction',
name='app_users_a_user_id_b0d6a5_idx',
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Generated by Django 5.0.9 on 2024-09-15 18:36

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


class Migration(migrations.Migration):

dependencies = [
('app_users', '0021_remove_appusertransaction_app_users_a_user_id_9b2e8d_idx_and_more'),
('handles', '0001_initial'),
('payments', '0005_alter_subscription_plan'),
('workspaces', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='appusertransaction',
name='workspace',
field=models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='transactions', to='workspaces.workspace'),
),
migrations.AlterField(
model_name='appuser',
name='balance',
field=models.IntegerField(blank=True, default=None, help_text='[Deprecated]', null=True, verbose_name='bal'),
),
migrations.AlterField(
model_name='appuser',
name='is_paying',
field=models.BooleanField(default=False, help_text='[Deprecated]', verbose_name='paid'),
),
migrations.AlterField(
model_name='appuser',
name='low_balance_email_sent_at',
field=models.DateTimeField(blank=True, help_text='[Deprecated]', null=True),
),
migrations.AlterField(
model_name='appuser',
name='stripe_customer_id',
field=models.CharField(blank=True, default='', help_text='[Deprecated]', max_length=255),
),
migrations.AlterField(
model_name='appuser',
name='subscription',
field=models.OneToOneField(blank=True, help_text='[Deprecated]', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='user', to='payments.subscription'),
),
migrations.AlterField(
model_name='appusertransaction',
name='user',
field=models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='transactions', to='app_users.appuser'),
),
migrations.AddIndex(
model_name='appuser',
index=models.Index(fields=['email'], name='app_users_a_email_2228c5_idx'),
),
migrations.AddIndex(
model_name='appusertransaction',
index=models.Index(fields=['workspace', 'amount', '-created_at'], name='app_users_a_workspa_bd7eb5_idx'),
),
migrations.AddConstraint(
model_name='appusertransaction',
constraint=models.CheckConstraint(check=models.Q(('user__isnull', False), ('workspace__isnull', False), _connector='OR'), name='user_or_workspace_present'),
),
]
20 changes: 20 additions & 0 deletions app_users/migrations/0023_alter_appusertransaction_workspace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 5.1.1 on 2024-09-16 00:13

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


class Migration(migrations.Migration):

dependencies = [
('app_users', '0022_appusertransaction_workspace_alter_appuser_balance_and_more'),
('workspaces', '0002_alter_workspace_domain_name'),
]

operations = [
migrations.AlterField(
model_name='appusertransaction',
name='workspace',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='transactions', to='workspaces.workspace'),
),
]
Loading

0 comments on commit b5a0a7d

Please sign in to comment.