Skip to content
This repository was archived by the owner on Sep 26, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions cloud/endagaweb/forms/dashboard_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,46 @@ 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"""

max_balance_title = 'Maximum account balance of an imsi within a network.'
max_balance = forms.CharField(required=False, label="Maximum Balance Limit",
max_length=10,
widget=forms.TextInput(attrs = {'title': max_balance_title}))
max_unsuccessful_transaction_title ='Maximum consecutive failure ' \
'transactions an imsi can perform ' \
'within 24 hrs.'
max_unsuccessful_transaction = forms.CharField(required=False, max_length=3,
label='Maximum Permissible '
'Unsuccessful Transactions',
widget=forms.TextInput
(attrs={'title': max_unsuccessful_transaction_title}))

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('max_balance', 'max_unsuccessful_transaction')

def clean_network_balance(self):
cleaned_data = super(NetworkBalanceLimit, self).clean()
max_balance = self.cleaned_data.get('max_balance', None)
max_unsuccessful_transaction = self.cleaned_data.get(
'max_unsuccessful_transaction', None)
if max_balance == "" and max_unsuccessful_transaction == "":
raise forms.ValidationError('Error : please provide value.')
if max_balance != "":
if float(max_balance) <= 0:
raise forms.ValidationError(
'Error : enter positive and non-zero value for '
'maximum balance Limit.')
return cleaned_data
3 changes: 3 additions & 0 deletions cloud/endagaweb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_balance = models.BigIntegerField(default=10000)
max_failure_transaction = models.PositiveIntegerField(blank=True, default=3)

class Meta:
permissions = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@
Start Amount <span class="aseriskField">*</span>
</label>
<div class="input-group col-xs-8">
<input class="form-control number" type="text" id="start_amount" name="start_amount" autocomplete="off"
<span class="has-tip tip-top" data-tooltip aria-haspopup="true" title="Start range for rate plan.">
<input class="form-control number" type="text" id="start_amount" name="start_amount"
placeholder="Enter start amount" maxlength="10" required/>
</span>
</div>
</div>
</div>
Expand All @@ -83,8 +85,10 @@
End Amount <span class="aseriskField">*</span>
</label>
<div class="input-group col-xs-8">
<input class="form-control number" type="text" id="end_amount" name="end_amount" autocomplete="off"
<span class="has-tip tip-top" data-tooltip aria-haspopup="true" title="End range for rate plan.">
<input class="form-control number" type="text" id="end_amount" name="end_amount"
placeholder="Enter end amount" maxlength="10" required/>
</span>
</div>
</div>
</div>
Expand All @@ -95,8 +99,10 @@
Validity (days)<span class="aseriskField">*</span>
</label>
<div class="input-group col-xs-8">
<input class="form-control" type="text" id="validity_days" name="validity_days" autocomplete="off"
<span class="has-tip tip-top" data-tooltip aria-haspopup="true" title="Validity for rate plan.">
<input class="form-control" type="text" id="validity_days" name="validity_days"
placeholder="Enter validity as number of days" required/>
</span>
</div>
</div>
</div>
Expand Down
6 changes: 6 additions & 0 deletions cloud/endagaweb/templates/dashboard/network_detail/nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,11 @@
{% else %}{% url 'network-edit' %}
{% endif %}">Edit</a>
</li>
<li role="presentation" class="{% if active_tab == 'security' %} active {% endif %}">
<a href="
{% if active_tab == 'security' %} #
#{% else %}{% url 'network_balance_limit' %}
{% endif %}">Security</a>
</li>
</ul>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{% 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 %}
<style>
#messages-container .alert {
margin-top: 20px;
}
#div_id_autoupgrade_enabled .controls, #div_id_autoupgrade_in_window .controls {
margin-left: 15px;
}
</style>
{% endblock %}


{% block content %}


{% include "dashboard/network_detail/header.html" with network=network %}


<div class = 'row'>
{% include "dashboard/network_detail/nav.html" with active_tab='security'%}

<div class='content col-xs-12 col-md-4'>
<div class='row'>
<div class='col-sm-12'>
{% if user_profile.user.is_staff %}
{% crispy network_balance_limit_form %}
{% endif %}
<p class='col-xs-12'>Network current maximum balance limit: {% currency network.max_balance %}</p>
<p class='col-xs-12'>Maximum permissible unsuccessful transactions current limit: {{ network.max_failure_transaction }}</p>
</div>
<div class='col-sm-12'>
{% for message in messages %}
<div class="{{ message.tags }} message">{{ message }}</div>
{% endfor %}
</div>
</div>

</div>
</div>
{% endblock %}
{% block js %}

<script type="text/javascript">
$(document).ready(function () {
$('#id_max_balance').keypress(function (event) {
var $this = $(this);
if ((event.which != 46 || $this.val().indexOf('.') != -1) &&
((event.which < 48 || event.which > 57) &&
(event.which != 0 && event.which != 8))) {
event.preventDefault();
}
var text = $(this).val();
if ((event.which == 46) && (text.indexOf('.') == -1)) {
if(text.length>= 10){
event.preventDefault();
return;
}
setTimeout(function () {
if ($this.val().substring($this.val().indexOf('.')).length > 3) {
$this.val($this.val().substring(0, $this.val().indexOf('.') + 3));
}
}, 1);
}
if ((text.indexOf('.') != -1) &&
(text.substring(text.indexOf('.')).length > 2) &&
(event.which != 0 && event.which != 8) &&
($(this)[0].selectionStart >= text.length - 2)) {
event.preventDefault();
}
});
$("#id_max_unsuccessful_transaction").keydown(function (e) {
// Allow: backspace, delete, tab, escape and enter
if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110]) !== -1 ||
// Allow: Ctrl+A, Command+A
(e.keyCode === 65 && (e.ctrlKey === true || e.metaKey === true)) ||
// Allow: home, end, left, right, down, up
(e.keyCode >= 35 && e.keyCode <= 40)) {
// let it happen, don't do anything
return;
}
// Ensure that it is a number and stop the keypress
if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault();
}
});
});

</script>

{% endblock %}

106 changes: 106 additions & 0 deletions cloud/endagaweb/tests/test_network_limit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"""Tests for NetworkBalanceLimit form

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.
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from django import test
from django.test import TestCase
from endagaweb import models


class TestBase(TestCase):

@classmethod
def setUpClass(cls):
cls.username = 'testuser'
cls.password = 'testpw'
cls.user = models.User(username=cls.username, email='y@l.com')
cls.user.set_password(cls.password)
cls.user.save()
cls.user_profile = models.UserProfile.objects.get(user=cls.user)
cls.uuid = "59216199-d664-4b7a-a2db-6f26e9a5d208"
# Create a test client.
cls.client = test.Client()

@classmethod
def tearDownClass(cls):
cls.user.delete()
cls.user_profile.delete()

def tearDown(self):
self.logout()

def login(self):
"""Log the client in."""
data = {
'email': self.username,
'password': self.password,
}
self.client.post('/auth/', data)

def logout(self):
"""Log the client out."""
self.client.get('/logout')


class NetworkLimitUITest(TestBase):
"""Testing Network Limit UI."""

def test_network_balance_limit_unauth_get_request(self):
self.logout()
response = self.client.get('/dashboard/network/balance-limit')
self.assertEqual(302, response.status_code)

def test_network_balance_limit_auth_get_request(self):
self.login()
response = self.client.get('/dashboard/network/balance-limit')
self.assertEqual(200, response.status_code)

def test_post_bad_response_with_invalid_input_limits(self):
self.login()
data = {
'max_balances': 1,
'max_unsuccessful_transaction': 2,

}
response = self.client.post('/dashboard/network/balance-limit', data)
self.assertEqual(400, response.status_code)

def test_post_bad_response_with_invalid_input_transactions(self):
self.login()
data = {
'max_balance': 1,
'max_unsuccessful_transactions': 2,

}
response = self.client.post('/dashboard/network/balance-limit', data)
self.assertEqual(400, response.status_code)

def test_post_response_redirect_status_code(self):
self.login()
data = {
'max_balance': 4,
'max_unsuccessful_transaction': 6,

}
response = self.client.post('/dashboard/network/balance-limit', data)
self.assertEqual(302, response.status_code)

def test_post_response_redirect_url(self):
self.login()
data = {
'max_balance': 4,
'max_unsuccessful_transaction': 6,

}
response = self.client.post('/dashboard/network/balance-limit', data)
self.assertEqual('/dashboard/network/balance-limit', response.url)
4 changes: 4 additions & 0 deletions cloud/endagaweb/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@
name='network-edit'),
url(r'^dashboard/network/select/(?P<network_id>[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(),
Expand Down
1 change: 1 addition & 0 deletions cloud/endagaweb/views/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ def post(self, request, imsi=None):
return HttpResponseBadRequest()
error_text = 'Error: credit value must be between -10M and 10M.'
try:

currency = network.subscriber_currency
amount = parse_credits(request.POST['amount'],
CURRENCIES[currency]).amount_raw
Expand Down
Loading