Cleanup FAT edit page tables with pagination.

Cleanup FAT statistics generation with smarter query.
This commit is contained in:
Adarnof 2016-12-13 17:31:36 -05:00
parent 6fd3c32ba0
commit 6ba084c710
2 changed files with 81 additions and 59 deletions

View File

@ -2,13 +2,13 @@
{% load bootstrap %} {% load bootstrap %}
{% load staticfiles %} {% load staticfiles %}
{% load i18n %} {% load i18n %}
{% load bootstrap_pagination %}
{% block title %}Alliance Auth{% endblock %} {% block title %}Alliance Auth{% endblock %}
{% block page_title %}{% trans "Fatlink view" %}{% endblock page_title %} {% block page_title %}{% trans "Fatlink view" %}{% endblock page_title %}
{% block content %} {% block content %}
<div class="col-lg-12"> <div class="col-lg-12">
<h1 class="page-header text-center">{% blocktrans %}Edit fatlink "{{ fatlink.name }}"{% endblocktrans %} <h1 class="page-header text-center">{% blocktrans %}Edit fatlink "{{ fatlink }}"{% endblocktrans %}
<div class="text-right"> <div class="text-right">
<form> <form>
<button type="submit" onclick="return confirm('Are you sure?')" class="btn btn-danger" name="deletefat" value="True"> <button type="submit" onclick="return confirm('Are you sure?')" class="btn btn-danger" name="deletefat" value="True">
@ -17,36 +17,44 @@
</form> </form>
</div> </div>
</h1> </h1>
<h4><b>{% trans "Registered characters" %}</b></h4> <div class="panel panel-default">
<table class="table table-responsive table-bordered"> <div class="panel-heading">{% trans "Registered characters" %}</div>
<tr> <div class="panel-body">
<th class="text-center">{% trans "User" %}</th> <div class="text-center">
<th class="text-center">{% trans "Character" %}</th> {% bootstrap_paginate registered_fats range=10 %}
<th class="text-center">{% trans "System" %}</th> </div>
<th class="text-center">{% trans "Ship" %}</th> <table class="table table-responsive table-hover">
<th class="text-center">{% trans "Eve Time" %}</th> <tr>
<th></th> <th class="text-center">{% trans "User" %}</th>
</tr> <th class="text-center">{% trans "Character" %}</th>
{% for fat in registered_fats %} <th class="text-center">{% trans "System" %}</th>
<tr> <th class="text-center">{% trans "Ship" %}</th>
<td class="text-center">{{ fat.user }}</td> <th class="text-center">{% trans "Eve Time" %}</th>
<td class="text-center">{{ fat.character.character_name }}</td> <th></th>
{% if fat.station != "No Station" %} </tr>
<td class="text-center">Docked in {{ fat.system }}</td> {% for fat in registered_fats %}
{% else %} <tr>
<td class="text-center">{{ fat.system }}</td> <td class="text-center">{{ fat.user }}</td>
{% endif %} <td class="text-center">{{ fat.character.character_name }}</td>
<td class="text-center">{{ fat.shiptype }}</td> {% if fat.station != "No Station" %}
<td class="text-center">{{ fat.fatlink.fatdatetime }}</td> <td class="text-center">Docked in {{ fat.system }}</td>
<td class="text-center"> {% else %}
<form> <td class="text-center">{{ fat.system }}</td>
<button type="submit" class="btn btn-warning" name="removechar" value="{{ fat.character.character_id }}"><span {% endif %}
class="glyphicon glyphicon-remove"></span></button> <td class="text-center">{{ fat.shiptype }}</td>
</form> <td class="text-center">{{ fat.fatlink.fatdatetime }}</td>
</td> <td class="text-center">
</tr> <form>
{% endfor %} <button type="submit" class="btn btn-warning" name="removechar" value="{{ fat.character.character_id }}">
</table> <span class="glyphicon glyphicon-remove"></span>
</button>
</form>
</td>
</tr>
{% endfor %}
</table>
</div>
</div>
</div> </div>

View File

@ -7,6 +7,7 @@ from django.contrib.auth.decorators import permission_required
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.utils import timezone from django.utils import timezone
from django.contrib import messages from django.contrib import messages
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from eveonline.models import EveCharacter from eveonline.models import EveCharacter
from eveonline.models import EveCorporationInfo from eveonline.models import EveCorporationInfo
from eveonline.managers import EveManager from eveonline.managers import EveManager
@ -17,8 +18,6 @@ from esi.decorators import token_required
from slugify import slugify from slugify import slugify
from collections import OrderedDict
import string import string
import random import random
import datetime import datetime
@ -27,15 +26,27 @@ import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
FATS_PER_PAGE = int(getattr(settings, 'FATS_PER_PAGE', 20))
def get_page(model_list, page_num):
p = Paginator(model_list, FATS_PER_PAGE)
try:
fats = p.page(page_num)
except PageNotAnInteger:
fatss = p.page(1)
except EmptyPage:
fatss = p.page(p.num_pages)
return fats
class CorpStat(object): class CorpStat(object):
def __init__(self, corp_id, corp=None, blue=False): def __init__(self, corp_id, start_of_month, start_of_next_month, corp=None):
if corp: if corp:
self.corp = corp self.corp = corp
else: else:
self.corp = EveCorporationInfo.objects.get(corporation_id=corp_id) self.corp = EveCorporationInfo.objects.get(corporation_id=corp_id)
self.n_fats = 0 self.n_fats = Fat.objects.filter(character__corporation_id=self.corp.corporation_id).filter(fatlink__fatdatetime__gte=start_of_month).filter(fatlink__fatdatetime__lte=start_of_next_month).count()
self.blue = blue self.blue = self.corp.is_blue
def avg_fat(self): def avg_fat(self):
return "%.2f" % (float(self.n_fats) / float(self.corp.member_count)) return "%.2f" % (float(self.n_fats) / float(self.corp.member_count))
@ -83,34 +94,35 @@ def fatlink_statistics_view(request, year=datetime.date.today().year, month=date
start_of_next_month = first_day_of_next_month(year, month) start_of_next_month = first_day_of_next_month(year, month)
start_of_previous_month = first_day_of_previous_month(year, month) start_of_previous_month = first_day_of_previous_month(year, month)
fatStats = OrderedDict() fat_stats = {}
# get FAT stats for member corps
if settings.IS_CORP: if settings.IS_CORP:
fatStats[settings.CORP_NAME] = CorpStat(settings.CORP_ID) fat_stats[settings.CORP_ID] = CorpStat(settings.CORP_ID, start_of_month, start_of_next_month)
else: else:
alliance_corps = EveCorporationInfo.objects.filter(alliance__alliance_id=settings.ALLIANCE_ID) alliance_corps = EveCorporationInfo.objects.filter(alliance__alliance_id=settings.ALLIANCE_ID)
for corp in alliance_corps: for corp in alliance_corps:
fatStats[corp.corporation_name] = CorpStat(corp.corporation_id, corp=corp) fat_stats[corp.corporation_id] = CorpStat(corp.corporation_id, start_of_month, start_of_next_month)
fatlinks_in_span = Fatlink.objects.filter(fatdatetime__gte=start_of_month).filter( # get FAT stats for corps not in alliance
fatdatetime__lt=start_of_next_month) fats_in_span = Fat.objects.filter(fatlink__fatdatetime__gte=start_of_month).filter(
fatlink__fatdatetime__lt=start_of_next_month).exclude(character__corporation_id__in=fat_stats)
for fatlink in fatlinks_in_span: for fat in fats_in_span:
fats_in_fatlink = Fat.objects.filter(fatlink=fatlink) if not fat.character.corporation_id in fat_stats:
for fat in fats_in_fatlink: fat_stats[fat.character.corporation_id] = CorpStat(fat.character.corporation_id, start_of_month, start_of_next_month)
fatStats.setdefault(fat.character.corporation_name,
CorpStat(fat.character.corporation_id, blue=True)
).n_fats += 1
fatStatsList = [fatStat for corp_name, fatStat in fatStats.items()] # collect and sort stats
fatStatsList.sort(key=lambda stat: stat.corp.corporation_name) stat_list = [fat_stats[x] for x in fat_stats]
fatStatsList.sort(key=lambda stat: (stat.n_fats, stat.n_fats / stat.corp.member_count), reverse=True) stat_list.sort(key=lambda stat: stat.corp.corporation_name)
stat_list.sort(key=lambda stat: (stat.n_fats, stat.n_fats / stat.corp.member_count), reverse=True)
if datetime.datetime.now() > start_of_next_month: if datetime.datetime.now() > start_of_next_month:
context = {'fatStats': fatStatsList, 'month': start_of_month.strftime("%B"), 'year': year, context = {'fatStats': stat_list, 'month': start_of_month.strftime("%B"), 'year': year,
'previous_month': start_of_previous_month, 'next_month': start_of_next_month} 'previous_month': start_of_previous_month, 'next_month': start_of_next_month}
else: else:
context = {'fatStats': fatStatsList, 'month': start_of_month.strftime("%B"), 'year': year, context = {'fatStats': stat_list, 'month': start_of_month.strftime("%B"), 'year': year,
'previous_month': start_of_previous_month} 'previous_month': start_of_previous_month}
return render(request, 'fleetactivitytracking/fatlinkstatisticsview.html', context=context) return render(request, 'fleetactivitytracking/fatlinkstatisticsview.html', context=context)
@ -276,24 +288,26 @@ def create_fatlink_view(request):
def modify_fatlink_view(request, hash=""): def modify_fatlink_view(request, hash=""):
logger.debug("modify_fatlink_view called by user %s" % request.user) logger.debug("modify_fatlink_view called by user %s" % request.user)
if not hash: if not hash:
return redirect('/fat/') return redirect('auth_fatlink_view')
fatlink = Fatlink.objects.filter(hash=hash)[0] fatlink = Fatlink.objects.filter(hash=hash)[0]
if request.GET.get('removechar'): if request.GET.get('removechar', None):
character_id = request.GET.get('removechar') character_id = request.GET.get('removechar')
character = EveCharacter.objects.get(character_id=character_id) character = EveCharacter.objects.get(character_id=character_id)
logger.debug("Removing character %s from fleetactivitytracking %s" % (character.character_name, fatlink.name)) logger.debug("Removing character %s from fleetactivitytracking %s" % (character.character_name, fatlink.name))
Fat.objects.filter(fatlink=fatlink).filter(character=character).delete() Fat.objects.filter(fatlink=fatlink).filter(character=character).delete()
if request.GET.get('deletefat'): if request.GET.get('deletefat', None):
logger.debug("Removing fleetactivitytracking %s" % fatlink.name) logger.debug("Removing fleetactivitytracking %s" % fatlink.name)
fatlink.delete() fatlink.delete()
return redirect('/fat/') return redirect('auth_fatlink_view')
registered_fats = Fat.objects.filter(fatlink=fatlink).order_by('character') registered_fats = Fat.objects.filter(fatlink=fatlink).order_by('character__character_name')
context = {'fatlink': fatlink, 'registered_fats': registered_fats} fat_page = get_page(registered_fats, request.GET.get('page', 1))
context = {'fatlink': fatlink, 'registered_fats': fat_page}
return render(request, 'fleetactivitytracking/fatlinkmodify.html', context=context) return render(request, 'fleetactivitytracking/fatlinkmodify.html', context=context)