Commit 392b2526 authored by David Seaward's avatar David Seaward

* replace parser enum with custom map

* define Container abstraction for new maps
parent 909430eb
...@@ -17,8 +17,8 @@ class Migration(migrations.Migration): ...@@ -17,8 +17,8 @@ class Migration(migrations.Migration):
migrations.AlterField( migrations.AlterField(
model_name='externalbundle', model_name='externalbundle',
name='parser', name='parser',
field=choicesenum.django.fields.EnumIntegerField(default=limitmonitor.models.ExternalParser(0), field=choicesenum.django.fields.EnumIntegerField(default=limitmonitor.models.Legacy(0),
enum=limitmonitor.models.ExternalParser), enum=limitmonitor.models.Legacy),
), ),
migrations.AlterField( migrations.AlterField(
model_name='externalbundle', model_name='externalbundle',
...@@ -29,8 +29,8 @@ class Migration(migrations.Migration): ...@@ -29,8 +29,8 @@ class Migration(migrations.Migration):
migrations.AlterField( migrations.AlterField(
model_name='externalcredit', model_name='externalcredit',
name='parser', name='parser',
field=choicesenum.django.fields.EnumIntegerField(default=limitmonitor.models.ExternalParser(0), field=choicesenum.django.fields.EnumIntegerField(default=limitmonitor.models.Legacy(0),
enum=limitmonitor.models.ExternalParser), enum=limitmonitor.models.Legacy),
), ),
migrations.AlterField( migrations.AlterField(
model_name='limit', model_name='limit',
......
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2018-01-09 12:11
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('limitmonitor', '0004_auto_20180109_1139'),
]
operations = [
migrations.AlterField(
model_name='externalbundle',
name='parser',
field=models.IntegerField(choices=[(0, 'Undefined')], default=0),
),
migrations.AlterField(
model_name='externalcredit',
name='parser',
field=models.IntegerField(choices=[(0, 'Undefined')], default=0),
),
]
from choicesenum import ChoicesEnum from choicesenum import ChoicesEnum
from choicesenum.django.fields import EnumIntegerField
from django.conf import settings from django.conf import settings
from django.db import models from django.db import models
from django.utils import timezone from django.utils import timezone
...@@ -9,12 +8,6 @@ class Legacy(ChoicesEnum): ...@@ -9,12 +8,6 @@ class Legacy(ChoicesEnum):
UNDEFINED = 0, "Undefined" UNDEFINED = 0, "Undefined"
class ExternalParser(ChoicesEnum):
UNDEFINED = 0, "Undefined"
WOO1 = 1, "WooCommerce v1"
WOOSUB1 = 2, "WooCommerce Subscription v1"
def create_missing_user_limits(user): def create_missing_user_limits(user):
for code in settings.LM_SERVICES.MAP.keys(): for code in settings.LM_SERVICES.MAP.keys():
is_defined = code != settings.LM_SERVICES.UNDEFINED is_defined = code != settings.LM_SERVICES.UNDEFINED
...@@ -112,7 +105,8 @@ class Limit(models.Model): ...@@ -112,7 +105,8 @@ class Limit(models.Model):
class ExternalBundle(models.Model): class ExternalBundle(models.Model):
parser = EnumIntegerField(enum=ExternalParser, default=ExternalParser.UNDEFINED) parser = models.IntegerField(default=settings.LM_PARSERS.UNDEFINED,
choices=settings.LM_PARSERS.choices())
external_key = models.CharField(max_length=30) external_key = models.CharField(max_length=30)
service = models.IntegerField(default=settings.LM_SERVICES.UNDEFINED, service = models.IntegerField(default=settings.LM_SERVICES.UNDEFINED,
choices=settings.LM_SERVICES.choices()) choices=settings.LM_SERVICES.choices())
...@@ -123,7 +117,8 @@ class ExternalBundle(models.Model): ...@@ -123,7 +117,8 @@ class ExternalBundle(models.Model):
class ExternalCredit(models.Model): class ExternalCredit(models.Model):
parser = EnumIntegerField(enum=ExternalParser, default=ExternalParser.UNDEFINED) parser = models.IntegerField(default=settings.LM_PARSERS.UNDEFINED,
choices=settings.LM_PARSERS.choices())
external_key = models.CharField(max_length=30) external_key = models.CharField(max_length=30)
label = models.CharField(max_length=30) label = models.CharField(max_length=30)
bundle_key = models.CharField(max_length=30) bundle_key = models.CharField(max_length=30)
...@@ -136,13 +131,17 @@ class ExternalCredit(models.Model): ...@@ -136,13 +131,17 @@ class ExternalCredit(models.Model):
created_date = models.DateTimeField(default=timezone.now) created_date = models.DateTimeField(default=timezone.now)
updated_date = models.DateTimeField(default=timezone.now) updated_date = models.DateTimeField(default=timezone.now)
@property
def parser_name(self):
settings.LM_PARSERS.get_name_by_code(self.parser)
@property @property
def external_code(self): def external_code(self):
return str(self.parser) + ":" + str(self.external_key) return self.parser_name + ":" + str(self.external_key)
@property @property
def external_bundle(self): def external_bundle(self):
return str(self.parser) + ":" + str(self.external_key) return self.parser_name + ":" + str(self.external_key)
class Credit(models.Model): class Credit(models.Model):
......
...@@ -170,6 +170,7 @@ OVPN_FILEPATH = config("OVPN_FILEPATH") ...@@ -170,6 +170,7 @@ OVPN_FILEPATH = config("OVPN_FILEPATH")
# #
LM_SERVICES = purist.limitmonitor.ServicesContainer LM_SERVICES = purist.limitmonitor.ServicesContainer
LM_PARSERS = purist.limitmonitor.ParserContainer
# #
# LOGGING # LOGGING
......
...@@ -3,17 +3,11 @@ from collections import OrderedDict ...@@ -3,17 +3,11 @@ from collections import OrderedDict
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
class ServicesContainer(object): class Container:
# CONSTANTS
UNDEFINED = 0 UNDEFINED = 0
TUNNEL = 1
COMMUNICATION = 2
# MAPPING
MAP = OrderedDict([ MAP = OrderedDict([
(UNDEFINED, _("Undefined")), (UNDEFINED, _("Undefined"))
(TUNNEL, _("Tunnel")),
(COMMUNICATION, _("Communication")),
]) ])
@classmethod @classmethod
...@@ -27,3 +21,31 @@ class ServicesContainer(object): ...@@ -27,3 +21,31 @@ class ServicesContainer(object):
@classmethod @classmethod
def get_name_by_code(cls, code): def get_name_by_code(cls, code):
return cls.MAP.get(code, cls.MAP.get(cls.UNDEFINED)) return cls.MAP.get(code, cls.MAP.get(cls.UNDEFINED))
class ServicesContainer(Container):
# CONSTANTS
UNDEFINED = 0
TUNNEL = 1
COMMUNICATION = 2
# MAPPING
MAP = OrderedDict([
(UNDEFINED, _("Undefined")),
(TUNNEL, _("Tunnel")),
(COMMUNICATION, _("Communication")),
])
class ParserContainer(Container):
# CONSTANTS
UNDEFINED = 0
WOO_PRODUCT_V1 = 1
WOO_SUBSCRIPTION_V1 = 2
# MAPPING
MAP = OrderedDict([
(UNDEFINED, _("Undefined")),
(WOO_PRODUCT_V1, _("WooCommerce Product v1")),
(WOO_SUBSCRIPTION_V1, _("WooCommerce Subscription v1")),
])
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