Commit 909430eb authored by David Seaward's avatar David Seaward

replace service enum with custom map (fixes hardcoding site logic in app logic)

parent c8ffcfe6
...@@ -23,8 +23,8 @@ class Migration(migrations.Migration): ...@@ -23,8 +23,8 @@ class Migration(migrations.Migration):
migrations.AlterField( migrations.AlterField(
model_name='externalbundle', model_name='externalbundle',
name='service', name='service',
field=choicesenum.django.fields.EnumIntegerField(default=limitmonitor.models.Service(0), field=choicesenum.django.fields.EnumIntegerField(default=limitmonitor.models.Legacy(0),
enum=limitmonitor.models.Service), enum=limitmonitor.models.Legacy),
), ),
migrations.AlterField( migrations.AlterField(
model_name='externalcredit', model_name='externalcredit',
...@@ -35,7 +35,7 @@ class Migration(migrations.Migration): ...@@ -35,7 +35,7 @@ class Migration(migrations.Migration):
migrations.AlterField( migrations.AlterField(
model_name='limit', model_name='limit',
name='service', name='service',
field=choicesenum.django.fields.EnumIntegerField(default=limitmonitor.models.Service(0), field=choicesenum.django.fields.EnumIntegerField(default=limitmonitor.models.Legacy(0),
enum=limitmonitor.models.Service), enum=limitmonitor.models.Legacy),
), ),
] ]
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2018-01-09 11:39
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('limitmonitor', '0003_auto_20171208_1327'),
]
operations = [
migrations.AlterField(
model_name='externalbundle',
name='service',
field=models.IntegerField(choices=[(0, 'Undefined')], default=0),
),
migrations.AlterField(
model_name='limit',
name='service',
field=models.IntegerField(choices=[(0, 'Undefined')], default=0),
),
]
...@@ -5,29 +5,28 @@ from django.db import models ...@@ -5,29 +5,28 @@ from django.db import models
from django.utils import timezone from django.utils import timezone
class ExternalParser(ChoicesEnum): class Legacy(ChoicesEnum):
UNDEFINED = 0, "Undefined" UNDEFINED = 0, "Undefined"
WOO1 = 1, "WooCommerce v1"
WOOSUB1 = 2, "WooCommerce Subscription v1"
class Service(ChoicesEnum): class ExternalParser(ChoicesEnum):
UNDEFINED = 0, "Undefined" UNDEFINED = 0, "Undefined"
TUNNEL = 1, "Tunnel" WOO1 = 1, "WooCommerce v1"
COMMUNICATION = 2, "Communication" WOOSUB1 = 2, "WooCommerce Subscription v1"
def create_missing_user_limits(user): def create_missing_user_limits(user):
for code, label in Service.choices(): for code in settings.LM_SERVICES.MAP.keys():
is_undefined = code == Service.UNDEFINED.value is_defined = code != settings.LM_SERVICES.UNDEFINED
is_exists = Limit.objects.filter(user=user, service=code).exists() is_exists = Limit.objects.filter(user=user, service=code).exists()
if not is_undefined and not is_exists: if is_defined and not is_exists:
Limit(user=user, service=code).save() Limit(user=user, service=code).save()
class Limit(models.Model): class Limit(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL) user = models.ForeignKey(settings.AUTH_USER_MODEL)
service = EnumIntegerField(enum=Service, default=Service.UNDEFINED) service = models.IntegerField(default=settings.LM_SERVICES.UNDEFINED,
choices=settings.LM_SERVICES.choices())
renewal_date = models.DateTimeField(default=None, blank=True, null=True) renewal_date = models.DateTimeField(default=None, blank=True, null=True)
expiry_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) volume_total = models.DecimalField(default=0, decimal_places=2, max_digits=6)
...@@ -38,12 +37,7 @@ class Limit(models.Model): ...@@ -38,12 +37,7 @@ class Limit(models.Model):
def service_label(self): def service_label(self):
label = "None" return settings.LM_SERVICES.get_name_by_code(self.service)
for key, value in Service.choices():
if self.service == key:
label = value
return label
def active_label(self): def active_label(self):
...@@ -77,7 +71,7 @@ class Limit(models.Model): ...@@ -77,7 +71,7 @@ class Limit(models.Model):
def credit_label(self): def credit_label(self):
if self.service == Service.TUNNEL: if self.service == settings.LM_SERVICES.TUNNEL:
return self.days_credit_label() return self.days_credit_label()
else: else:
return self.days_credit_label() return self.days_credit_label()
...@@ -120,7 +114,8 @@ class Limit(models.Model): ...@@ -120,7 +114,8 @@ class Limit(models.Model):
class ExternalBundle(models.Model): class ExternalBundle(models.Model):
parser = EnumIntegerField(enum=ExternalParser, default=ExternalParser.UNDEFINED) parser = EnumIntegerField(enum=ExternalParser, default=ExternalParser.UNDEFINED)
external_key = models.CharField(max_length=30) external_key = models.CharField(max_length=30)
service = EnumIntegerField(enum=Service, default=Service.UNDEFINED) service = models.IntegerField(default=settings.LM_SERVICES.UNDEFINED,
choices=settings.LM_SERVICES.choices())
time_credit = models.DecimalField(default=0, decimal_places=2, max_digits=6) time_credit = models.DecimalField(default=0, decimal_places=2, max_digits=6)
volume_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) created_date = models.DateTimeField(default=timezone.now)
......
...@@ -8,7 +8,7 @@ from django.db import transaction ...@@ -8,7 +8,7 @@ from django.db import transaction
from django.utils import timezone from django.utils import timezone
from woocommerce import API as WOO_API from woocommerce import API as WOO_API
from ..models import ExternalCredit, ExternalBundle, Limit, Service from ..models import ExternalCredit, ExternalBundle, Limit
logger = get_task_logger(__name__) logger = get_task_logger(__name__)
...@@ -101,7 +101,7 @@ def activate(ssh, limit, credit_timedelta=None, renewal_date=None): ...@@ -101,7 +101,7 @@ def activate(ssh, limit, credit_timedelta=None, renewal_date=None):
# otherwise, activate the limit before saving # otherwise, activate the limit before saving
if limit.service == Service.TUNNEL: if limit.service == settings.LM_SERVICES.TUNNEL:
user_identity = limit.user.get_identity() user_identity = limit.user.get_identity()
filepath = settings.OVPN_FILEPATH.replace("{USER_IDENTITY}", user_identity) filepath = settings.OVPN_FILEPATH.replace("{USER_IDENTITY}", user_identity)
is_file = pathlib.Path(filepath).is_file() is_file = pathlib.Path(filepath).is_file()
...@@ -117,7 +117,7 @@ def activate(ssh, limit, credit_timedelta=None, renewal_date=None): ...@@ -117,7 +117,7 @@ def activate(ssh, limit, credit_timedelta=None, renewal_date=None):
def deactivate(ssh, limit): def deactivate(ssh, limit):
if limit.service == Service.TUNNEL: if limit.service == settings.LM_SERVICES.TUNNEL:
managed_exec(ssh, "./create_new_ovpn_config --revoke %s" % (limit.user.get_identity(),)) managed_exec(ssh, "./create_new_ovpn_config --revoke %s" % (limit.user.get_identity(),))
limit.is_active = False limit.is_active = False
......
...@@ -3,6 +3,7 @@ import strictyaml ...@@ -3,6 +3,7 @@ import strictyaml
from decouple import Config, Csv, RepositoryIni from decouple import Config, Csv, RepositoryIni
from django_auth_ldap.config import LDAPSearch from django_auth_ldap.config import LDAPSearch
import purist.limitmonitor
from .settings_original import * from .settings_original import *
# #
...@@ -164,6 +165,12 @@ OVPN_PORT = config("OVPN_PORT", cast=int) ...@@ -164,6 +165,12 @@ OVPN_PORT = config("OVPN_PORT", cast=int)
OVPN_USERNAME = config("OVPN_USERNAME") OVPN_USERNAME = config("OVPN_USERNAME")
OVPN_FILEPATH = config("OVPN_FILEPATH") OVPN_FILEPATH = config("OVPN_FILEPATH")
#
# LIMIT MONITOR
#
LM_SERVICES = purist.limitmonitor.ServicesContainer
# #
# LOGGING # LOGGING
# #
......
from collections import OrderedDict
from django.utils.translation import ugettext_lazy as _
class ServicesContainer(object):
# CONSTANTS
UNDEFINED = 0
TUNNEL = 1
COMMUNICATION = 2
# MAPPING
MAP = OrderedDict([
(UNDEFINED, _("Undefined")),
(TUNNEL, _("Tunnel")),
(COMMUNICATION, _("Communication")),
])
@classmethod
def choices(cls):
choices = []
for code, name in cls.MAP.items():
choices.append((code, name))
return choices
@classmethod
def get_name_by_code(cls, code):
return cls.MAP.get(code, cls.MAP.get(cls.UNDEFINED))
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