begin to introduce product

Signed-off-by: Nico Schottelius <nico@nico-notebook.schottelius.org>
This commit is contained in:
Nico Schottelius 2020-02-21 10:41:22 +01:00
parent 118c66799c
commit c456355059
15 changed files with 147 additions and 11 deletions

View File

View File

@ -1,6 +0,0 @@
from django.db import models
# Create your models here.
class CreditCard(models.Model):
pass

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -2,4 +2,4 @@ from django.apps import AppConfig
class ApiConfig(AppConfig):
name = 'api'
name = 'uncloud_api'

View File

@ -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')),
],
),
]

View File

@ -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)

View File

@ -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):

View File

@ -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