v3: link product to resource

This commit is contained in:
Nico Schottelius 2022-01-02 19:34:55 +01:00
parent 27462a4d0d
commit 42e200b564
16 changed files with 237 additions and 43 deletions

View file

@ -1 +1,2 @@
venv
.env

View file

@ -1,3 +1,12 @@
from django.contrib import admin
# Register your models here.
from .models import *
for m in [
Order,
Product,
Resource,
TimeFrame,
]:
admin.site.register(m)

View file

@ -0,0 +1,43 @@
# Generated by Django 4.0 on 2022-01-02 18:06
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
class Migration(migrations.Migration):
initial = True
dependencies = [
('uauth', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Product',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=128, unique=True)),
],
),
migrations.CreateModel(
name='TimeFrame',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=128, unique=True)),
('seconds', models.IntegerField(blank=True, null=True)),
],
),
migrations.CreateModel(
name='Order',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('creation_date', models.DateTimeField(auto_now_add=True)),
('starting_date', models.DateTimeField(default=django.utils.timezone.now)),
('ending_date', models.DateTimeField(blank=True, null=True)),
('owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to='uauth.user')),
('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.product')),
],
),
]

View file

@ -0,0 +1,27 @@
# Generated by Django 4.0 on 2022-01-02 18:28
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('app', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Resource',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=128, unique=True)),
('unit', models.CharField(max_length=128, unique=True)),
('minimum_units', models.FloatField(blank=True, null=True)),
('maximum_units', models.FloatField(blank=True, null=True)),
('step_size', models.FloatField(default=1)),
('price', models.FloatField()),
('timeframe', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.timeframe')),
],
),
]

View file

@ -0,0 +1,18 @@
# Generated by Django 4.0 on 2022-01-02 18:33
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('app', '0002_resource'),
]
operations = [
migrations.AddField(
model_name='product',
name='resources',
field=models.ManyToManyField(to='app.Resource'),
),
]

View file

@ -1,3 +1,68 @@
from django.db import models
from django.contrib.auth import get_user_model
from django.utils import timezone
# Create your models here.
class TimeFrame(models.Model):
name = models.CharField(max_length=128, unique=True)
seconds = models.IntegerField(null=True, blank=True)
@staticmethod
def secs_to_name(secs):
name = ""
days = 0
hours = 0
if secs >= 24*3600:
days = secs // (24*3600)
secs -= (days*24*3600)
if secs >= 3600:
hours = secs // 3600
secs -= hours*3600
return f"{days} days {hours} hours {secs} seconds"
def __str__(self):
return "{} ({})".format(self.name, self.secs_to_name(self.seconds))
class Resource(models.Model):
name = models.CharField(max_length=128, unique=True) # CPU, RAM
unit = models.CharField(max_length=128, unique=True) # Count, GB
minimum_units = models.FloatField(null=True, blank=True) # might have min
maximum_units = models.FloatField(null=True, blank=True) # might have max
step_size = models.FloatField(default=1) # might/must step size
timeframe = models.ForeignKey(TimeFrame, on_delete=models.CASCADE)
price = models.FloatField() # Per unit and per time frame
def __str__(self):
return f"{self.name}: {self.minimum_units}-{self.maximum_units} (+/-){self.step_size} {self.unit} {self.price}/{self.timeframe}"
class Product(models.Model):
"""
Describes a product a user can buy
"""
name = models.CharField(max_length=128, unique=True)
# textconfig = models.ManyToManyField(ProductTextConfiguration)
# textfieldconfig = models.ManyToManyField(ProductTextFieldConfiguration)
# timeframes = models.ManyToManyField(TimeFrame)
resources = models.ManyToManyField(Resource)
def __str__(self):
return self.name
class Order(models.Model):
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, editable=False)
creation_date = models.DateTimeField(auto_now_add=True)
starting_date = models.DateTimeField(default=timezone.now)
ending_date = models.DateTimeField(blank=True, null=True)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
#resourceconfigs = models.ManyToManyField(ResourceConfig)
#textconfigs = models.ManyToManyField(ResourceConfig)

View file

View file

@ -0,0 +1,6 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import User
admin.site.register(User, UserAdmin)

6
uncloud_v3/uauth/apps.py Normal file
View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class UauthConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'uauth'

View file

@ -0,0 +1,44 @@
# Generated by Django 4.0 on 2022-01-02 18:03
import django.contrib.auth.models
import django.contrib.auth.validators
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
initial = True
dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
]
operations = [
migrations.CreateModel(
name='User',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')),
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
],
options={
'verbose_name': 'user',
'verbose_name_plural': 'users',
'abstract': False,
},
managers=[
('objects', django.contrib.auth.models.UserManager()),
],
),
]

View file

View file

@ -0,0 +1,7 @@
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
"""
Custom class base
"""

View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View file

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

View file

@ -38,6 +38,8 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app',
'uauth'
]
MIDDLEWARE = [
@ -100,6 +102,7 @@ AUTH_PASSWORD_VALIDATORS = [
},
]
AUTH_USER_MODEL = 'uauth.User'
# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/

View file

@ -19,47 +19,9 @@ class Nextcloudv1(models.Model):
storage_in_gb = models.FloatField(null=True, blank=True)
def __str__(self):
return f"Nextcloud {domain}: {cores}C {ram_in_gb}GB RAM {storage} GB storage from {owner}"
class Order(models.Model):
# name = models.CharField(max_length=128, unique=True)
owner = models.ForeignKey(
get_user_model(), on_delete=models.CASCADE, editable=False
)
creation_date = models.DateTimeField(auto_now_add=True)
starting_date = models.DateTimeField(default=timezone.now)
ending_date = models.DateTimeField(blank=True, null=True)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
resourceconfigs = models.ManyToManyField(ResourceConfig)
textconfigs = models.ManyToManyField(ResourceConfig)
class Product(models.Model):
"""
Describes a product a user can buy, its pricings and its configurations
"""
name = models.CharField(max_length=128, unique=True) # Nextcloud, Matrix, etc.
textconfig = models.ManyToManyField(ProductTextConfiguration)
textfieldconfig = models.ManyToManyField(ProductTextFieldConfiguration)
timeframes = models.ManyToManyField(TimeFrame)
resources = models.ManyToManyField(Resource)
class Resource(models.Model):
name = models.CharField(max_length=128, unique=True) # CPU, RAM
unit = models.CharField(max_length=128, unique=True) # Count, GB
minimum_units = models.FloatField(null=True, blank=True) # might have min
maximum_units = models.FloatField(null=True, blank=True) # might have max
step_size = models.FloatField(default=1, null=True, blank=True) # might/must step size
timeframe = models.ForeignKey(TimeFrame, on_delete=models.CASCADE)
price = models.FloatField() # Per unit and per time frame
class ResourceConfig(models.Model):
"""
@ -89,9 +51,6 @@ class ProductTextFieldConfig(models.Model):
value = models.TextField()
config = models.ForeignKey(ProductTextFieldConfiguration, on_delete=models.CASCADE)
class TimeFrame(models.Model):
name = models.CharField(max_length=128, unique=True)
duration_in_seconds = models.IntegerField(null=True, blank=True)
# 3600
# 86400