Commit 640870e4 authored by David Seaward's avatar David Seaward

convert Service and ExternalParser to enums

parent c995f11e
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2017-12-08 13:27
from __future__ import unicode_literals
import choicesenum.django.fields
from django.db import migrations
import limitmonitor.models
class Migration(migrations.Migration):
dependencies = [
('limitmonitor', '0002_auto_20171009_2334'),
]
operations = [
migrations.AlterField(
model_name='externalbundle',
name='parser',
field=choicesenum.django.fields.EnumIntegerField(default=limitmonitor.models.ExternalParser(0),
enum=limitmonitor.models.ExternalParser),
),
migrations.AlterField(
model_name='externalbundle',
name='service',
field=choicesenum.django.fields.EnumIntegerField(default=limitmonitor.models.Service(0),
enum=limitmonitor.models.Service),
),
migrations.AlterField(
model_name='externalcredit',
name='parser',
field=choicesenum.django.fields.EnumIntegerField(default=limitmonitor.models.ExternalParser(0),
enum=limitmonitor.models.ExternalParser),
),
migrations.AlterField(
model_name='limit',
name='service',
field=choicesenum.django.fields.EnumIntegerField(default=limitmonitor.models.Service(0),
enum=limitmonitor.models.Service),
),
]
from choicesenum import ChoicesEnum
from choicesenum.django.fields import EnumIntegerField
from django.conf import settings
from django.db import models
from django.utils import timezone
EXTERNAL_PARSER_CHOICES = (
("WOO1", "WooCommerce v1"),
("WOOSUB1", "WooCommerce Subscription v1"),
)
SERVICE_CHOICES = (
("TUNNEL", "Tunnel"),
("COMMUNICATION", "Communication"),
)
class ExternalParser(ChoicesEnum):
UNDEFINED = 0, "Undefined"
WOO1 = 1, "WooCommerce v1"
WOOSUB1 = 2, "WooCommerce Subscription v1"
class Service(ChoicesEnum):
UNDEFINED = 0, "Undefined"
TUNNEL = 1, "Tunnel"
COMMUNICATION = 2, "Communication"
def create_missing_user_limits(user):
for code, label in SERVICE_CHOICES:
if not Limit.objects.filter(user=user, service=code).exists():
for code, label in Service.choices():
is_undefined = code == Service.UNDEFINED.value
is_exists = Limit.objects.filter(user=user, service=code).exists()
if not is_undefined and not is_exists:
Limit(user=user, service=code).save()
class Limit(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
service = models.CharField(max_length=30, choices=SERVICE_CHOICES)
service = EnumIntegerField(enum=Service, default=Service.UNDEFINED)
renewal_date = models.DateTimeField(default=None, blank=True, null=True)
expiry_date = models.DateTimeField(default=None, blank=True, null=True)
volume_total = models.DecimalField(default=0, decimal_places=2, max_digits=6)
......@@ -33,7 +39,7 @@ class Limit(models.Model):
def service_label(self):
label = "None"
for key, value in SERVICE_CHOICES:
for key, value in Service.choices():
if self.service == key:
label = value
......@@ -106,9 +112,9 @@ class Limit(models.Model):
class ExternalBundle(models.Model):
parser = models.CharField(max_length=30, choices=EXTERNAL_PARSER_CHOICES)
parser = EnumIntegerField(enum=ExternalParser, default=ExternalParser.UNDEFINED)
external_key = models.CharField(max_length=30)
service = models.CharField(max_length=30, choices=SERVICE_CHOICES)
service = EnumIntegerField(enum=Service, default=Service.UNDEFINED)
time_credit = models.DecimalField(default=0, decimal_places=2, max_digits=6)
volume_credit = models.DecimalField(default=0, decimal_places=2, max_digits=6)
created_date = models.DateTimeField(default=timezone.now)
......@@ -116,7 +122,7 @@ class ExternalBundle(models.Model):
class ExternalCredit(models.Model):
parser = models.CharField(max_length=30, choices=EXTERNAL_PARSER_CHOICES)
parser = EnumIntegerField(enum=ExternalParser, default=ExternalParser.UNDEFINED)
external_key = models.CharField(max_length=30)
label = models.CharField(max_length=30)
bundle_key = models.CharField(max_length=30)
......
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2017-12-08 13:27
from __future__ import unicode_literals
from django.db import migrations, models
import purist.custom
class Migration(migrations.Migration):
dependencies = [
('purist', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='user',
name='username',
field=models.CharField(error_messages={'unique': 'A user with that username already exists.'},
help_text='Required. Start with a letter, followed by letters and numbers.',
max_length=150, unique=True, validators=[purist.custom.UsernameValidator()],
verbose_name='username'),
),
]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment