From c45635505927da610deaba4e0b6a7de573502a48 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Fri, 21 Feb 2020 10:41:22 +0100 Subject: [PATCH] begin to introduce product Signed-off-by: Nico Schottelius --- hack.org | 0 nicohack202002/uncloud/api/models.py | 6 -- nicohack202002/uncloud/uncloud/settings.py | 6 +- nicohack202002/uncloud/uncloud/stripe.py | 55 +++++++++++++++++++ nicohack202002/uncloud/uncloud/urls.py | 2 +- .../uncloud/{api => uncloud_api}/__init__.py | 0 .../uncloud/{api => uncloud_api}/admin.py | 0 .../uncloud/{api => uncloud_api}/apps.py | 2 +- .../uncloud_api/migrations/0001_initial.py | 34 ++++++++++++ .../migrations/__init__.py | 0 nicohack202002/uncloud/uncloud_api/models.py | 30 ++++++++++ .../{api => uncloud_api}/serializers.py | 0 .../uncloud/{api => uncloud_api}/tests.py | 0 .../uncloud/{api => uncloud_api}/views.py | 2 +- notes-nico.org | 21 ++++++- 15 files changed, 147 insertions(+), 11 deletions(-) delete mode 100644 hack.org delete mode 100644 nicohack202002/uncloud/api/models.py create mode 100644 nicohack202002/uncloud/uncloud/stripe.py rename nicohack202002/uncloud/{api => uncloud_api}/__init__.py (100%) rename nicohack202002/uncloud/{api => uncloud_api}/admin.py (100%) rename nicohack202002/uncloud/{api => uncloud_api}/apps.py (71%) create mode 100644 nicohack202002/uncloud/uncloud_api/migrations/0001_initial.py rename nicohack202002/uncloud/{api => uncloud_api}/migrations/__init__.py (100%) create mode 100644 nicohack202002/uncloud/uncloud_api/models.py rename nicohack202002/uncloud/{api => uncloud_api}/serializers.py (100%) rename nicohack202002/uncloud/{api => uncloud_api}/tests.py (100%) rename nicohack202002/uncloud/{api => uncloud_api}/views.py (94%) diff --git a/hack.org b/hack.org deleted file mode 100644 index e69de29..0000000 diff --git a/nicohack202002/uncloud/api/models.py b/nicohack202002/uncloud/api/models.py deleted file mode 100644 index 7288ecf..0000000 --- a/nicohack202002/uncloud/api/models.py +++ /dev/null @@ -1,6 +0,0 @@ -from django.db import models - -# Create your models here. - -class CreditCard(models.Model): - pass diff --git a/nicohack202002/uncloud/uncloud/settings.py b/nicohack202002/uncloud/uncloud/settings.py index 91bcf47..d6cbb0e 100644 --- a/nicohack202002/uncloud/uncloud/settings.py +++ b/nicohack202002/uncloud/uncloud/settings.py @@ -37,7 +37,8 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', - 'rest_framework' + 'rest_framework', + 'uncloud_api' ] MIDDLEWARE = [ @@ -146,3 +147,6 @@ STATIC_URL = '/static/' # Uncommitted file import uncloud.secrets + +import stripe +stripe.api_key = uncloud.secrets.STRIPE_KEY diff --git a/nicohack202002/uncloud/uncloud/stripe.py b/nicohack202002/uncloud/uncloud/stripe.py new file mode 100644 index 0000000..ce35fd9 --- /dev/null +++ b/nicohack202002/uncloud/uncloud/stripe.py @@ -0,0 +1,55 @@ +import stripe + +def handle_stripe_error(f): + def handle_problems(*args, **kwargs): + response = { + 'paid': False, + 'response_object': None, + 'error': None + } + + common_message = "Currently it's not possible to make payments." + try: + response_object = f(*args, **kwargs) + response = { + 'response_object': response_object, + 'error': None + } + return response + except stripe.error.CardError as e: + # Since it's a decline, stripe.error.CardError will be caught + body = e.json_body + err = body['error'] + response.update({'error': err['message']}) + logging.error(str(e)) + return response + except stripe.error.RateLimitError: + response.update( + {'error': "Too many requests made to the API too quickly"}) + return response + except stripe.error.InvalidRequestError as e: + logging.error(str(e)) + response.update({'error': "Invalid parameters"}) + return response + except stripe.error.AuthenticationError as e: + # Authentication with Stripe's API failed + # (maybe you changed API keys recently) + logging.error(str(e)) + response.update({'error': common_message}) + return response + except stripe.error.APIConnectionError as e: + logging.error(str(e)) + response.update({'error': common_message}) + return response + except stripe.error.StripeError as e: + # maybe send email + logging.error(str(e)) + response.update({'error': common_message}) + return response + except Exception as e: + # maybe send email + logging.error(str(e)) + response.update({'error': common_message}) + return response + + return handle_problems diff --git a/nicohack202002/uncloud/uncloud/urls.py b/nicohack202002/uncloud/uncloud/urls.py index e52fd35..e0a0b61 100644 --- a/nicohack202002/uncloud/uncloud/urls.py +++ b/nicohack202002/uncloud/uncloud/urls.py @@ -17,7 +17,7 @@ from django.contrib import admin from django.urls import path, include from rest_framework import routers -from api import views +from uncloud_api import views router = routers.DefaultRouter() router.register(r'users', views.UserViewSet) diff --git a/nicohack202002/uncloud/api/__init__.py b/nicohack202002/uncloud/uncloud_api/__init__.py similarity index 100% rename from nicohack202002/uncloud/api/__init__.py rename to nicohack202002/uncloud/uncloud_api/__init__.py diff --git a/nicohack202002/uncloud/api/admin.py b/nicohack202002/uncloud/uncloud_api/admin.py similarity index 100% rename from nicohack202002/uncloud/api/admin.py rename to nicohack202002/uncloud/uncloud_api/admin.py diff --git a/nicohack202002/uncloud/api/apps.py b/nicohack202002/uncloud/uncloud_api/apps.py similarity index 71% rename from nicohack202002/uncloud/api/apps.py rename to nicohack202002/uncloud/uncloud_api/apps.py index d87006d..6830fa2 100644 --- a/nicohack202002/uncloud/api/apps.py +++ b/nicohack202002/uncloud/uncloud_api/apps.py @@ -2,4 +2,4 @@ from django.apps import AppConfig class ApiConfig(AppConfig): - name = 'api' + name = 'uncloud_api' diff --git a/nicohack202002/uncloud/uncloud_api/migrations/0001_initial.py b/nicohack202002/uncloud/uncloud_api/migrations/0001_initial.py new file mode 100644 index 0000000..7248a66 --- /dev/null +++ b/nicohack202002/uncloud/uncloud_api/migrations/0001_initial.py @@ -0,0 +1,34 @@ +# Generated by Django 3.0.3 on 2020-02-21 09:40 + +from django.db import migrations, models +import django.db.models.deletion +import uuid + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Product', + fields=[ + ('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('name', models.CharField(max_length=256)), + ('recurring_period', models.CharField(choices=[('per_year', 'Per Year'), ('per_month', 'Per Month'), ('per_week', 'Per Week'), ('per_day', 'Per Day'), ('per_hour', 'Per Hour'), ('not_recurring', 'Not recurring')], default='not_recurring', max_length=256)), + ], + ), + migrations.CreateModel( + name='Feature', + fields=[ + ('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('name', models.CharField(max_length=256)), + ('recurring_price', models.FloatField(default=0)), + ('one_time_price', models.FloatField()), + ('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_api.Product')), + ], + ), + ] diff --git a/nicohack202002/uncloud/api/migrations/__init__.py b/nicohack202002/uncloud/uncloud_api/migrations/__init__.py similarity index 100% rename from nicohack202002/uncloud/api/migrations/__init__.py rename to nicohack202002/uncloud/uncloud_api/migrations/__init__.py diff --git a/nicohack202002/uncloud/uncloud_api/models.py b/nicohack202002/uncloud/uncloud_api/models.py new file mode 100644 index 0000000..2dca8ea --- /dev/null +++ b/nicohack202002/uncloud/uncloud_api/models.py @@ -0,0 +1,30 @@ +from django.db import models +import uuid + +class Product(models.Model): + uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) + name = models.CharField(max_length=256) + + recurring_period = models.CharField(max_length=256, + choices = ( + ("per_year", "Per Year"), + ("per_month", "Per Month"), + ("per_week", "Per Week"), + ("per_day", "Per Day"), + ("per_hour", "Per Hour"), + ("not_recurring", "Not recurring") + ), + default="not_recurring" + ) + + + + +class Feature(models.Model): + uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) + name = models.CharField(max_length=256) + + recurring_price = models.FloatField(default=0) + one_time_price = models.FloatField() + + product = models.ForeignKey(Product, on_delete=models.CASCADE) diff --git a/nicohack202002/uncloud/api/serializers.py b/nicohack202002/uncloud/uncloud_api/serializers.py similarity index 100% rename from nicohack202002/uncloud/api/serializers.py rename to nicohack202002/uncloud/uncloud_api/serializers.py diff --git a/nicohack202002/uncloud/api/tests.py b/nicohack202002/uncloud/uncloud_api/tests.py similarity index 100% rename from nicohack202002/uncloud/api/tests.py rename to nicohack202002/uncloud/uncloud_api/tests.py diff --git a/nicohack202002/uncloud/api/views.py b/nicohack202002/uncloud/uncloud_api/views.py similarity index 94% rename from nicohack202002/uncloud/api/views.py rename to nicohack202002/uncloud/uncloud_api/views.py index c9b1e57..9310d8b 100644 --- a/nicohack202002/uncloud/api/views.py +++ b/nicohack202002/uncloud/uncloud_api/views.py @@ -5,7 +5,7 @@ from django.shortcuts import render from django.contrib.auth.models import User, Group from rest_framework import viewsets, permissions -from api.serializers import UserSerializer, GroupSerializer +from .serializers import UserSerializer, GroupSerializer class CreditCardViewSet(viewsets.ModelViewSet): diff --git a/notes-nico.org b/notes-nico.org index e2b8cac..21102f9 100644 --- a/notes-nico.org +++ b/notes-nico.org @@ -1,5 +1,21 @@ +* snapshot feature +** product: vm-snapshot +* steps +** DONE authenticate via ldap + CLOSED: [2020-02-20 Thu 19:05] +** DONE Make classes / views require authentication + CLOSED: [2020-02-20 Thu 19:05] +** TODO register credit card +*** TODO find out what saving with us +*** Info +**** should not be fully saved in the DB +**** model needs to be a bit different +* Decide where to save sensitive data +** stripe access key, etc. * python requirements (nicohack202002) - django djangorestframework + django djangorestframework django-auth-ldap stripe +* os package requirements (alpine) + openldap-dev * VPN case ** put on /orders with uuid ** register cc @@ -21,3 +37,6 @@ *** Is for free if the customer has an active VM ** IPv6 only VM *** Parameters: cores, ram, os_disk_size, OS +* Django rest framework +** viewset: .list and .create +** view: .get .post