Commit 2bbce29e authored by David Seaward's avatar David Seaward

subscription renewals + minor UX fixes

* subscriptions: renewals are now monitored
* ux: "Yes" or "No" for is active (not "True" and "False")
* ux: only display download links for active services
* admin: improve admin page readability (ids and smart column values)
parent 802f1a30
......@@ -29,3 +29,4 @@ OVPN_HOSTNAME = ssh.example.com
OVPN_PORT = 22
OVPN_USERNAME = username
OVPN_FILEPATH = "/path/to/{IDENTITY}/{IDENTITY}.ovpn"
SUBSCRIPTION_LINK = https://www.example.com
......@@ -8,20 +8,20 @@ from .models import Credit, ExternalBundle, ExternalCredit, Limit
#
class CreditAdmin(admin.ModelAdmin):
list_display = ['limit', 'external', 'time_credit', 'volume_credit']
list_display = ['id', 'limit', 'external', 'time_credit', 'volume_credit']
class ExternalBundleAdmin(admin.ModelAdmin):
list_display = ['parser', 'external_key', 'service', 'time_credit', 'volume_credit']
list_display = ['id', 'parser', 'external_key', 'service', 'time_credit', 'volume_credit']
class ExternalCreditAdmin(admin.ModelAdmin):
list_display = ['parser', 'external_key', 'bundle_key', 'account_name', 'is_converted', 'error_message']
list_display = ['label', 'external_code', 'bundle_key', 'account_name', 'is_converted', 'error_message']
class LimitAdmin(admin.ModelAdmin):
list_display = ['user', 'service', 'is_active', 'expiry_date', 'volume_total', 'time_total']
list_display = ['id', 'user', 'service', 'is_active', 'renewal_date', 'expiry_date', 'volume_total', 'time_total']
list_filter = ['service', 'renewal_date', 'user']
#
# Register admin models
......
......@@ -65,7 +65,7 @@ SPDX-License-Identifier: CC-BY-SA-4.0
<tr>
<th style="text-align: left;">{{ limit.service_label() }}</th>
<td>{{ limit.is_active }}</td>
<td>{{ limit.active_label() }}</td>
<td title="{{ limit.endpoint_full_label() }}">{{ limit.endpoint_short_label() }}</td>
<td>{{ limit.credit_label() }}</td>
</tr>
......@@ -79,14 +79,20 @@ SPDX-License-Identifier: CC-BY-SA-4.0
<ul>
<li><a href="{% url 'auth_password_change' %}">{% trans "Change password" %}</a></li>
<li><a href="{{ link_subscription }}" target="_blank">{% trans "Manage subscriptions" %}</a></li>
</ul>
<h2>{% trans "Downloads" %}</h2>
<ul>
{% if has_limit["TUNNEL"] %}
<li><a href="{% url 'ovpn_userfile' %}">{% trans "Tunnel certificate" %}</a></li>
<li><a href="{% static 'tunnel_instructions.pdf' %}">{% trans "How to use Tunnel certificate (PDF)" %}</a>
</li>
{% endif %}
{% if has_limit["NONE"] %}
<li>None</li>
{% endif %}
</ul>
</article>
......
......@@ -39,6 +39,13 @@ class Limit(models.Model):
return label
def active_label(self):
if self.is_active:
return "Yes"
else:
return "No"
def endpoint_short_label(self):
return self.endpoint_label("%Y-%m-%d")
......@@ -122,6 +129,14 @@ class ExternalCredit(models.Model):
created_date = models.DateTimeField(default=timezone.now)
updated_date = models.DateTimeField(default=timezone.now)
@property
def external_code(self):
return str(self.parser) + ":" + str(self.external_key)
@property
def external_bundle(self):
return str(self.parser) + ":" + str(self.external_key)
class Credit(models.Model):
limit = models.ForeignKey(Limit)
......
......@@ -19,7 +19,7 @@ def parse_woo1(json_entry, product_id):
quantity = line_item["quantity"]
account = get_account_from_woo_meta(line_item["meta"])
external_key = str(order_id) + ":" + str(item_id)
external_label = order_name + ":" + str(item_id)
external_label = str(order_name)
result_list.append({
"parser": "WOO1",
......
......@@ -28,7 +28,7 @@ def parse_woosub1(json_entry):
result = {
"parser": "WOOSUB1",
"external_key": subscription_id,
"label": json_entry["number"] + ":" + json_entry["order_key"],
"label": json_entry["number"],
"bundle_key": str(line_item["product_id"]),
"bundle_label": str(line_item["name"]),
"quantity": 1,
......@@ -89,26 +89,50 @@ def update_limit_woosub1(ssh, credit, renewal_date):
def monitor_woosub1_renewals():
return
# make connection objects
woo_connection = get_woo_connection()
ssh = get_openvpn_ssh_connection()
# get objects due for renewal and deactivate them
now = timezone.now()
# get objects due for renewal (one hour grace / buffer)
now = timezone.now() - datetime.timedelta(hours=1)
overdue_list = Limit.objects.filter(renewal_date__lte=now,expiry_date=None, is_active=True)
count = 0
for limit in overdue_list:
woosub1_list = ExternalCredit.objects.filter(parser="WOOSUB1")
for woosub in woosub1_list:
subscription_query = "subscriptions/" + woosub.external_key
woosub_json = woo_connection.get(subscription_query).json()
result = parse_woosub1(woosub_json)
if not result["active"]:
deactivate(ssh, limit)
else:
woosub.updated_date = now
limit.renewal_date = result["renewal"]
limit.save()
identity = limit.user.get_identity()
woosub_bundle_list = ExternalBundle.objects.filter(service=limit.service, parser="WOOSUB1")
for bundle in woosub_bundle_list:
woosub_credit = ExternalCredit.objects.get(account_name=identity, bundle_key=bundle.external_key,
parser="WOOSUB1")
try:
subscription_query = "subscriptions/" + woosub_credit.external_key
woosub_json = woo_connection.get(subscription_query).json()
result = parse_woosub1(woosub_json)
# deactivate expired limits
if not result["active"]:
deactivate(ssh, limit)
woosub_credit.is_converted = False
woosub_credit.error_message = "Expired."
else:
store_credit_and_update_limit(ssh, woosub_credit, result["renewal"])
woosub_credit.is_converted = True
woosub_credit.error_message = ""
count += 1
except Exception as e:
message = "Skipping bad credit %s (%s)." % (woosub_credit.id, woosub_credit.external_code)
logger.exception(message)
woosub_credit.is_converted = False
woosub_credit.error_message = message + repr(e)
finally:
woosub_credit.save()
if count > 0:
logger.info("Updated %i subscriptions." % (count,))
......@@ -11,11 +11,24 @@ def userlimit(request):
username = request.user.get_username()
limits = Limit.objects.filter(user__username=username)
# get flags for each limit
has_limit = {}
none_limit = True
for limit in limits:
has_limit[limit.service] = limit.is_active
if limit.is_active:
none_limit = False
has_limit["NONE"] = none_limit # true if no limits are active
render_data = {
"username": username,
"site_title": settings.SITE_TITLE,
"site_byline": settings.SITE_BYLINE,
"limits": limits,
"has_limit": has_limit,
"link_subscription": settings.LINK_SUBSCRIPTION,
}
return render(request, 'limitmonitor/userlimit.html', render_data)
......
......@@ -126,6 +126,8 @@ SITE_TITLE = config("SITE_TITLE")
SITE_BYLINE = config("SITE_BYLINE")
SITE_DOMAIN = config("SITE_DOMAIN")
LINK_SUBSCRIPTION = config("LINK_SUBSCRIPTION")
#
# WOOCOMMERCE
#
......
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