Skip to content

Commit

Permalink
Merge branch 'master' into secrets-manager
Browse files Browse the repository at this point in the history
  • Loading branch information
nikochiko committed Feb 6, 2024
2 parents 6f977c8 + 4783f7d commit a33a280
Show file tree
Hide file tree
Showing 120 changed files with 8,721 additions and 1,907 deletions.
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- repo: local
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ RUN pip install --no-cache-dir -U poetry pip && poetry install --no-cache --only

# install nltk stopwords
RUN poetry run python -c 'import nltk; nltk.download("stopwords")'
# install playwright
RUN poetry run playwright install-deps && poetry run playwright install

# copy the code into the container
COPY . .
Expand Down
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Install [pyenv](https://github.com/pyenv/pyenv) & install the same python version as in our [Dockerfile](Dockerfile)
* Install [poetry](https://python-poetry.org/docs/)
* Clone the github repo to gooey-server (and make sure that's the folder name)
* Create & activate a virtualenv (e.g. `poetry shell`)
* Run `poetry install --with dev`
* Install [redis](https://redis.io/docs/getting-started/installation/install-redis-on-mac-os/), [rabbitmq](https://www.rabbitmq.com/install-homebrew.html), and [postgresql](https://formulae.brew.sh/formula/postgresql@15) (e.g. `brew install redis rabbitmq postgresql@15`)
Expand Down Expand Up @@ -87,7 +88,7 @@ gooey.ai (dev)
App ID: 228027632918921
```

Create a [meta developer account](https://developers.facebook.com/docs/development/register/) & ask someone to add you to the test app [here](https://developers.facebook.com/apps/228027632918921/roles/roles/?business_id=549319917267066)
Create a [meta developer account](https://developers.facebook.com/docs/development/register/) & send admin your **facebook ID** to add you to the test app [here](https://developers.facebook.com/apps/228027632918921/roles/roles/?business_id=549319917267066)

1. start ngrok

Expand All @@ -107,6 +108,12 @@ ngrok http 8080
5. Copy the temporary access token there and set env var `WHATSAPP_ACCESS_TOKEN = XXXX`


**(Optional) Use the test script to send yourself messages**

```bash
python manage.py runscript test_wa_msg_send --script-args 104696745926402 +918764022384
```
Replace `+918764022384` with your number and `104696745926402` with the test number ID

## Dangerous postgres commands

Expand All @@ -126,6 +133,7 @@ docker cp $cid:/app/$fname .
echo $PWD/$fname
```

**on local**
```bash
# reset the database
./manage.py reset_db -c
Expand All @@ -137,6 +145,7 @@ pg_restore --no-privileges --no-owner -d $PGDATABASE $fname

### create & load fixtures

**on server**
```bash
# select a running container
cid=$(docker ps | grep gooey-api-prod | cut -d " " -f 1 | head -1)
Expand All @@ -148,6 +157,12 @@ docker cp $cid:/app/fixture.json .
echo $PWD/fixture.json
```

**on local**
```bash
# copy fixture.json from server to local
rsync -P -a <username>@captain.us-1.gooey.ai:/home/<username>/fixture.json .
```

```bash
# reset the database
./manage.py reset_db -c
Expand All @@ -157,12 +172,16 @@ echo $PWD/fixture.json
./manage.py migrate
# load the fixture
./manage.py loaddata fixture.json
# create a superuser to access admin
./manage.py createsuperuser
```

### copy one postgres db to another

```
**on server**
```bash
./manage.py reset_db
createdb -T template0 $PGDATABASE
pg_dump $SOURCE_DATABASE | psql -q $PGDATABASE
```

35 changes: 33 additions & 2 deletions app_users/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib import admin
from django.contrib.admin.models import LogEntry

from app_users import models
from bots.admin_links import open_in_new_tab, list_related_html_url
Expand Down Expand Up @@ -128,10 +129,40 @@ class AppUserTransactionAdmin(admin.ModelAdmin):
"invoice_id",
"user",
"amount",
"created_at",
"end_balance",
"payment_provider",
"dollar_amount",
"created_at",
]
readonly_fields = ["created_at"]
list_filter = ["created_at", IsStripeFilter]
list_filter = ["created_at", IsStripeFilter, "payment_provider"]
inlines = [SavedRunInline]
ordering = ["-created_at"]

@admin.display(description="Charged Amount")
def dollar_amount(self, obj: models.AppUserTransaction):
if not obj.payment_provider:
return
return f"${obj.charged_amount / 100}"


@admin.register(LogEntry)
class LogEntryAdmin(admin.ModelAdmin):
list_display = readonly_fields = [
"action_time",
"user",
"action_flag",
"content_type",
"object_repr",
"object_id",
"change_message",
]

# to have a date-based drilldown navigation in the admin page
date_hierarchy = "action_time"

# to filter the results by users, content types and action flags
list_filter = ["action_time", "user", "content_type", "action_flag"]

# when searching the user will be able to search in both object_repr and change_message
search_fields = ["object_repr", "change_message"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Generated by Django 4.2.7 on 2024-01-08 13:43

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("app_users", "0010_alter_appuser_balance_alter_appuser_created_at_and_more"),
]

operations = [
migrations.AddField(
model_name="appusertransaction",
name="charged_amount",
field=models.PositiveIntegerField(
default=0,
help_text="The charged dollar amount in the currency’s smallest unit.<br>E.g. for 10 USD, this would be of 1000 (that is, 1000 cents).<br><a href='https://stripe.com/docs/currencies'>Learn More</a>",
),
),
migrations.AddField(
model_name="appusertransaction",
name="payment_provider",
field=models.IntegerField(
blank=True,
choices=[(1, "Stripe"), (2, "Paypal")],
default=None,
help_text="The payment provider used for this transaction.<br>If this is provided, the Charged Amount should also be provided.",
null=True,
),
),
migrations.AlterField(
model_name="appusertransaction",
name="amount",
field=models.IntegerField(
help_text="The amount (Gooey credits) added/deducted in this transaction.<br>Positive for credits added, negative for credits deducted."
),
),
migrations.AlterField(
model_name="appusertransaction",
name="end_balance",
field=models.IntegerField(
help_text="The end balance (Gooey credits) of the user after this transaction"
),
),
migrations.AlterField(
model_name="appusertransaction",
name="invoice_id",
field=models.CharField(
help_text="The Payment Provider's Invoice ID for this transaction.<br>For Gooey, this will be of the form 'gooey_in_{uuid}'",
max_length=255,
unique=True,
),
),
]
49 changes: 42 additions & 7 deletions app_users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ def first_name(self):

@db_middleware
@transaction.atomic
def add_balance(self, amount: int, invoice_id: str) -> "AppUserTransaction":
def add_balance(
self, amount: int, invoice_id: str, **kwargs
) -> "AppUserTransaction":
"""
Used to add/deduct credits when they are bought or consumed.
Expand All @@ -117,7 +119,6 @@ def add_balance(self, amount: int, invoice_id: str) -> "AppUserTransaction":
When credits are deducted due to a run -- invoice_id is of the
form "gooey_in_{uuid}"
"""

# if an invoice entry exists
try:
# avoid updating twice for same invoice
Expand All @@ -133,12 +134,12 @@ def add_balance(self, amount: int, invoice_id: str) -> "AppUserTransaction":
user: AppUser = AppUser.objects.select_for_update().get(pk=self.pk)
user.balance += amount
user.save(update_fields=["balance"])

return AppUserTransaction.objects.create(
user=self,
invoice_id=invoice_id,
amount=amount,
end_balance=user.balance,
**kwargs,
)

def copy_from_firebase_user(self, user: auth.UserRecord) -> "AppUser":
Expand Down Expand Up @@ -177,7 +178,9 @@ def copy_from_firebase_user(self, user: auth.UserRecord) -> "AppUser":
default_balance = settings.LOGIN_USER_FREE_CREDITS
if self.is_anonymous:
default_balance = settings.ANON_USER_FREE_CREDITS
elif provider_list[-1].provider_id == "password":
elif (
"+" in str(self.email) or "@gmail.com" not in str(self.email)
) and provider_list[-1].provider_id == "password":
default_balance = settings.EMAIL_USER_FREE_CREDITS
self.balance = db.get_doc_field(
doc_ref=db.get_user_doc_ref(user.uid),
Expand Down Expand Up @@ -217,13 +220,45 @@ def search_stripe_customer(self) -> stripe.Customer | None:
return customer


class PaymentProvider(models.IntegerChoices):
STRIPE = 1, "Stripe"
PAYPAL = 2, "Paypal"


class AppUserTransaction(models.Model):
user = models.ForeignKey(
"AppUser", on_delete=models.CASCADE, related_name="transactions"
)
invoice_id = models.CharField(max_length=255, unique=True)
amount = models.IntegerField()
end_balance = models.IntegerField()
invoice_id = models.CharField(
max_length=255,
unique=True,
help_text="The Payment Provider's Invoice ID for this transaction.<br>"
"For Gooey, this will be of the form 'gooey_in_{uuid}'",
)

amount = models.IntegerField(
help_text="The amount (Gooey credits) added/deducted in this transaction.<br>"
"Positive for credits added, negative for credits deducted."
)
end_balance = models.IntegerField(
help_text="The end balance (Gooey credits) of the user after this transaction"
)

payment_provider = models.IntegerField(
choices=PaymentProvider.choices,
null=True,
blank=True,
default=None,
help_text="The payment provider used for this transaction.<br>"
"If this is provided, the Charged Amount should also be provided.",
)
charged_amount = models.PositiveIntegerField(
help_text="The charged dollar amount in the currency’s smallest unit.<br>"
"E.g. for 10 USD, this would be of 1000 (that is, 1000 cents).<br>"
"<a href='https://stripe.com/docs/currencies'>Learn More</a>",
default=0,
)

created_at = models.DateTimeField(editable=False, blank=True, default=timezone.now)

class Meta:
Expand Down
Loading

0 comments on commit a33a280

Please sign in to comment.