diff --git a/.dockerignore b/.dockerignore index 09f3b9b72..c4d8cda6c 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,3 +1,51 @@ +#django +*.pyc +*.pyo +*.mo +*.db +*.css.map +*.egg-info +*.sql.gz +.cache +.project +.idea +.pydevproject +.idea/workspace.xml +.DS_Store +.git/ +.sass-cache +.vagrant/ +__pycache__ +dist +docs +env +logs +src/{{ project_name }}/settings/local.py +src/node_modules +web/media +web/static/CACHE +stats +Dockerfile* +static/ +frontend/ +staticfiles/ + +#react +node_modules +build +.git +.gitignore +LICENSE +VERSION +README.md +Changelog.md +Makefile +docker-compose.yml +docs +Dockerfile +static + +#general README.md node_modules pgdata diff --git a/.github/workflows/django-postgres.yml b/.github/workflows/django-postgres.yml index 7cafa25c5..2e83d81dc 100644 --- a/.github/workflows/django-postgres.yml +++ b/.github/workflows/django-postgres.yml @@ -9,11 +9,6 @@ on: pull_request: branches: ["main"] - -defaults: - run: - working-directory: lotus - permissions: contents: read diff --git a/lotus/Dockerfile b/Dockerfile similarity index 86% rename from lotus/Dockerfile rename to Dockerfile index 3579bd335..2a7059c22 100644 --- a/lotus/Dockerfile +++ b/Dockerfile @@ -3,17 +3,16 @@ # --------------------------------------- FROM --platform=linux/amd64 python:3.9-bullseye AS build ENV PYTHONUNBUFFERED 1 -ENV POSTGRES_USER lotus -ENV POSTGRES_NAME lotus -ENV POSTGRES_PASSWORD lotus #make lotus user -WORKDIR /code +WORKDIR /lotus # pip install optimization COPY Pipfile Pipfile.lock ./ RUN pip install -U --no-cache-dir --disable-pip-version-check pipenv RUN pipenv install --system --deploy --ignore-pipfile # copy python files, -COPY . . +COPY ./lotus/ ./lotus/ +COPY ./metering_billing/ ./metering_billing/ +COPY ./manage.py ./ # --------------------------------------- # Development # --------------------------------------- diff --git a/lotus/Pipfile b/Pipfile similarity index 100% rename from lotus/Pipfile rename to Pipfile diff --git a/lotus/Pipfile.lock b/Pipfile.lock similarity index 100% rename from lotus/Pipfile.lock rename to Pipfile.lock diff --git a/Procfile b/Procfile index bcd92e18b..59a2a34ec 100644 --- a/Procfile +++ b/Procfile @@ -1,4 +1,4 @@ release: chmod u+x release.sh && ./release.sh -web: gunicorn lotus.wsgi +web: gunicorn lotus.wsgi:application -w 4 --threads 4 --preload worker: celery -A lotus worker -l info beat: celery -A lotus beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler diff --git a/app.json b/app.json index c65325fb6..ab829b036 100644 --- a/app.json +++ b/app.json @@ -8,6 +8,9 @@ "addons":[ { "plan":"heroku-postgresql" + }, + { + "plan":"heroku-redis" } ], "buildpacks": [ @@ -32,6 +35,9 @@ }, "STRIPE_SECRET_KEY":{ "value": "1234" + }, + "ON_HEROKU":{ + "value": "True" } }, "scripts":{ diff --git a/docker-compose.dev.yaml b/docker-compose.dev.yaml index a6eb0f7ca..e3297d1c7 100644 --- a/docker-compose.dev.yaml +++ b/docker-compose.dev.yaml @@ -2,39 +2,48 @@ services: db: image: postgres:14-alpine restart: on-failure - env_file: - - ./env/.env.dev ports: - 5432:5432 volumes: - ./pgdata:/var/lib/postgresql/data + environment: + POSTGRES_USER: lotus + POSTGRES_PASSWORD: lotus + POSTGRES_DB: lotus backend: env_file: - ./env/.env.dev build: - context: ./lotus + context: . dockerfile: Dockerfile - target: development + target: build ports: - 8000:8000 volumes: - - ./lotus:/code - command: python3 manage.py runserver 0.0.0.0:8000 + - ./lotus:/lotus/lotus + - ./metering_billing:/lotus/metering_billing + command: > + sh -c "python3 manage.py wait_for_db && \ + python3 manage.py migrate && \ + python3 manage.py initadmin && \ + python3 manage.py setup_tasks && \ + python3 manage.py runserver 0.0.0.0:8000" depends_on: - db frontend: restart: always build: - context: ./frontend - dockerfile: Dockerfile + context: . + dockerfile: frontend.Dockerfile target: development ports: - 3000:3000 command: yarn run dev --host 0.0.0.0 --port 3000 volumes: - - ./frontend:/frontend:delegated + - ./src:/frontend/src:delegated + - ./public:/frontend/public:delegated - /frontend/node_modules/ stdin_open: true depends_on: @@ -49,7 +58,10 @@ services: celery: env_file: - ./env/.env.dev - build: ./lotus + build: + context: . + dockerfile: Dockerfile + target: build command: celery -A lotus worker -l info depends_on: - redis @@ -59,10 +71,15 @@ services: env_file: - ./env/.env.dev restart: on-failure - build: ./lotus + build: + context: . + dockerfile: Dockerfile + target: build command: celery -A lotus beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler depends_on: - celery + - backend + - redis volumes: pgdata: diff --git a/docker-compose.prod.yaml b/docker-compose.prod.yaml index f9dea84f9..f42fa5bf1 100644 --- a/docker-compose.prod.yaml +++ b/docker-compose.prod.yaml @@ -2,24 +2,26 @@ services: db: image: postgres:14-alpine restart: on-failure - env_file: - - ./env/.env expose: - 5432 volumes: - ./pgdata:/var/lib/postgresql/data + environment: + POSTGRES_USER: lotus + POSTGRES_PASSWORD: lotus + POSTGRES_DB: lotus backend: env_file: - ./env/.env build: - context: ./lotus + context: . dockerfile: Dockerfile target: build expose: - 8000 volumes: - - django_static_volume:/code/staticfiles + - django_static_volume:/lotus/staticfiles command: > sh -c "python3 manage.py wait_for_db && \ python3 manage.py migrate && \ @@ -33,9 +35,10 @@ services: frontend: restart: always build: - context: ./frontend - dockerfile: Dockerfile + context: . + dockerfile: frontend.Dockerfile target: production + stdin_open: true ports: - 80:80 volumes: @@ -53,7 +56,7 @@ services: env_file: - ./env/.env build: - context: ./lotus + context: . dockerfile: Dockerfile target: build command: celery -A lotus worker -l info @@ -66,12 +69,14 @@ services: - ./env/.env restart: on-failure build: - context: ./lotus + context: . dockerfile: Dockerfile target: build command: celery -A lotus beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler depends_on: - celery + - backend + - redis volumes: pgdata: diff --git a/frontend/Dockerfile b/frontend.Dockerfile similarity index 75% rename from frontend/Dockerfile rename to frontend.Dockerfile index babf5cf6b..adae4c719 100644 --- a/frontend/Dockerfile +++ b/frontend.Dockerfile @@ -2,14 +2,15 @@ # Development stage # --------------------------------------- FROM --platform=linux/amd64 node:18.7.0-alpine AS development -ENV DOCKER_BUILDKIT 0 -ENV COMPOSE_DOCKER_CLI_BUILD 0 WORKDIR /frontend -COPY package*.json yarn.lock tsconfig.json ./ +COPY package*.json yarn.lock tsconfig.json \ + vite.config.ts tsconfig.node.json postcss.config.cjs\ + tailwind.config.cjs ./ RUN yarn config set network-timeout 300000 && \ yarn install --frozen-lockfile # Bundle app source -COPY . ./ +COPY public/ ./public/ +COPY src/ ./src/ # --------------------------------------- # Build stage # --------------------------------------- @@ -21,7 +22,7 @@ RUN yarn run build FROM nginx:1.23-alpine AS production #copy static files to nginx RUN rm -rf /usr/share/nginx/html/* -COPY --from=build frontend/src/dist /usr/share/nginx/html +COPY --from=build /frontend/src/dist /usr/share/nginx/html COPY nginx.conf /etc/nginx/conf.d/default.conf ENTRYPOINT ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/frontend/.dockerignore b/frontend/.dockerignore deleted file mode 100644 index 0fb77db06..000000000 --- a/frontend/.dockerignore +++ /dev/null @@ -1,13 +0,0 @@ -node_modules -build -.git -.gitignore -LICENSE -VERSION -README.md -Changelog.md -Makefile -docker-compose.yml -docs -Dockerfile -static diff --git a/lotus/locustfiles/track_event.py b/locustfiles/track_event.py similarity index 100% rename from lotus/locustfiles/track_event.py rename to locustfiles/track_event.py diff --git a/lotus/.dockerignore b/lotus/.dockerignore deleted file mode 100644 index 6a898ad41..000000000 --- a/lotus/.dockerignore +++ /dev/null @@ -1,30 +0,0 @@ -*.pyc -*.pyo -*.mo -*.db -*.css.map -*.egg-info -*.sql.gz -.cache -.project -.idea -.pydevproject -.idea/workspace.xml -.DS_Store -.git/ -.sass-cache -.vagrant/ -__pycache__ -dist -docs -env -logs -src/{{ project_name }}/settings/local.py -src/node_modules -web/media -web/static/CACHE -stats -Dockerfile* -static/ -frontend/ -staticfiles/ \ No newline at end of file diff --git a/lotus/lotus/__init__.py b/lotus/__init__.py similarity index 62% rename from lotus/lotus/__init__.py rename to lotus/__init__.py index 0350f8107..377fce9c3 100644 --- a/lotus/lotus/__init__.py +++ b/lotus/__init__.py @@ -1,5 +1,5 @@ from __future__ import absolute_import, unicode_literals -from .celery import celery as celery_app +from lotus.lotus_celery import celery as celery_app __all__ = ("celery_app",) diff --git a/lotus/lotus/asgi.py b/lotus/asgi.py similarity index 100% rename from lotus/lotus/asgi.py rename to lotus/asgi.py diff --git a/lotus/lotus/celery.py b/lotus/lotus_celery.py similarity index 100% rename from lotus/lotus/celery.py rename to lotus/lotus_celery.py diff --git a/lotus/lotus/middleware.py b/lotus/middleware.py similarity index 100% rename from lotus/lotus/middleware.py rename to lotus/middleware.py diff --git a/lotus/lotus/settings.py b/lotus/settings.py similarity index 96% rename from lotus/lotus/settings.py rename to lotus/settings.py index 002fa90fe..c0df6a7df 100644 --- a/lotus/lotus/settings.py +++ b/lotus/settings.py @@ -25,23 +25,20 @@ # set casting, default value DEBUG=(bool, False), PROFILER_ENABLED=(bool, False), + DOCKERIZED=(bool, False), + ON_HEROKU=(bool, False), POSTGRES_NAME=(str, "lotus"), POSTGRES_USER=(str, "lotus"), POSTGRES_PASSWORD=(str, "lotus"), ) +DOCKERIZED = env("DOCKERIZED") +ON_HEROKU = env("ON_HEROKU") # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent local_env_file = BASE_DIR / "env/.env" if local_env_file.is_file(): environ.Env.read_env(local_env_file) - DOCKERIZED = True -else: - DOCKERIZED = False - global_env_file = BASE_DIR / ".." / "env/.env" - if global_env_file.is_file(): - environ.Env.read_env(global_env_file) - try: sentry_sdk.init( @@ -253,11 +250,9 @@ INTERNAL_IPS += [ip[: ip.rfind(".")] + ".1" for ip in ips] -# VITE_APP_DIR = BASE_DIR / "frontend" / "src" - -# DJANGO_VITE_ASSETS_PATH = BASE_DIR / "frontend" / "static" / "dist" +VITE_APP_DIR = BASE_DIR / "src" -STATICFILES_DIRS = [BASE_DIR / "static"] +STATICFILES_DIRS = [BASE_DIR / "static", VITE_APP_DIR / "dist"] STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles") STATIC_URL = "static/" diff --git a/lotus/lotus/urls.py b/lotus/urls.py similarity index 97% rename from lotus/lotus/urls.py rename to lotus/urls.py index 2c0c449e9..3543f69a1 100644 --- a/lotus/lotus/urls.py +++ b/lotus/urls.py @@ -37,7 +37,7 @@ ) from rest_framework import routers -from .settings import DEBUG, PROFILER_ENABLED +from .settings import DEBUG, ON_HEROKU, PROFILER_ENABLED router = routers.DefaultRouter() router.register(r"users", UserViewSet, basename="user") @@ -82,5 +82,5 @@ if PROFILER_ENABLED: urlpatterns += [path("silk/", include("silk.urls", namespace="silk"))] -if DEBUG: +if DEBUG or ON_HEROKU: urlpatterns += [re_path(".*", TemplateView.as_view(template_name="index.html"))] diff --git a/lotus/lotus/wsgi.py b/lotus/wsgi.py similarity index 100% rename from lotus/lotus/wsgi.py rename to lotus/wsgi.py diff --git a/lotus/manage.py b/manage.py similarity index 100% rename from lotus/manage.py rename to manage.py diff --git a/lotus/metering_billing/__init__.py b/metering_billing/__init__.py similarity index 100% rename from lotus/metering_billing/__init__.py rename to metering_billing/__init__.py diff --git a/lotus/metering_billing/admin.py b/metering_billing/admin.py similarity index 100% rename from lotus/metering_billing/admin.py rename to metering_billing/admin.py diff --git a/lotus/metering_billing/apps.py b/metering_billing/apps.py similarity index 100% rename from lotus/metering_billing/apps.py rename to metering_billing/apps.py diff --git a/lotus/metering_billing/auth_utils.py b/metering_billing/auth_utils.py similarity index 100% rename from lotus/metering_billing/auth_utils.py rename to metering_billing/auth_utils.py diff --git a/lotus/metering_billing/exceptions.py b/metering_billing/exceptions.py similarity index 100% rename from lotus/metering_billing/exceptions.py rename to metering_billing/exceptions.py diff --git a/lotus/metering_billing/invoice.py b/metering_billing/invoice.py similarity index 100% rename from lotus/metering_billing/invoice.py rename to metering_billing/invoice.py diff --git a/lotus/metering_billing/management/__init__.py b/metering_billing/management/__init__.py similarity index 100% rename from lotus/metering_billing/management/__init__.py rename to metering_billing/management/__init__.py diff --git a/lotus/metering_billing/management/commands/__init__.py b/metering_billing/management/commands/__init__.py similarity index 100% rename from lotus/metering_billing/management/commands/__init__.py rename to metering_billing/management/commands/__init__.py diff --git a/lotus/metering_billing/management/commands/initadmin.py b/metering_billing/management/commands/initadmin.py similarity index 100% rename from lotus/metering_billing/management/commands/initadmin.py rename to metering_billing/management/commands/initadmin.py diff --git a/lotus/metering_billing/management/commands/setup_tasks.py b/metering_billing/management/commands/setup_tasks.py similarity index 100% rename from lotus/metering_billing/management/commands/setup_tasks.py rename to metering_billing/management/commands/setup_tasks.py diff --git a/lotus/metering_billing/management/commands/setup_test_db.py b/metering_billing/management/commands/setup_test_db.py similarity index 100% rename from lotus/metering_billing/management/commands/setup_test_db.py rename to metering_billing/management/commands/setup_test_db.py diff --git a/lotus/metering_billing/management/commands/wait_for_db.py b/metering_billing/management/commands/wait_for_db.py similarity index 100% rename from lotus/metering_billing/management/commands/wait_for_db.py rename to metering_billing/management/commands/wait_for_db.py diff --git a/lotus/metering_billing/migrations/0001_initial.py b/metering_billing/migrations/0001_initial.py similarity index 100% rename from lotus/metering_billing/migrations/0001_initial.py rename to metering_billing/migrations/0001_initial.py diff --git a/lotus/metering_billing/migrations/0002_alter_billingplan_flat_rate_and_more.py b/metering_billing/migrations/0002_alter_billingplan_flat_rate_and_more.py similarity index 100% rename from lotus/metering_billing/migrations/0002_alter_billingplan_flat_rate_and_more.py rename to metering_billing/migrations/0002_alter_billingplan_flat_rate_and_more.py diff --git a/lotus/metering_billing/migrations/0003_alter_billingplan_components.py b/metering_billing/migrations/0003_alter_billingplan_components.py similarity index 100% rename from lotus/metering_billing/migrations/0003_alter_billingplan_components.py rename to metering_billing/migrations/0003_alter_billingplan_components.py diff --git a/lotus/metering_billing/migrations/0003_alter_invoice_customer_alter_invoice_line_items_and_more.py b/metering_billing/migrations/0003_alter_invoice_customer_alter_invoice_line_items_and_more.py similarity index 100% rename from lotus/metering_billing/migrations/0003_alter_invoice_customer_alter_invoice_line_items_and_more.py rename to metering_billing/migrations/0003_alter_invoice_customer_alter_invoice_line_items_and_more.py diff --git a/lotus/metering_billing/migrations/0004_alter_invoice_status_alter_organization_stripe_id.py b/metering_billing/migrations/0004_alter_invoice_status_alter_organization_stripe_id.py similarity index 100% rename from lotus/metering_billing/migrations/0004_alter_invoice_status_alter_organization_stripe_id.py rename to metering_billing/migrations/0004_alter_invoice_status_alter_organization_stripe_id.py diff --git a/lotus/metering_billing/migrations/0005_invoice_payment_intent_id.py b/metering_billing/migrations/0005_invoice_payment_intent_id.py similarity index 100% rename from lotus/metering_billing/migrations/0005_invoice_payment_intent_id.py rename to metering_billing/migrations/0005_invoice_payment_intent_id.py diff --git a/lotus/metering_billing/migrations/0006_merge_20220906_2201.py b/metering_billing/migrations/0006_merge_20220906_2201.py similarity index 100% rename from lotus/metering_billing/migrations/0006_merge_20220906_2201.py rename to metering_billing/migrations/0006_merge_20220906_2201.py diff --git a/lotus/metering_billing/migrations/0007_alter_customer_customer_id_and_more.py b/metering_billing/migrations/0007_alter_customer_customer_id_and_more.py similarity index 100% rename from lotus/metering_billing/migrations/0007_alter_customer_customer_id_and_more.py rename to metering_billing/migrations/0007_alter_customer_customer_id_and_more.py diff --git a/lotus/metering_billing/migrations/0008_alter_billablemetric_unique_together_and_more.py b/metering_billing/migrations/0008_alter_billablemetric_unique_together_and_more.py similarity index 100% rename from lotus/metering_billing/migrations/0008_alter_billablemetric_unique_together_and_more.py rename to metering_billing/migrations/0008_alter_billablemetric_unique_together_and_more.py diff --git a/lotus/metering_billing/migrations/0009_remove_subscription_next_plan_and_more.py b/metering_billing/migrations/0009_remove_subscription_next_plan_and_more.py similarity index 100% rename from lotus/metering_billing/migrations/0009_remove_subscription_next_plan_and_more.py rename to metering_billing/migrations/0009_remove_subscription_next_plan_and_more.py diff --git a/lotus/metering_billing/migrations/__init__.py b/metering_billing/migrations/__init__.py similarity index 100% rename from lotus/metering_billing/migrations/__init__.py rename to metering_billing/migrations/__init__.py diff --git a/lotus/metering_billing/models.py b/metering_billing/models.py similarity index 100% rename from lotus/metering_billing/models.py rename to metering_billing/models.py diff --git a/lotus/metering_billing/permissions.py b/metering_billing/permissions.py similarity index 100% rename from lotus/metering_billing/permissions.py rename to metering_billing/permissions.py diff --git a/lotus/metering_billing/serializers/internal_serializers.py b/metering_billing/serializers/internal_serializers.py similarity index 100% rename from lotus/metering_billing/serializers/internal_serializers.py rename to metering_billing/serializers/internal_serializers.py diff --git a/lotus/metering_billing/serializers/model_serializers.py b/metering_billing/serializers/model_serializers.py similarity index 100% rename from lotus/metering_billing/serializers/model_serializers.py rename to metering_billing/serializers/model_serializers.py diff --git a/lotus/metering_billing/tasks.py b/metering_billing/tasks.py similarity index 100% rename from lotus/metering_billing/tasks.py rename to metering_billing/tasks.py diff --git a/lotus/metering_billing/template_tags/__init__.py b/metering_billing/template_tags/__init__.py similarity index 100% rename from lotus/metering_billing/template_tags/__init__.py rename to metering_billing/template_tags/__init__.py diff --git a/lotus/metering_billing/template_tags/render_vite_bundle.py b/metering_billing/template_tags/render_vite_bundle.py similarity index 70% rename from lotus/metering_billing/template_tags/render_vite_bundle.py rename to metering_billing/template_tags/render_vite_bundle.py index 12f4f3d29..c5973c206 100644 --- a/lotus/metering_billing/template_tags/render_vite_bundle.py +++ b/metering_billing/template_tags/render_vite_bundle.py @@ -18,9 +18,9 @@ def render_vite_bundle(): Supposed to only be used in production. For development, see other files. """ - + root = f"{settings.VITE_APP_DIR}/dist" try: - fd = open(f"{settings.VITE_APP_DIR}/dist/manifest.json", "r") + fd = open(f"{root}/manifest.json", "r") manifest = json.load(fd) except: raise Exception( @@ -29,13 +29,13 @@ def render_vite_bundle(): imports_files = "".join( [ - f'' + f'' for file in manifest["index.html"]["imports"] ] ) return mark_safe( - f""" - + f""" + {imports_files}""" ) diff --git a/lotus/metering_billing/templates/index.html b/metering_billing/templates/index.html similarity index 100% rename from lotus/metering_billing/templates/index.html rename to metering_billing/templates/index.html diff --git a/lotus/metering_billing/tests/__init__.py b/metering_billing/tests/__init__.py similarity index 100% rename from lotus/metering_billing/tests/__init__.py rename to metering_billing/tests/__init__.py diff --git a/lotus/metering_billing/tests/conftest.py b/metering_billing/tests/conftest.py similarity index 100% rename from lotus/metering_billing/tests/conftest.py rename to metering_billing/tests/conftest.py diff --git a/lotus/metering_billing/tests/test_billable_metric.py b/metering_billing/tests/test_billable_metric.py similarity index 100% rename from lotus/metering_billing/tests/test_billable_metric.py rename to metering_billing/tests/test_billable_metric.py diff --git a/lotus/metering_billing/tests/test_customer.py b/metering_billing/tests/test_customer.py similarity index 100% rename from lotus/metering_billing/tests/test_customer.py rename to metering_billing/tests/test_customer.py diff --git a/lotus/metering_billing/tests/test_subscription.py b/metering_billing/tests/test_subscription.py similarity index 100% rename from lotus/metering_billing/tests/test_subscription.py rename to metering_billing/tests/test_subscription.py diff --git a/lotus/metering_billing/tests/test_tasks.py b/metering_billing/tests/test_tasks.py similarity index 100% rename from lotus/metering_billing/tests/test_tasks.py rename to metering_billing/tests/test_tasks.py diff --git a/lotus/metering_billing/tests/test_track_event.py b/metering_billing/tests/test_track_event.py similarity index 100% rename from lotus/metering_billing/tests/test_track_event.py rename to metering_billing/tests/test_track_event.py diff --git a/lotus/metering_billing/utils.py b/metering_billing/utils.py similarity index 100% rename from lotus/metering_billing/utils.py rename to metering_billing/utils.py diff --git a/lotus/metering_billing/views/auth_views.py b/metering_billing/views/auth_views.py similarity index 100% rename from lotus/metering_billing/views/auth_views.py rename to metering_billing/views/auth_views.py diff --git a/lotus/metering_billing/views/model_views.py b/metering_billing/views/model_views.py similarity index 100% rename from lotus/metering_billing/views/model_views.py rename to metering_billing/views/model_views.py diff --git a/lotus/metering_billing/views/track.py b/metering_billing/views/track.py similarity index 100% rename from lotus/metering_billing/views/track.py rename to metering_billing/views/track.py diff --git a/lotus/metering_billing/views/views.py b/metering_billing/views/views.py similarity index 83% rename from lotus/metering_billing/views/views.py rename to metering_billing/views/views.py index 57ddca205..17115894a 100644 --- a/lotus/metering_billing/views/views.py +++ b/metering_billing/views/views.py @@ -173,52 +173,57 @@ def get(self, request, format=None): "daily_usage_revenue_period_2": {}, "total_revenue_period_2": Decimal(0), } - for billable_metric in all_org_billable_metrics: + if all_org_billable_metrics.count() > 0: + for billable_metric in all_org_billable_metrics: + for p_start, p_end, p_num in [ + (p1_start, p1_end, 1), + (p2_start, p2_end, 2), + ]: + return_dict[f"daily_usage_revenue_period_{p_num}"][ + billable_metric.id + ] = { + "metric": str(billable_metric), + "data": { + str(x): Decimal(0) + for x in dates_bwn_twodates(p_start, p_end) + }, + "total_revenue": Decimal(0), + } for p_start, p_end, p_num in [(p1_start, p1_end, 1), (p2_start, p2_end, 2)]: - return_dict[f"daily_usage_revenue_period_{p_num}"][ - billable_metric.id - ] = { - "metric": str(billable_metric), - "data": { - str(x): Decimal(0) for x in dates_bwn_twodates(p_start, p_end) - }, - "total_revenue": Decimal(0), - } - for p_start, p_end, p_num in [(p1_start, p1_end, 1), (p2_start, p2_end, 2)]: - subs = Subscription.objects.filter( - Q(start_date__range=[p_start, p_end]) - | Q(end_date__range=[p_start, p_end]), - organization=organization, - ) - for sub in subs: - billing_plan = sub.billing_plan - flat_fee_billable_date = ( - sub.start_date if billing_plan.pay_in_advance else sub.end_date + subs = Subscription.objects.filter( + Q(start_date__range=[p_start, p_end]) + | Q(end_date__range=[p_start, p_end]), + organization=organization, ) - if ( - flat_fee_billable_date >= p_start - and flat_fee_billable_date <= p_end - ): - return_dict[ - f"total_revenue_period_{p_num}" - ] += billing_plan.flat_rate.amount - for plan_component in billing_plan.components.all(): - usage_cost_per_day = calculate_plan_component_daily_revenue( - sub.customer, - plan_component, - sub.start_date, - sub.end_date, - p_start, - p_end, + for sub in subs: + billing_plan = sub.billing_plan + flat_fee_billable_date = ( + sub.start_date if billing_plan.pay_in_advance else sub.end_date ) - metric_dict = return_dict[f"daily_usage_revenue_period_{p_num}"][ - plan_component.billable_metric.id - ] - for date, usage_cost in usage_cost_per_day.items(): - usage_cost = Decimal(usage_cost) - metric_dict["data"][str(date)] += usage_cost - metric_dict["total_revenue"] += usage_cost - return_dict[f"total_revenue_period_{p_num}"] += usage_cost + if ( + flat_fee_billable_date >= p_start + and flat_fee_billable_date <= p_end + ): + return_dict[ + f"total_revenue_period_{p_num}" + ] += billing_plan.flat_rate.amount + for plan_component in billing_plan.components.all(): + usage_cost_per_day = calculate_plan_component_daily_revenue( + sub.customer, + plan_component, + sub.start_date, + sub.end_date, + p_start, + p_end, + ) + metric_dict = return_dict[ + f"daily_usage_revenue_period_{p_num}" + ][plan_component.billable_metric.id] + for date, usage_cost in usage_cost_per_day.items(): + usage_cost = Decimal(usage_cost) + metric_dict["data"][str(date)] += usage_cost + metric_dict["total_revenue"] += usage_cost + return_dict[f"total_revenue_period_{p_num}"] += usage_cost for p_num in [1, 2]: dailies = return_dict[f"daily_usage_revenue_period_{p_num}"] @@ -257,7 +262,6 @@ def get(self, request, format=None): ] return_dict = {} - p1_subs = Subscription.objects.filter( Q(start_date__range=[p1_start, p1_end]) | Q(end_date__range=[p1_start, p1_end]), @@ -370,8 +374,7 @@ def get(self, request, format=None): Return current usage for a customer during a given billing period. """ organization = parse_organization(request) - customers = get_list_or_404(Customer, organization=organization) - + customers = Customer.objects.filter(organization=organization) customers_dict = {"customers": []} for customer in customers: customer_dict = {} diff --git a/frontend/nginx.conf b/nginx.conf similarity index 100% rename from frontend/nginx.conf rename to nginx.conf diff --git a/frontend/package.json b/package.json similarity index 100% rename from frontend/package.json rename to package.json diff --git a/frontend/postcss.config.cjs b/postcss.config.cjs similarity index 100% rename from frontend/postcss.config.cjs rename to postcss.config.cjs diff --git a/lotus/project_schema.yaml b/project_schema.yaml similarity index 100% rename from lotus/project_schema.yaml rename to project_schema.yaml diff --git a/frontend/public/corner_logo.svg b/public/corner_logo.svg similarity index 100% rename from frontend/public/corner_logo.svg rename to public/corner_logo.svg diff --git a/frontend/public/logo_large.png b/public/logo_large.png similarity index 100% rename from frontend/public/logo_large.png rename to public/logo_large.png diff --git a/frontend/public/logo_small256.jpg b/public/logo_small256.jpg similarity index 100% rename from frontend/public/logo_small256.jpg rename to public/logo_small256.jpg diff --git a/frontend/public/vite.svg b/public/vite.svg similarity index 100% rename from frontend/public/vite.svg rename to public/vite.svg diff --git a/lotus/pytest.ini b/pytest.ini similarity index 100% rename from lotus/pytest.ini rename to pytest.ini diff --git a/lotus/requirements.txt b/requirements.txt similarity index 100% rename from lotus/requirements.txt rename to requirements.txt diff --git a/frontend/src/.env.prod b/src/.env.prod similarity index 100% rename from frontend/src/.env.prod rename to src/.env.prod diff --git a/frontend/src/App.css b/src/App.css similarity index 100% rename from frontend/src/App.css rename to src/App.css diff --git a/frontend/src/App.tsx b/src/App.tsx similarity index 100% rename from frontend/src/App.tsx rename to src/App.tsx diff --git a/frontend/src/Data.tsx b/src/Data.tsx similarity index 100% rename from frontend/src/Data.tsx rename to src/Data.tsx diff --git a/frontend/src/api/api.ts b/src/api/api.ts similarity index 100% rename from frontend/src/api/api.ts rename to src/api/api.ts diff --git a/frontend/src/assets/images/connectwithstripe.svg b/src/assets/images/connectwithstripe.svg similarity index 100% rename from frontend/src/assets/images/connectwithstripe.svg rename to src/assets/images/connectwithstripe.svg diff --git a/frontend/src/assets/images/corner_logo.svg b/src/assets/images/corner_logo.svg similarity index 100% rename from frontend/src/assets/images/corner_logo.svg rename to src/assets/images/corner_logo.svg diff --git a/frontend/src/assets/images/logo_large.jpg b/src/assets/images/logo_large.jpg similarity index 100% rename from frontend/src/assets/images/logo_large.jpg rename to src/assets/images/logo_large.jpg diff --git a/frontend/src/assets/images/logo_small.svg b/src/assets/images/logo_small.svg similarity index 100% rename from frontend/src/assets/images/logo_small.svg rename to src/assets/images/logo_small.svg diff --git a/frontend/src/components/CreateCustomerForm.tsx b/src/components/CreateCustomerForm.tsx similarity index 100% rename from frontend/src/components/CreateCustomerForm.tsx rename to src/components/CreateCustomerForm.tsx diff --git a/frontend/src/components/CreateMetricForm.tsx b/src/components/CreateMetricForm.tsx similarity index 100% rename from frontend/src/components/CreateMetricForm.tsx rename to src/components/CreateMetricForm.tsx diff --git a/frontend/src/components/CustomerDetail.tsx b/src/components/CustomerDetail.tsx similarity index 100% rename from frontend/src/components/CustomerDetail.tsx rename to src/components/CustomerDetail.tsx diff --git a/frontend/src/components/CustomerTable.tsx b/src/components/CustomerTable.tsx similarity index 100% rename from frontend/src/components/CustomerTable.tsx rename to src/components/CustomerTable.tsx diff --git a/frontend/src/components/Dashboard/Dashboard.tsx b/src/components/Dashboard/Dashboard.tsx similarity index 100% rename from frontend/src/components/Dashboard/Dashboard.tsx rename to src/components/Dashboard/Dashboard.tsx diff --git a/frontend/src/components/Dashboard/MetricBarGraph.tsx b/src/components/Dashboard/MetricBarGraph.tsx similarity index 100% rename from frontend/src/components/Dashboard/MetricBarGraph.tsx rename to src/components/Dashboard/MetricBarGraph.tsx diff --git a/frontend/src/components/Dashboard/RevenueBarGraph.tsx b/src/components/Dashboard/RevenueBarGraph.tsx similarity index 100% rename from frontend/src/components/Dashboard/RevenueBarGraph.tsx rename to src/components/Dashboard/RevenueBarGraph.tsx diff --git a/frontend/src/components/Dashboard/RevenueDisplay.tsx b/src/components/Dashboard/RevenueDisplay.tsx similarity index 100% rename from frontend/src/components/Dashboard/RevenueDisplay.tsx rename to src/components/Dashboard/RevenueDisplay.tsx diff --git a/frontend/src/components/Dashboard/SubscriptionStatistics.tsx b/src/components/Dashboard/SubscriptionStatistics.tsx similarity index 100% rename from frontend/src/components/Dashboard/SubscriptionStatistics.tsx rename to src/components/Dashboard/SubscriptionStatistics.tsx diff --git a/frontend/src/components/LoadingSpinner.tsx b/src/components/LoadingSpinner.tsx similarity index 100% rename from frontend/src/components/LoadingSpinner.tsx rename to src/components/LoadingSpinner.tsx diff --git a/frontend/src/components/MetricTable.tsx b/src/components/MetricTable.tsx similarity index 100% rename from frontend/src/components/MetricTable.tsx rename to src/components/MetricTable.tsx diff --git a/frontend/src/components/PlanDisplayBasic.tsx b/src/components/PlanDisplayBasic.tsx similarity index 100% rename from frontend/src/components/PlanDisplayBasic.tsx rename to src/components/PlanDisplayBasic.tsx diff --git a/frontend/src/components/Seperator.tsx b/src/components/Seperator.tsx similarity index 100% rename from frontend/src/components/Seperator.tsx rename to src/components/Seperator.tsx diff --git a/frontend/src/components/Settings.css b/src/components/Settings.css similarity index 100% rename from frontend/src/components/Settings.css rename to src/components/Settings.css diff --git a/frontend/src/components/Settings.tsx b/src/components/Settings.tsx similarity index 100% rename from frontend/src/components/Settings.tsx rename to src/components/Settings.tsx diff --git a/frontend/src/components/SideBar.css b/src/components/SideBar.css similarity index 100% rename from frontend/src/components/SideBar.css rename to src/components/SideBar.css diff --git a/frontend/src/components/SideBar.tsx b/src/components/SideBar.tsx similarity index 100% rename from frontend/src/components/SideBar.tsx rename to src/components/SideBar.tsx diff --git a/frontend/src/components/UsageComponentForm.tsx b/src/components/UsageComponentForm.tsx similarity index 100% rename from frontend/src/components/UsageComponentForm.tsx rename to src/components/UsageComponentForm.tsx diff --git a/frontend/src/config/Routes.tsx b/src/config/Routes.tsx similarity index 100% rename from frontend/src/config/Routes.tsx rename to src/config/Routes.tsx diff --git a/frontend/src/index.css b/src/index.css similarity index 100% rename from frontend/src/index.css rename to src/index.css diff --git a/frontend/src/index.html b/src/index.html similarity index 100% rename from frontend/src/index.html rename to src/index.html diff --git a/frontend/src/main.tsx b/src/main.tsx similarity index 100% rename from frontend/src/main.tsx rename to src/main.tsx diff --git a/frontend/src/pages/CreatePlan.tsx b/src/pages/CreatePlan.tsx similarity index 100% rename from frontend/src/pages/CreatePlan.tsx rename to src/pages/CreatePlan.tsx diff --git a/frontend/src/pages/Login.css b/src/pages/Login.css similarity index 100% rename from frontend/src/pages/Login.css rename to src/pages/Login.css diff --git a/frontend/src/pages/Login.tsx b/src/pages/Login.tsx similarity index 100% rename from frontend/src/pages/Login.tsx rename to src/pages/Login.tsx diff --git a/frontend/src/pages/StripeRedirect.tsx b/src/pages/StripeRedirect.tsx similarity index 100% rename from frontend/src/pages/StripeRedirect.tsx rename to src/pages/StripeRedirect.tsx diff --git a/frontend/src/pages/ViewCustomers.tsx b/src/pages/ViewCustomers.tsx similarity index 100% rename from frontend/src/pages/ViewCustomers.tsx rename to src/pages/ViewCustomers.tsx diff --git a/frontend/src/pages/ViewMetrics.tsx b/src/pages/ViewMetrics.tsx similarity index 100% rename from frontend/src/pages/ViewMetrics.tsx rename to src/pages/ViewMetrics.tsx diff --git a/frontend/src/pages/ViewPlans.tsx b/src/pages/ViewPlans.tsx similarity index 100% rename from frontend/src/pages/ViewPlans.tsx rename to src/pages/ViewPlans.tsx diff --git a/frontend/src/pages/ViewSettings.tsx b/src/pages/ViewSettings.tsx similarity index 100% rename from frontend/src/pages/ViewSettings.tsx rename to src/pages/ViewSettings.tsx diff --git a/frontend/src/pages/ViewSubscriptions.tsx b/src/pages/ViewSubscriptions.tsx similarity index 100% rename from frontend/src/pages/ViewSubscriptions.tsx rename to src/pages/ViewSubscriptions.tsx diff --git a/frontend/src/types/customer-type.ts b/src/types/customer-type.ts similarity index 100% rename from frontend/src/types/customer-type.ts rename to src/types/customer-type.ts diff --git a/frontend/src/types/metric-type.ts b/src/types/metric-type.ts similarity index 100% rename from frontend/src/types/metric-type.ts rename to src/types/metric-type.ts diff --git a/frontend/src/types/plan-type.ts b/src/types/plan-type.ts similarity index 100% rename from frontend/src/types/plan-type.ts rename to src/types/plan-type.ts diff --git a/frontend/src/types/revenue-type.ts b/src/types/revenue-type.ts similarity index 100% rename from frontend/src/types/revenue-type.ts rename to src/types/revenue-type.ts diff --git a/frontend/src/types/stripe-type.ts b/src/types/stripe-type.ts similarity index 100% rename from frontend/src/types/stripe-type.ts rename to src/types/stripe-type.ts diff --git a/frontend/src/types/subscription-type.ts b/src/types/subscription-type.ts similarity index 100% rename from frontend/src/types/subscription-type.ts rename to src/types/subscription-type.ts diff --git a/frontend/src/vite-env.d.ts b/src/vite-env.d.ts similarity index 100% rename from frontend/src/vite-env.d.ts rename to src/vite-env.d.ts diff --git a/frontend/tailwind.config.cjs b/tailwind.config.cjs similarity index 100% rename from frontend/tailwind.config.cjs rename to tailwind.config.cjs diff --git a/frontend/tsconfig.json b/tsconfig.json similarity index 100% rename from frontend/tsconfig.json rename to tsconfig.json diff --git a/frontend/tsconfig.node.json b/tsconfig.node.json similarity index 100% rename from frontend/tsconfig.node.json rename to tsconfig.node.json diff --git a/frontend/vite.config.ts b/vite.config.ts similarity index 100% rename from frontend/vite.config.ts rename to vite.config.ts diff --git a/frontend/yarn.lock b/yarn.lock similarity index 100% rename from frontend/yarn.lock rename to yarn.lock