Commit f0240888 authored by David Seaward's avatar David Seaward

refactor common functions, add missing migration

parent 5fe5b620
# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2017-08-28 06:15
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('limitmonitor', '0002_auto_20170815_1159'),
]
operations = [
migrations.AddField(
model_name='limit',
name='renewal_date',
field=models.DateTimeField(blank=True, default=None, null=True),
),
migrations.AlterField(
model_name='externalbundle',
name='parser',
field=models.CharField(choices=[('WOO1', 'WooCommerce v1'), ('WOOSUB1', 'WooCommerce Subscription v1')], max_length=30),
),
migrations.AlterField(
model_name='externalcredit',
name='parser',
field=models.CharField(choices=[('WOO1', 'WooCommerce v1'), ('WOOSUB1', 'WooCommerce Subscription v1')], max_length=30),
),
]
......@@ -4,6 +4,7 @@ from django.conf import settings
from django.db import transaction
from django.utils import timezone
from woocommerce import API as WOO_API
import django.contrib.auth
from ..models import ExternalCredit, ExternalBundle, Limit
......@@ -72,18 +73,32 @@ def get_external_bundle(parser, external_key):
)
def activate(ssh, limit, credit_timedelta):
if limit.is_active:
def activate(ssh, limit, credit_timedelta=None, renewal_date=None):
is_credit = credit_timedelta is not None
is_renewal = renewal_date is not None
if is_credit == is_renewal:
raise Exception("Invalid activation attempt. Need strictly one of credit or renewal data.")
if is_credit and limit.is_active:
limit.expiry_date += credit_timedelta
else:
elif is_credit and not limit.is_active:
limit.expiry_date = timezone.now() + credit_timedelta
limit.is_active = True
elif is_renewal:
limit.renewal_date = renewal_date
limit.is_active = True
else:
raise Exception("Invalid activation attempt. Unknown condition.")
# skip activation command if we are debugging
if settings.DEBUG_SKIP_ACTIVATION_COMMAND:
limit.save()
return
# otherwise, activate the limit before saving
if limit.service == "TUNNEL":
managed_exec(ssh, "./create_new_ovpn_config --generate %s" % (limit.user.get_identity(),))
......@@ -98,12 +113,59 @@ def deactivate(ssh, limit):
limit.save()
def get_account_from_woo_meta(meta_list):
account = "invalid"
for meta_item in meta_list:
if meta_item["key"] in settings.WOO1_FIELD_LIST: # for example, "Existing username,"
account = meta_item["value"]
at_count = account.count("@")
if at_count == 0: # nodomain
account += "@" + settings.SITE_DOMAIN # corrected to nodomain@example.com
elif at_count == 1:
pass # valid account format
else: # at_count > 1, for example bad@user@example.com
account = account.replace("@", ".AT.") # force invalid name bad.AT.user.AT.example.com
return account
def get_limit_objects(credit):
# get and validate local username
suffix = "@" + settings.SITE_DOMAIN
if credit.account_name is None or not str.endswith(credit.account_name, suffix):
raise Exception("Invalid account name: " + str(credit.account_name))
else:
suffix_len = 0 - len(suffix)
username = credit.account_name[:suffix_len]
# get objects (implicit validation that they exist)
external_bundle = get_external_bundle(credit.parser, credit.bundle_key)
limit = Limit.objects.get(
user__username=username,
service=external_bundle.service,
)
user = django.contrib.auth.get_user_model().objects.get(
username=username,
)
return user, limit, external_bundle
@transaction.atomic
def store_credit_and_update_limit(ssh, credit):
def store_credit_and_update_limit(ssh, credit, next_renewal=None):
try:
if credit.parser == "WOO1":
from .tunnel_credit import update_limit_woo1
update_limit_woo1(ssh, credit)
elif credit.parser == "WOOSUB1":
from .tunnel_subscription import update_limit_woosub1
update_limit_woosub1(ssh, credit, next_renewal)
else:
raise Exception("Unrecognised parser " + credit.parser)
......
import django.contrib.auth
from .common import *
......@@ -19,20 +17,7 @@ def parse_woo1(json_entry, product_id):
product_id = line_item["product_id"]
product_label = line_item["name"]
quantity = line_item["quantity"]
account = "invalid"
for meta_item in line_item["meta"]:
if meta_item["key"] in settings.WOO1_FIELD_LIST: # for example, "Existing username,"
account = meta_item["value"]
at_count = account.count("@")
if at_count == 0: # nodomain
account += "@" + settings.SITE_DOMAIN # corrected to nodomain@example.com
elif at_count == 1:
pass # valid account format
else: # at_count > 1, for example bad@user@example.com
account = account.replace("@", ".AT.") # force invalid name bad.AT.user.AT.example.com
account = get_account_from_woo_meta(line_item["meta"])
external_key = str(order_id) + ":" + str(item_id)
external_label = order_name + ":" + str(item_id)
......@@ -52,63 +37,24 @@ def parse_woo1(json_entry, product_id):
def update_limit_woo1(ssh, credit):
# validate credit
suffix = "@" + settings.SITE_DOMAIN
if credit.account_name is None or not str.endswith(credit.account_name, suffix):
raise Exception("Invalid account name: " + str(credit.account_name))
else:
suffix_len = 0 - len(suffix)
username = credit.account_name[:suffix_len]
# get external references (implicit validation that they exist)
external_bundle = get_external_bundle(credit.parser, credit.bundle_key)
limit = Limit.objects.get(
user__username=username,
service=external_bundle.service,
)
user = django.contrib.auth.get_user_model().objects.get(
username=username,
)
# set user email if missing
credit_has_email = not (credit.additional_data is None or credit.additional_data == "")
user_has_email = not (user.email is None or user.email == "")
user, limit, external_bundle = get_limit_objects(credit)
if user_has_email:
pass
elif credit_has_email:
user.email = credit.additional_data
user.save()
else:
logger.warn("No email address for credit " + credit.parser + " " + credit.external_key)
# # set user email if missing
# credit_has_email = not (credit.additional_data is None or credit.additional_data == "")
# user_has_email = not (user.email is None or user.email == "")
#
# if user_has_email:
# pass
# elif credit_has_email:
# user.email = credit.additional_data
# user.save()
# else:
# logger.warn("No email address for credit " + credit.parser + " " + credit.external_key)
credit_days = int(external_bundle.time_credit * credit.quantity)
credit_timedelta = timezone.timedelta(days=credit_days)
activate(ssh, limit, credit_timedelta)
@transaction.atomic
def store_credit_and_update_limit(ssh, credit):
try:
if credit.parser == "WOO1":
update_limit_woo1(ssh, credit)
else:
raise Exception("Unrecognised parser " + credit.parser)
credit.is_converted = True
credit.error_message = ""
except Exception as e:
message = "Skipped adding credit " + credit.parser + ":" + credit.external_key + ". "
logger.exception(message)
credit.error_message = message + repr(e)
finally:
credit.save()
state = "converted" if credit.is_converted else "skipped"
logger.info("Stored " + state + " credit " + credit.parser + ":" + credit.external_key)
activate(ssh, limit, credit_timedelta, None)
def monitor_woo1():
......
This diff is collapsed.
File mode changed from 100755 to 100644
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