From 8e88b74fb41b0ad6301be8d8265231557e696034 Mon Sep 17 00:00:00 2001 From: Shiv K Sah Date: Fri, 2 Jun 2017 20:06:33 +0530 Subject: [PATCH 1/7] Credit limit setting --- cloud/endagaweb/forms/dashboard_forms.py | 21 ++++ cloud/endagaweb/models.py | 3 + .../dashboard/network_detail/nav.html | 6 + .../network_detail/network-balancelimit.html | 115 ++++++++++++++++++ cloud/endagaweb/urls.py | 4 + cloud/endagaweb/views/dashboard.py | 3 + cloud/endagaweb/views/network.py | 76 ++++++++++++ 7 files changed, 228 insertions(+) create mode 100644 cloud/endagaweb/templates/dashboard/network_detail/network-balancelimit.html diff --git a/cloud/endagaweb/forms/dashboard_forms.py b/cloud/endagaweb/forms/dashboard_forms.py index 2f9a6ddc..2bd9786c 100644 --- a/cloud/endagaweb/forms/dashboard_forms.py +++ b/cloud/endagaweb/forms/dashboard_forms.py @@ -368,3 +368,24 @@ def __init__(self, *args, **kwargs): self.helper.form_action = '/dashboard/staff/tower-monitoring' self.helper.add_input(Submit('submit', 'Select')) self.helper.layout = Layout('tower') + +class NetworkBalanceLimit(forms.Form): + + """Crispy form to set Network balance limit and transaction. + set min_value =0.01 so that it will not accept 0 value""" + + limit = forms.CharField(required=False, label="Maximum Balance Limit", + max_length=10) + transaction = forms.CharField(required=False, max_length=3, + label='Maximum Permissible Unsuccessful ' + 'Transactions') + + def __init__(self, *args, **kwargs): + super(NetworkBalanceLimit, self).__init__(*args, **kwargs) + self.helper = FormHelper() + self.helper.form_id = 'id-NetworkBalanceLimitForm' + self.helper.form_method = 'post' + self.helper.form_action = '/dashboard/network/balance-limit' + self.helper.form_class = 'col-xs-12 col-sm-8 col-md-12 col-xl-8' + self.helper.add_input(Submit('submit', 'Save')) + self.helper.layout = Layout('limit', 'transaction') \ No newline at end of file diff --git a/cloud/endagaweb/models.py b/cloud/endagaweb/models.py index d2429f9d..502aa19a 100644 --- a/cloud/endagaweb/models.py +++ b/cloud/endagaweb/models.py @@ -978,6 +978,9 @@ class Network(models.Model): # Network environments let you specify things like "prod", "test", "dev", # etc so they can be filtered out of alerts. For internal use. environment = models.TextField(default="default") + # Added for Network Balance Limit + max_account_limit = models.BigIntegerField(default=10000) + max_failure_transaction = models.IntegerField(default=3) class Meta: permissions = ( diff --git a/cloud/endagaweb/templates/dashboard/network_detail/nav.html b/cloud/endagaweb/templates/dashboard/network_detail/nav.html index 80a0705a..86121c06 100644 --- a/cloud/endagaweb/templates/dashboard/network_detail/nav.html +++ b/cloud/endagaweb/templates/dashboard/network_detail/nav.html @@ -37,6 +37,12 @@ {% else %}{% url 'network-edit' %} {% endif %}">Edit + diff --git a/cloud/endagaweb/templates/dashboard/network_detail/network-balancelimit.html b/cloud/endagaweb/templates/dashboard/network_detail/network-balancelimit.html new file mode 100644 index 00000000..ec38b2ec --- /dev/null +++ b/cloud/endagaweb/templates/dashboard/network_detail/network-balancelimit.html @@ -0,0 +1,115 @@ +{% extends "dashboard/layout.html" %} +{% comment %} +Copyright (c) 2016-present, Facebook, Inc. +All rights reserved. + +This source code is licensed under the BSD-style license found in the +LICENSE file in the root directory of this source tree. An additional grant +of patent rights can be found in the PATENTS file in the same directory. +{% endcomment %} +{% load apptags %} +{% load humanize %} +{% load crispy_forms_tags %} + + +{% block title %} +{% if network.name %} +{% tmpl_const "SITENAME" %} | "{{ network.name }}" +{% else %} +{% tmpl_const "SITENAME" %} | Network +{% endif %} +{% endblock %} + + +{% block pagestyle %} + +{% endblock %} + + +{% block content %} + + +{% include "dashboard/network_detail/header.html" with network=network %} + + +
+ {% include "dashboard/network_detail/nav.html" with active_tab='limit'%} + +
+
+
+ + {% if user_profile.user.is_staff %} + {% crispy network_balance_limit_form %} + {% endif %} +

Network current maximum balance limit: {% currency network.max_account_limit %}

+

Maximum permissible unsuccessful transactions current limit: {{ network.max_failure_transaction }}

+
+
+ {% for message in messages %} +
{{ message }}
+ {% endfor %} +
+
+ +
+
+ {% endblock %} +{% block js %} + + + +{% endblock %} + diff --git a/cloud/endagaweb/urls.py b/cloud/endagaweb/urls.py index 4c7d2469..205ab452 100644 --- a/cloud/endagaweb/urls.py +++ b/cloud/endagaweb/urls.py @@ -150,6 +150,10 @@ name='network-edit'), url(r'^dashboard/network/select/(?P[0-9]+)$', endagaweb.views.network.NetworkSelectView.as_view()), + # Added for network balance limit + url(r'^dashboard/network/balance-limit', + endagaweb.views.network.NetworkBalanceLimit.as_view(), + name='network_balance_limit'), # The activity table. url(r'^dashboard/activity', endagaweb.views.dashboard.ActivityView.as_view(), diff --git a/cloud/endagaweb/views/dashboard.py b/cloud/endagaweb/views/dashboard.py index fad4b84e..9f1ec2c8 100644 --- a/cloud/endagaweb/views/dashboard.py +++ b/cloud/endagaweb/views/dashboard.py @@ -560,6 +560,9 @@ def post(self, request, imsi=None): CURRENCIES[currency]).amount_raw if abs(amount) > 2147483647: raise ValueError(error_text) + if sub.balance + amount > network.max_account_limit: + error_text = 'Error : Crossed Credit Limit.' + raise ValueError(error_text) except ValueError: messages.error(request, error_text) return adjust_credit_redirect diff --git a/cloud/endagaweb/views/network.py b/cloud/endagaweb/views/network.py index bf0db230..d5712e80 100644 --- a/cloud/endagaweb/views/network.py +++ b/cloud/endagaweb/views/network.py @@ -453,3 +453,79 @@ def get(self, request, network_id): user_profile.network = network user_profile.save() return http.HttpResponseRedirect(request.META.get('HTTP_REFERER', '/dashboard')) + + +class NetworkBalanceLimit(ProtectedView): + """Edit basic network info (to add credit to Network).""" + + def get(self, request): + """Handles GET requests.""" + user_profile = models.UserProfile.objects.get(user=request.user) + network = user_profile.network + # Set the context with various stats. + currency = network.subscriber_currency + context = { + 'networks': get_objects_for_user(request.user, 'view_network', + klass=models.Network), + 'user_profile': user_profile, + 'network': network, + 'currency': CURRENCIES[network.subscriber_currency], + 'network_balance_limit_form': dashboard_forms.NetworkBalanceLimit({ + 'limit': '', + 'transitions': '', + + }), + } + # Render template. + edit_template = template.loader.get_template( + 'dashboard/network_detail/network-balancelimit.html') + html = edit_template.render(context, request) + return http.HttpResponse(html) + + def post(self, request): + """Handles POST requests.""" + user_profile = models.UserProfile.objects.get(user=request.user) + network = user_profile.network + success = [] + if 'limit' not in request.POST: + return http.HttpResponseBadRequest() + if 'transaction' not in request.POST: + return http.HttpResponseBadRequest() + limit = float(request.POST.get('limit') or 0) + + if request.POST.get('transaction') == "" and request.POST.get( + 'limit') == "": + error_text = 'Error : please provide value.' + messages.error(request, error_text, + extra_tags="alert alert-danger") + return redirect(urlresolvers.reverse('network_balance_limit')) + if request.POST.get('transaction') == "" and limit <= 0: + error_text = 'Error : Enter positive and non-zero value ' \ + 'for balance limit.' + messages.error(request, error_text, + extra_tags="alert alert-danger") + return redirect(urlresolvers.reverse('network_balance_limit')) + with transaction.atomic(): + try: + currency = network.subscriber_currency + if request.POST.get('limit'): + limit = float(request.POST.get('limit')) + amount = parse_credits(limit, + CURRENCIES[currency]).amount_raw + network.max_account_limit = amount + success.append('Network maximum balance limit updated.') + if request.POST.get('transaction'): + transaction_val = float(request.POST.get('transaction')) + network.max_failure_transaction = transaction_val + success.append('Network maximun permissible unsuccessful' \ + ' transactions limit updated.') + network.save() + except ValueError: + error_text = 'Error : please provide valid value.' + messages.error(request, error_text, + extra_tags="alert alert-danger") + return redirect(urlresolvers.reverse('network_balance_limit')) + messages.success(request, + ''.join(success), + extra_tags="alert alert-success") + return redirect(urlresolvers.reverse('network_balance_limit')) \ No newline at end of file From 847fc80c7c9c5f2f962965099040e3cbc94cc9e6 Mon Sep 17 00:00:00 2001 From: Shiv K Sah Date: Fri, 2 Jun 2017 20:48:59 +0530 Subject: [PATCH 2/7] Denomination --- cloud/endagaweb/models.py | 20 ++ .../network_detail/denomination.html | 305 ++++++++++++++++++ .../dashboard/network_detail/nav.html | 6 + cloud/endagaweb/tests/test_denomination.py | 120 +++++++ cloud/endagaweb/urls.py | 3 + cloud/endagaweb/views/django_tables.py | 51 +++ cloud/endagaweb/views/network.py | 194 ++++++++++- 7 files changed, 698 insertions(+), 1 deletion(-) create mode 100644 cloud/endagaweb/templates/dashboard/network_detail/denomination.html create mode 100644 cloud/endagaweb/tests/test_denomination.py diff --git a/cloud/endagaweb/models.py b/cloud/endagaweb/models.py index 502aa19a..f6fdc45d 100644 --- a/cloud/endagaweb/models.py +++ b/cloud/endagaweb/models.py @@ -1463,6 +1463,26 @@ def create_ledger(sender, instance, created, **kwargs): post_save.connect(Network.create_billing_tiers, sender=Network) +class NetworkDenomination(models.Model): + """Network has its own denomination bracket for rechange and validity + + Subscriber status depends on recharge under denomination bracket + """ + start_amount = models.BigIntegerField() + end_amount = models.BigIntegerField() + validity_days = models.PositiveIntegerField(blank=True, default=0) + + # The denomination group associated with the network + network = models.ForeignKey('Network', null=True, on_delete=models.CASCADE) + + def __unicode__(self): + return "Amount %s - %d for %s(days)" % ( + self.start_amount, self.end_amount, self.validity_days) + + class Meta: + ordering = ('start_amount',) + + class ConfigurationKey(models.Model): """A key->value mapping for storing settings. diff --git a/cloud/endagaweb/templates/dashboard/network_detail/denomination.html b/cloud/endagaweb/templates/dashboard/network_detail/denomination.html new file mode 100644 index 00000000..197f7f21 --- /dev/null +++ b/cloud/endagaweb/templates/dashboard/network_detail/denomination.html @@ -0,0 +1,305 @@ +{% extends "dashboard/layout.html" %} +{% comment %} +Copyright (c) 2016-present, Facebook, Inc. +All rights reserved. + +This source code is licensed under the BSD-style license found in the +LICENSE file in the root directory of this source tree. An additional grant +of patent rights can be found in the PATENTS file in the same directory. +{% endcomment %} +{% load apptags %} +{% load humanize %} +{% load crispy_forms_tags %} +{% load render_table from django_tables2 %} + + +{% block title %} + {% if network.name %} + {% tmpl_const "SITENAME" %} | "{{ network.name }}" + {% else %} + {% tmpl_const "SITENAME" %} | Network + {% endif %} +{% endblock %} + +{% block pagestyle %} + +{% endblock %} + +{% block content %} + {% include "dashboard/network_detail/header.html" with network=network %} + +
+ {% include "dashboard/network_detail/nav.html" with active_tab='network-denominations' %} + +
+ {% for message in messages %} +
+ × + {{ message }} +
+ {% endfor %} +
+ {% if denomination %} + {% render_table denominations_table %} + {% else %} +

There are currently no denominations associated with this network.

+ {% endif %} +
+
+ {% if user_profile.user.is_staff %} +
+
+
Create Denomination
+
+
+
{% csrf_token %} +
+
+ +
+ +
+
+
+ +
+
+ +
+ +
+
+
+ +
+
+ +
+ +
+
+
+ +
+
+
+ + + +
+
+
+
+
+
+
+
+ {% endif %} + +
+ + + +{% endblock %} +{% block js %} + + + + +{% endblock %} diff --git a/cloud/endagaweb/templates/dashboard/network_detail/nav.html b/cloud/endagaweb/templates/dashboard/network_detail/nav.html index 86121c06..c26a10a1 100644 --- a/cloud/endagaweb/templates/dashboard/network_detail/nav.html +++ b/cloud/endagaweb/templates/dashboard/network_detail/nav.html @@ -23,6 +23,12 @@ {% endif %} ">Prices {% endcomment %} +