Commit 774fb92f authored by David Seaward's avatar David Seaward

* define profile view in limitmonitor application

* refine schema for limitmonitor
* clean up html templates
* move global admin settings from app to urls.py
parent b016dda4
......@@ -28,10 +28,3 @@ class LdapPersonAdmin(admin.ModelAdmin):
admin.site.register(User, UserAdmin)
admin.site.register(LdapGroup, LdapGroupAdmin)
admin.site.register(LdapPerson, LdapPersonAdmin)
#
# Set admin titles
#
admin.site.site_title = "Site administration"
admin.site.site_header = "Site administration"
<!DOCTYPE html>
<!--
Copyright 2017 Purism SPC and contributors
https://plan.puri.st/app/account_web
SPDX-License-Identifier: CC-BY-SA-4.0
-->
<html lang="en">
<head>
<link rel="stylesheet" href="{% static 'PuristFlex.css' %}"/>
......@@ -13,7 +20,7 @@
<p style="text-align: right">
{% if request.user.is_authenticated %}
{% trans "Logged in as" %} {{ username }}<br/>
<a href="{% url 'auth_password_change' %}">{% trans "Change password" %}</a> |
<a href="{% url 'profile' %}">{% trans "Profile" %}</a> |
{% if request.user.is_superuser %}
<a href="{% url 'admin:index' %}">{% trans "Admin" %}</a> |
......
<!DOCTYPE html>
{% load i18n %} {% load static %}
<!-- Content, copyright 2017 Purism SPC and contributors -->
<!-- https://plan.puri.st/app/account_web -->
<!-- SPDX-License-Identifier: CC-BY-SA-4.0 -->
<!-- Original registration template, copyright 2015 Anders Hofstee and contributors -->
<!-- https://github.com/RatanShreshtha/django-registration-templates -->
<!-- SPDX-License-Identifier: MIT -->
<!--
Page content, copyright 2017 Purism SPC and contributors
https://plan.puri.st/app/account_web
SPDX-License-Identifier: CC-BY-SA-4.0
Original registration template, copyright 2015 Anders Hofstee and contributors
https://github.com/RatanShreshtha/django-registration-templates
SPDX-License-Identifier: MIT
-->
<html lang="en">
<head>
......@@ -14,6 +17,7 @@
<title>{% block title %}Base{% endblock %}</title>
<link rel="icon" sizes="960x960" href="{% static 'favicon.png' %}">
<meta name="application-name" content="Purist">
<meta charset="UTF-8">
</head>
<body>
......@@ -29,7 +33,7 @@
<div id="log_state">
{% if user.is_authenticated %}
{% trans "Logged in as" %} {{ user.username }}<br/>
<a href="{% url 'auth_password_change' %}">{% trans "Change password" %}</a> |
<a href="{% url 'profile' %}">{% trans "Profile" %}</a> |
{% if user.is_superuser %}
<a href="{% url 'admin:index' %}">{% trans "Admin" %}</a> |
{% endif %}
......
default_app_config = 'limitmonitor.apps.LimitMonitorConfig'
# Register your models here.
from django.contrib import admin
from .models import Credit, ExternalBundle, ExternalCredit, Limit
#
# Declare admin models
#
class CreditAdmin(admin.ModelAdmin):
list_display = ['limit', 'external', 'time_credit', 'volume_credit']
class ExternalBundleAdmin(admin.ModelAdmin):
list_display = ['parser', 'external_key', 'service', 'time_credit', 'volume_credit']
class ExternalCreditAdmin(admin.ModelAdmin):
list_display = ['parser', 'external_key', 'bundle_key', 'account_name', 'is_converted']
class LimitAdmin(admin.ModelAdmin):
list_display = ['user', 'service', 'is_active', 'expiry_date', 'volume_total', 'time_total']
#
# Register admin models
#
admin.site.register(Credit, CreditAdmin)
admin.site.register(ExternalBundle, ExternalBundleAdmin)
admin.site.register(ExternalCredit, ExternalCreditAdmin)
admin.site.register(Limit, LimitAdmin)
<!DOCTYPE html>
<!--
Copyright 2017 Purism SPC and contributors
https://plan.puri.st/app/account_web
SPDX-License-Identifier: CC-BY-SA-4.0
-->
<html lang="en">
<head>
<link rel="stylesheet" href="{% static 'PuristFlex.css' %}"/>
<title>User limits</title>
<title>{% trans "User profile" %}</title>
<link rel="icon" sizes="960x960" href="{% static 'favicon.png' %}">
<meta name="application-name" content="{{ site_title }}">
<meta charset="UTF-8">
......@@ -10,31 +17,72 @@
<body>
<table style="border-collapse: collapse;">
<header style="justify-content: space-between;">
<div id="title_box">
<a href="{% url 'home' %}"><img class="logo" src="{% static 'logo.png' %}" alt="Purist"/></a>
<div id="title_text">
<h1>{% trans "User profile" %}</h1>
<p>{% trans "Service credit and profile management" %}</p>
</div>
</div>
<div id="log_state">
{% if request.user.is_authenticated %}
{% trans "Logged in as" %} {{ username }}<br/>
<em>{% trans "Profile" %}</em> |
{% if request.user.is_superuser %}
<a href="{% url 'admin:index' %}">{% trans "Admin" %}</a> |
{% endif %}
<a href="{% url 'auth_logout' %}">{% trans "Log out" %}</a>
{% else %}
{% trans "You are not logged in." %}<br/>
<a href="{% url 'auth_login' %}">{% trans "Log in" %}</a> {% trans "or" %}
<a href="{% url 'registration_register' %}">{% trans "register." %}</a>
{% endif %}
</div>
</header>
<div id="main">
<article>
<h2>Services</h2>
<table style="border-collapse: collapse;">
<thead>
<tr>
<th>Service</th>
<th>Active</th>
<th>Expires</th>
<th>Credit</th>
</tr>
</thead>
<tbody>
{% for limit in limits %}
<tr>
<th style="text-align: left;">{{ limit.service_label() }}</th>
<td>{{ limit.is_active }}</td>
<td title="{{ limit.expiry_full_label() }}">{{ limit.expiry_short_label() }}</td>
<td>{{ limit.credit_label() }}</td>
</tr>
<thead>
<tr>
<th>Service</th>
<th>Active</th>
<th>Expires</th>
<th>Credit</th>
</tr>
</thead>
{% endfor %}
</tbody>
<tbody>
{% for limit in limits %}
</table>
<tr>
<th>{{ limit.service }}</th>
<td>{{ limit.is_active }}</td>
<td>{{ limit.expiry_date }}</td>
<td>{{ limit.time_total }}</td>
</tr>
<h2>Profile management</h2>
{% endfor %}
</tbody>
<ul>
<li><a href="{% url 'auth_password_change' %}">{% trans "Change password" %}</a></li>
</ul>
</table>
</article>
</div>
</body>
</html>
......@@ -22,12 +22,16 @@ class Migration(migrations.Migration):
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('time_credit', models.DecimalField(decimal_places=2, default=0, max_digits=6)),
('volume_credit', models.DecimalField(decimal_places=2, default=0, max_digits=6)),
('old_expiry_date', models.DateTimeField(default=None, null=True)),
('old_time_total', models.DecimalField(decimal_places=2, default=None, max_digits=6, null=True)),
('old_volume_total', models.DecimalField(decimal_places=2, default=None, max_digits=6, null=True)),
('new_expiry_date', models.DateTimeField(default=None, null=True)),
('new_time_total', models.DecimalField(decimal_places=2, default=None, max_digits=6, null=True)),
('new_volume_total', models.DecimalField(decimal_places=2, default=None, max_digits=6, null=True)),
('old_expiry_date', models.DateTimeField(default=None, blank=True, null=True)),
('old_time_total',
models.DecimalField(blank=True, decimal_places=2, default=None, max_digits=6, null=True)),
('old_volume_total',
models.DecimalField(blank=True, decimal_places=2, default=None, max_digits=6, null=True)),
('new_expiry_date', models.DateTimeField(blank=True, default=None, null=True)),
('new_time_total',
models.DecimalField(blank=True, decimal_places=2, default=None, max_digits=6, null=True)),
('new_volume_total',
models.DecimalField(blank=True, decimal_places=2, default=None, max_digits=6, null=True)),
('created_date', models.DateTimeField(default=django.utils.timezone.now)),
('updated_date', models.DateTimeField(default=django.utils.timezone.now)),
],
......@@ -69,7 +73,7 @@ class Migration(migrations.Migration):
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('service',
models.CharField(choices=[('TUNNEL', 'Tunnel'), ('COMMUNICATION', 'Communication')], max_length=30)),
('expiry_date', models.DateTimeField(blank=True, default=None)),
('expiry_date', models.DateTimeField(blank=True, default=None, null=True)),
('volume_total', models.DecimalField(decimal_places=2, max_digits=6)),
('time_total', models.DecimalField(decimal_places=2, max_digits=6)),
('is_active', models.BooleanField()),
......@@ -81,7 +85,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='credit',
name='external',
field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.CASCADE,
field=models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.CASCADE,
to='limitmonitor.ExternalCredit'),
),
migrations.AddField(
......
......@@ -26,13 +26,67 @@ def get_wcapi():
class Limit(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
service = models.CharField(max_length=30, choices=SERVICE_CHOICES)
expiry_date = models.DateTimeField(default=None, blank=True)
expiry_date = models.DateTimeField(default=None, blank=True, null=True)
volume_total = models.DecimalField(decimal_places=2, max_digits=6)
time_total = models.DecimalField(decimal_places=2, max_digits=6)
is_active = models.BooleanField()
created_date = models.DateTimeField(default=timezone.now)
updated_date = models.DateTimeField(default=timezone.now)
def service_label(self):
label = "None"
for key, value in SERVICE_CHOICES:
if self.service == key:
label = value
return label
def expiry_short_label(self):
if self.expiry_date is None:
return "-"
else:
return self.expiry_date.strftime("%Y-%m-%d")
def expiry_full_label(self):
if self.expiry_date is None:
return "-"
else:
return self.expiry_date.strftime("%Y-%m-%d %H:%M:%S %z")
def credit_label(self):
if self.service == "TUNNEL":
return self.tunnel_credit_label()
else:
return self.tunnel_credit_label()
def timedelta_to_daystring(self, delta):
single_day = 60 * 60 * 24
days = int(delta.total_seconds() / single_day)
return str(days) + " days"
def tunnel_credit_label(self):
label = "-"
has_zero = self.expiry_date is None and self.time_total == 0
has_credit_only = self.expiry_date is None and self.time_total > 0
has_valid_expiry = self.expiry_date is not None and self.time_total == 0
if has_zero:
label = "-"
elif has_credit_only:
label = str(int(self.time_total)) + " days"
elif has_valid_expiry:
difference = self.expiry_date - timezone.now()
label = self.timedelta_to_daystring(difference)
else:
label = "Invalid"
return label
class ExternalBundle(models.Model):
parser = models.CharField(max_length=30, choices=EXTERNAL_PARSER_CHOICES)
......@@ -60,14 +114,14 @@ class ExternalCredit(models.Model):
class Credit(models.Model):
limit = models.ForeignKey(Limit)
external = models.ForeignKey(ExternalCredit, null=True, default=None)
external = models.ForeignKey(ExternalCredit, blank=True, null=True, default=None)
time_credit = models.DecimalField(default=0, decimal_places=2, max_digits=6)
volume_credit = models.DecimalField(default=0, decimal_places=2, max_digits=6)
old_expiry_date = models.DateTimeField(null=True, default=None)
old_time_total = models.DecimalField(null=True, default=None, decimal_places=2, max_digits=6)
old_volume_total = models.DecimalField(null=True, default=None, decimal_places=2, max_digits=6)
new_expiry_date = models.DateTimeField(null=True, default=None)
new_time_total = models.DecimalField(null=True, default=None, decimal_places=2, max_digits=6)
new_volume_total = models.DecimalField(null=True, default=None, decimal_places=2, max_digits=6)
old_expiry_date = models.DateTimeField(blank=True, null=True, default=None)
old_time_total = models.DecimalField(blank=True, null=True, default=None, decimal_places=2, max_digits=6)
old_volume_total = models.DecimalField(blank=True, null=True, default=None, decimal_places=2, max_digits=6)
new_expiry_date = models.DateTimeField(blank=True, null=True, default=None)
new_time_total = models.DecimalField(blank=True, null=True, default=None, decimal_places=2, max_digits=6)
new_volume_total = models.DecimalField(blank=True, null=True, default=None, decimal_places=2, max_digits=6)
created_date = models.DateTimeField(default=timezone.now)
updated_date = models.DateTimeField(default=timezone.now)
......@@ -22,11 +22,22 @@ import ldapregister.views
import limitmonitor.views
from ldapregister.forms import RegistrationForm
#
# Set admin titles for this site
#
admin.site.site_title = "Site administration"
admin.site.site_header = "Site administration"
#
# Define patterns for this site
#
urlpatterns = [
url(r'^$', ldapregister.views.home, name='home'),
url(r'^admin/', admin.site.urls),
url(r'^accounts/$', RedirectView.as_view(url='/')),
url(r'^accounts/profile/$', limitmonitor.views.userlimit, name='limitmonitor_userlimit'),
url(r'^accounts/profile/$', limitmonitor.views.userlimit, name='profile'),
url(r'^accounts/register/$', RegistrationView.as_view(form_class=RegistrationForm), name='registration_register'),
url(r'^accounts/', include('registration.backends.simple.urls')),
]
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