Browse Source

optimize

devel
Dmitry Novikov 8 years ago
parent
commit
5f6f28b10e
  1. 4
      abonapp/templates/abonapp/peoples.html
  2. 21
      abonapp/test.sql
  3. 19
      abonapp/views.py
  4. 1
      djing/lib/__init__.py

4
abonapp/templates/abonapp/peoples.html

@ -63,7 +63,7 @@
</thead>
<tbody>
{% with can_ch_trf=perms.tariff_app.change_tariff can_del_abon=perms.abonapp.delete_abon %}
{% for human in peoples %}
{% for human in object_list %}
{% if human.is_active %}
<tr>
{% else %}
@ -76,7 +76,7 @@
{% endif %}
</td>
<td class="col-xs-1">
<a href="{% url 'abonapp:abon_home' human.group.pk human.username %}">{{ human.username }}</a>
<a href="{% url 'abonapp:abon_home' human.group_id human.username %}">{{ human.username }}</a>
</td>
<td class="hidden-xs">
{% if human.statcache %}

21
abonapp/test.sql

@ -0,0 +1,21 @@
SELECT
`base_accounts`.`username`,
`base_accounts`.`telephone`,
`abonent`.`ballance`,
`abonent_tariff`.`tariff_id`,
`tariffs`.`title`,
`tariffs`.`speedIn`,
`tariffs`.`speedOut`,
`tariffs`.`amount`,
`groups`.`title`,
`abon_street`.`name`
FROM `abonent`
INNER JOIN `base_accounts` ON (`abonent`.`baseaccount_ptr_id` = `base_accounts`.`id`)
INNER JOIN `groups` ON (`abonent`.`group_id` = `groups`.`id`)
LEFT OUTER JOIN `abonent_tariff` ON (`abonent`.`current_tariff_id` = `abonent_tariff`.`id`)
LEFT OUTER JOIN `tariffs` ON (`abonent_tariff`.`tariff_id` = `tariffs`.`id`)
LEFT OUTER JOIN `abon_street` ON (`abonent`.`street_id` = `abon_street`.`id`)
LEFT OUTER JOIN `flowcache` ON (`abonent`.`baseaccount_ptr_id` = `flowcache`.`abon_id`)
WHERE (`base_accounts`.`is_admin` = 0 AND `abonent`.`group_id` = 46)
ORDER BY `base_accounts`.`fio` ASC
LIMIT 20;

19
abonapp/views.py

@ -18,7 +18,6 @@ from djing.lib import DuplicateEntry
from jsonview.decorators import json_view
from agent.commands.dhcp import dhcp_commit, dhcp_expiry, dhcp_release
from statistics.models import StatCache
from tariff_app.models import Tariff
from agent import NasFailedResult, Transmitter, NasNetworkError
from . import forms
@ -39,22 +38,20 @@ from djing.global_base_views import OrderedFilteredList, SecureApiView
@method_decorator((login_required, lib.decorators.only_admins), name='dispatch')
class PeoplesListView(OrderedFilteredList):
context_object_name = 'peoples'
template_name = 'abonapp/peoples.html'
def get_queryset(self):
# TODO: optimize that query
street_id = lib.safe_int(self.request.GET.get('street'))
gid = lib.safe_int(self.kwargs.get('gid'))
peoples_list = models.Abon.objects.filter(group__pk=gid)
if street_id > 0:
peoples_list = peoples_list.filter(street=street_id)
peoples_list = peoples_list.select_related(
'group', 'street', 'current_tariff', 'statcache'
'group', 'street', 'statcache', 'current_tariff'
).only(
'group', 'street', 'current_tariff', 'statcache', 'fio',
'group', 'street', 'statcache', 'fio',
'street', 'house', 'telephone', 'ballance', 'markers',
'username', 'is_active'
'username', 'is_active', 'current_tariff'
)
ordering = self.get_ordering()
if ordering and isinstance(ordering, str):
@ -72,7 +69,7 @@ class PeoplesListView(OrderedFilteredList):
context = super(PeoplesListView, self).get_context_data(**kwargs)
context['streets'] = models.AbonStreet.objects.filter(group=gid)
context['streets'] = models.AbonStreet.objects.filter(group=gid).only('name')
context['street_id'] = lib.safe_int(self.request.GET.get('street'))
context['group'] = group
return context
@ -446,9 +443,7 @@ def pick_tariff(request, gid, uname):
@permission_required('abonapp.delete_abontariff')
def unsubscribe_service(request, gid, uname, abon_tariff_id):
try:
#abon = get_object_or_404(models.Abon, username=uname)
abon_tariff = get_object_or_404(models.AbonTariff, pk=int(abon_tariff_id))
#abon.disable_on_nas()
abon_tariff.delete()
messages.success(request, _('User has been detached from service'))
except NasFailedResult as e:
@ -701,7 +696,7 @@ def abon_ping(request):
@login_required
def vcards(r):
abons = models.Abon.objects.exclude(group=None).select_related('group', 'street').only(
users = models.Abon.objects.exclude(group=None).select_related('group', 'street').only(
'username', 'fio', 'group__title', 'telephone',
'street__name', 'house'
)
@ -715,7 +710,7 @@ def vcards(r):
"END:VCARD\r\n")
def _make_vcard():
for ab in abons.iterator():
for ab in users.iterator():
tel = ab.telephone
if tel:
yield tmpl % {
@ -1214,7 +1209,7 @@ class DhcpLever(SecureApiView):
@staticmethod
def on_dhcp_event(data: Dict) -> Optional[str]:
"""
data = {
:param data = {
'client_ip': ip_address('127.0.0.1'),
'client_mac': 'aa:bb:cc:dd:ee:ff',
'switch_mac': 'aa:bb:cc:dd:ee:ff',

1
djing/lib/__init__.py

@ -128,6 +128,7 @@ def process_lock(fn):
s.close()
return wrapped
#
# Raises when IntegrityError in db
#

Loading…
Cancel
Save