def nation_overview(request): context = {} if request.method == "POST": if 'search' in request.POST: form = viewplayerform(request.POST) if form.is_valid(): name = form.cleaned_data['player'] ncount = Nation.objects.filter(name__icontains=name).count() ucount = Nation.objects.filter( user__username__icontains=name).count() context.update({ 'nation_name_matches': Nation.objects.filter(name__icontains=name), 'username_matches': Nation.objects.filter(user__username__icontains=name), 'search_query': name, }) else: context.update({'result': 'Invalid input'}) query = Nation.objects.filter(deleted=False, reset=False) paginator, actionlist = utils.paginate_me(query, 50, request.GET.get('page', 1)) context.update({ 'pages': utils.pagination(paginator, actionlist), 'nations': actionlist, 'searchform': viewplayerform(), }) return render(request, 'mod/nations.html', context)
def reports(request): context = {} nation = request.user.nation result = False page = (request.GET['page'] if 'page' in request.GET else 1) if request.method == "POST": if 'claim' in request.POST: claimed = Report.objects.get(pk=request.POST['claim']) result = claim(claimed, nation) elif 'release' in request.POST: claimed = nation.investigated.get(pk=request.POST['release']) claimed.investigator = None claimed.save(update_fields=['investigator']) result = "Report has been released." reportquery = Report.objects.all() paginator, actionlist = utils.paginate_me(reportquery, 50, page) context.update({ 'result': result, 'pages': utils.pagination(paginator, actionlist), 'reports': actionlist, }) return render(request, 'mod/reports.html', context)
def aidpage(request, nation_id): nation = utils.get_player(nation_id) order = request.COOKIES.get('order_by', '-timestamp') try: if '-' in order: Aid._meta.get_field(order[1:]) else: Aid._meta.get_field(order) except: order = "-timestamp" aid = Aid.objects.filter(Q(sender=nation) | Q(reciever=nation)).order_by(order) pager, logs = utils.paginate_me(aid, 25, request.GET.get('page', 1)) direction = "up" if '-' in order: direction = "down" order = order[1:] context = { 'target': nation, 'pages': utils.pagination(pager, logs), 'aids': logs, 'ordering': order, 'direction': 'arrow-' + direction, } """ disabled because not implemented for sqlite incoming = calculaid(nation.incoming_aid, 'sender') if incoming: context.update({'incoming': {'player': incoming[0], 'count': incoming[1]}}) outgoing = calculaid(nation.outgoing_aid, 'reciever') if outgoing: context.update({'outgoing': {'player': outgoing[0], 'count': outgoing[1]}}) """ #This section is for the totals regarding aid #like how much money has been sent or recieved #in a specfic order so dicts don't derp it #this data should be cached at some point to avoid the excess database hits ordering = [ 'budget', 'rm', 'mg', 'oil', 'food', 'troops', 'weapons', 'research', 'uranium', 'nuke' ] totals = [] for resource in ordering: total_in = nation.incoming_aid.filter(resource=resource).aggregate( total=Sum('amount'))['total'] total_in = (total_in if total_in != None else 0) total_out = nation.outgoing_aid.filter(resource=resource).aggregate( total=Sum('amount'))['total'] total_out = (total_out if total_out != None else 0) totals.append({ 'resource': v.aidnames[resource], 'incoming': total_in, 'outgoing': total_out }) context.update({'totals': totals}) return render(request, 'mod/aidpage.html', context)
def alliancerankings(request): #this big chunk retrieves alliances ordered by highest membercount alliances = Alliance.objects.annotate( membercount=Count('members')).order_by('-membercount') paginator, alliancelist = utils.paginate_me(alliances, 10, request.GET.get('page', 1)) context = { 'pages': utils.pagination(paginator, alliancelist), 'alliances': alliancelist, } return render(request, 'alliance/rankings.html', context)
def basedetails(request, nation_id, manager, pcheck, var): nation = request.user.nation target = utils.get_player(nation_id) if target == False: return render(request, 'mod/not_found.html') context = {'target': target} utils.pagecheck(nation, target, pcheck) query = getattr(target, manager).all().order_by('-pk') paginator, actionlist = utils.paginate_me(query, 50, page) context.update({ 'pages': utils.pagination(paginator, actionlist), var: actionlist, }) return context
def nation_logins(request, nation_id): nation = request.user.nation target = utils.get_player(nation_id) if target == False: return render(request, 'mod/not_found.html') context = {'target': target} page = (request.GET['page'] if 'page' in request.GET else 1) utils.pagecheck(nation, target, "all wars") query = target.login_times.all().order_by('-pk') paginator, actionlist = utils.paginate_me(query, 50, page) context.update({ 'pages': utils.pagination(paginator, actionlist), 'logins': actionlist, }) return details(request, nation_id, page, 'logins')
def bankinterface(request, page): context = {} nation = Nation.objects.select_related('alliance', 'permissions', 'permissions__template', 'alliance__bank').get(user=request.user) permissions = nation.permissions alliance = nation.alliance result = '' if not permissions.can_banking(): #needs officer :DDDDD return redirect('alliance:main') ############# ## log part ############# if request.POST: if 'delete' in request.POST and permissions.logchange: try: alliance.bank_logs.filter(pk=request.POST['delete']).update(deleted=True) result = "Log entry deleted!" nation.actionlogging('deleted banklog') except: result = "Log entry doesn't exist!" paginator = Paginator(alliance.bank_logs.filter(deleted=False).order_by('-pk'), 50) page = int(page) try: logentries = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. logentries = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. logentries = paginator.page(paginator.num_pages) ############3######## ## info display part ########### #using a bankstats object for the member functions #easy to total #also means there's no need to fill out 0s when a field isn't added incomebreakdown = Bankstats() incomebreakdown.populate(turnchange.alliancetotal(alliance, display=True)) context.update({ 'bankstats': incomebreakdown, 'bank': alliance.bank, 'result': result, 'logentries': logentries, 'pages': utils.pagination(paginator, logentries) }) return render(request, 'alliance/bank.html', context)
def nation_allaid(request, nation_id): nation = request.user.nation target = utils.get_player(nation_id) if target == False: return render(request, 'mod/not_found.html') context = {'target': target} page = (request.GET['page'] if 'page' in request.GET else 1) utils.pagecheck(nation, target, "all aid") query = Aidlog.objects.filter(Q(sender=target) | Q(reciever=target)).order_by('-pk') paginator, actionlist = utils.paginate_me(query, 50, page) context.update({ 'pages': utils.pagination(paginator, actionlist), 'aidlist': actionlist, }) return render(request, 'mod/allaid.html', context)
def alliancedeclarations(request): context = {'declarationform': declarationform()} nation = request.user.nation if request.method == 'POST': context.update({'result': oa.declare(nation, request.POST)}) declarations = Alliancedeclaration.objects.select_related( 'nation', 'nation__settings', 'alliance').all().order_by('-pk') paginator, declist = utils.paginate_me(declarations, 10, request.GET.get('page', 1)) context.update({ 'pages': utils.pagination(paginator, declist), 'declarations': declist, }) return render(request, 'alliance/declarations.html', context)
def nation_wars(request, nation_id): nation = request.user.nation target = utils.get_player(nation_id) if target == False: return render(request, 'mod/not_found.html') context = {'target': target} page = (request.GET['page'] if 'page' in request.GET else 1) utils.pagecheck(nation, target, "all wars") query = War.objects.filter(Q(attacker=target) | Q(defender=target)).order_by('-pk') paginator, actionlist = utils.paginate_me(query, 50, request.GET.get('page', 1)) context.update({ 'pages': utils.pagination(paginator, actionlist), 'reports': actionlist, }) return render(request, 'mod/wars.html', context)
def chat(request): context = {} nation = request.user.nation alliance = nation.alliance result = False if request.method == "POST": context.update({'result': ma.post_chat(nation, request.POST)}) chats = Alliancechat.objects.select_related('nation').filter( alliance=alliance).order_by('-pk') paginator, chatslist = utils.paginate_me(chats, 10, request.GET.get('page', 1)) context.update({ 'chatlist': chatslist, 'pages': utils.pagination(paginator, chatslist), 'decform': declarationform(), 'alliance': alliance, }) return render(request, 'alliance/chat.html', context)
def nation_outgoing(request, nation_id): nation = request.user.nation target = utils.get_player(nation_id) if target == False: return render(request, 'mod/not_found.html') page = (request.GET['page'] if 'page' in request.GET else 1) context = { 'target': target, 'title': 'All outgoing aid', 'direction': 'out' } utils.pagecheck(nation, target, "outgoing aid") query = target.outgoing_aid.all().order_by('-pk') paginator, actionlist = utils.paginate_me(query, 50, page) context.update({ 'pages': utils.pagination(paginator, actionlist), 'aidlist': actionlist, }) return render(request, 'mod/aid.html', context)
def bankinterface(request): alliance = request.user.nation.alliance nation = request.user.nation if not nation.permissions.has_permission('banking'): return redirect('alliance:main') result = False if request.POST: if 'delete' in request.POST: result = oa.delete_log(request.user.nation, request.POST) average = alliance.averagegdp context = { 'result': result, 'headers': allianceheaders(request), 'bank': alliance.bank, 'wealthies': alliance.members.filter(gdp__gte=average * 2).count(), 'poorsies': alliance.members.filter(gdp__lte=average / 2).count(), 'middlelowers': alliance.members.filter(Q(gdp__gt=average / 2) & Q(gdp__lte=average)).count(), 'middleuppers': alliance.members.filter(Q(gdp__gt=average) & Q(gdp__lte=average * 2)).count(), } incomebreakdown = Bankstats() incomebreakdown.populate(turnchange.alliancetotal(alliance, display=True)) pager, logs = utils.paginate_me(alliance.bank_logs.all(), 15, request.GET.get('page', 1)) context.update({ 'pages': utils.pagination(pager, logs), 'logentries': logs, 'bankstats': incomebreakdown, }) return render(request, 'alliance/bank.html', context)