def get_dates(request): show_in_space = json.loads(request.GET.get('space', 'true')) show_in_stations = json.loads(request.GET.get('stations', 'true')) divisions = extract_divisions(request) since_weeks = int(request.GET.get('since_weeks', '8')) to_weeks = int(request.GET.get('to_weeks', '0')) oldest_date = timezone.now() - timedelta(weeks=since_weeks) newest_date = timezone.now() - timedelta(weeks=to_weeks) query = AssetDiff.objects.all() query = query.filter(date__gte=oldest_date) query = query.filter(date__lte=newest_date) if not show_in_space: query = query.filter(stationID__lt=constants.MAX_STATION_ID) if not show_in_stations: query = query.filter(stationID__gt=constants.MAX_STATION_ID) if divisions is not None: query = query.filter(hangarID__in=divisions) dates = [] for date in query.values_list('date', flat=True).distinct().order_by('-date'): dates.append({ 'value': datetime.strftime(date, DATE_PATTERN), 'show': print_time_min(date), }) return HttpResponse(json.dumps(dates))
def get_dates(request): show_in_space = json.loads(request.GET.get('space', 'true')) show_in_stations = json.loads(request.GET.get('stations', 'true')) divisions = extract_divisions(request) since_weeks = int(request.GET.get('since_weeks', '8')) to_weeks = int(request.GET.get('to_weeks', '0')) oldest_date = timezone.now() - timedelta(weeks=since_weeks) newest_date = timezone.now() - timedelta(weeks=to_weeks) query = AssetDiff.objects.all() query = query.filter(date__gte=oldest_date) query = query.filter(date__lte=newest_date) if not show_in_space: query = query.filter(stationID__lt=constants.MAX_STATION_ID) if not show_in_stations: query = query.filter(stationID__gt=constants.MAX_STATION_ID) if divisions is not None: query = query.filter(hangarID__in=divisions) dates = [] for date in query.values_list('date', flat=True).distinct().order_by('-date'): dates.append({ 'value' : datetime.strftime(date, DATE_PATTERN), 'show' : print_time_min(date), }) return HttpResponse(json.dumps(dates))
def player_list_data(request): try: params = extract_datatable_params(request) except KeyError: return HttpResponseBadRequest() query = User.objects.select_related(depth=2).filter(is_active=True) query = query.annotate(account_count=Count("eve_accounts")) query = query.annotate(char_count=Count("characters")) query = query.annotate(group_count=Count("groups")) query = query.filter(eve_accounts__gt=0) sort_by = PLAYERS_COLUMNS[params.column]['db_field'] # SQL hack for making a case insensitive sort if sort_by == "username": sort_col = "%s_nocase" % sort_by query = query.extra(select={sort_col: 'LOWER("%s")' % sort_by}) else: sort_col = sort_by if not params.asc: sort_col = "-" + sort_col query = query.extra(order_by=[sort_col]) if params.search: total_count = query.count() query = query.filter(username__icontains=params.search) filtered_count = query.count() else: total_count = filtered_count = query.count() player_list = [] for player in query[params.first_id:params.last_id]: player_list.append([ '<a href="/hr/players/%d/" class="player">%s</a>' % (player.id, player.username), player.is_staff and player.is_superuser, player.eve_accounts.all().count(), player.characters.all().count(), player.groups.all().count(), print_time_min(player.last_login), print_time_min(player.date_joined) ]) return datatable_ajax_data(player_list, params.sEcho, total_count, filtered_count)
def player_list_data(request): try: params = extract_datatable_params(request) except KeyError: return HttpResponseBadRequest() query = User.objects.select_related(depth=2).filter(is_active=True) query = query.annotate(account_count=Count("eve_accounts")) query = query.annotate(char_count=Count("characters")) query = query.annotate(group_count=Count("groups")) query = query.filter(eve_accounts__gt=0) sort_by = PLAYERS_COLUMNS[params.column]['db_field'] # SQL hack for making a case insensitive sort if sort_by == "username": sort_col = "%s_nocase" % sort_by query = query.extra(select={sort_col : 'LOWER("%s")' % sort_by}) else: sort_col = sort_by if not params.asc: sort_col = "-" + sort_col query = query.extra(order_by=[sort_col]) if params.search: total_count = query.count() query = query.filter(username__icontains=params.search) filtered_count = query.count() else: total_count = filtered_count = query.count() player_list = [] for player in query[params.first_id:params.last_id]: player_list.append([ '<a href="/hr/players/%d/" class="player">%s</a>' % (player.id, player.username), player.is_staff and player.is_superuser, player.eve_accounts.all().count(), player.characters.all().count(), player.groups.all().count(), print_time_min(player.last_login), print_time_min(player.date_joined) ]) return datatable_ajax_data(player_list, params.sEcho, total_count, filtered_count)
def titles_data(request): try: params = extract_datatable_params(request) corp_id = request.GET.get('corp') except KeyError: return HttpResponseBadRequest() if corp_id: try: query = Corporation.objects.get( corporationID=int(corp_id)).titles.all() except Corporation.DoesNotExist: query = Corporation.objects.mine().titles.all() except ValueError: # corp_id cannot be casted to int, we take all corps query = Title.objects.all() else: query = Corporation.objects.mine().titles.all() sort_by = TITLES_COLUMNS[params.column]['db_field'] if params.column == 0: # SQL hack for making a case insensitive sort sort_col = "%s_nocase" % sort_by query = query.extra(select={sort_col: 'LOWER("%s")' % sort_by}) else: sort_col = sort_by if not params.asc: sort_col = "-" + sort_col query = query.extra(order_by=[sort_col]) # fetch the number of members having each title query = query.extra(select={ "title_members": SQL_TITLE_MEMBERS, "roles_in_title": SQL_ROLES_IN_TITLES }) titles = [] for title in query: if title.title_compo_diffs.all(): modification_date = print_time_min( title.title_compo_diffs.latest('id').date) else: modification_date = "-" titles.append([ title.permalink, title.accessLvl, '<a href="/%s/titles/%d/members/">%d</a>' % (app_prefix, title.id, title.title_members), title.roles_in_title, modification_date ]) return datatable_ajax_data(titles, params.sEcho)
def titles_data(request): try: params = extract_datatable_params(request) corp_id = request.GET.get("corp") except KeyError: return HttpResponseBadRequest() if corp_id: try: query = Corporation.objects.get(corporationID=int(corp_id)).titles.all() except Corporation.DoesNotExist: query = Corporation.objects.mine().titles.all() except ValueError: # corp_id cannot be casted to int, we take all corps query = Title.objects.all() else: query = Corporation.objects.mine().titles.all() sort_by = TITLES_COLUMNS[params.column]["db_field"] if params.column == 0: # SQL hack for making a case insensitive sort sort_col = "%s_nocase" % sort_by query = query.extra(select={sort_col: 'LOWER("%s")' % sort_by}) else: sort_col = sort_by if not params.asc: sort_col = "-" + sort_col query = query.extra(order_by=[sort_col]) # fetch the number of members having each title query = query.extra(select={"title_members": SQL_TITLE_MEMBERS, "roles_in_title": SQL_ROLES_IN_TITLES}) titles = [] for title in query: if title.title_compo_diffs.all(): modification_date = print_time_min(title.title_compo_diffs.latest("id").date) else: modification_date = "-" titles.append( [ title.permalink, title.accessLvl, '<a href="/%s/titles/%d/members/">%d</a>' % (app_prefix, title.id, title.title_members), title.roles_in_title, modification_date, ] ) return datatable_ajax_data(titles, params.sEcho)
def task_list_data(request): try: params = extract_datatable_params(request) except: return HttpResponseBadRequest() tasks = [] query = ScheduledTask.objects.filter(function__in=FUNCTIONS, is_active=True) for task in query.order_by('-priority'): tasks.append([ task.function, task.next_execution_admin_display(), task.frequency_admin_display(), print_time_min(task.last_execution), task.is_last_exec_success, task.is_running, task.is_scheduled, task.as_html() ]) return datatable_ajax_data(tasks, params.sEcho)
def compo_diff_data(request, title_id): try: params = extract_datatable_params(request) except KeyError: return HttpResponseBadRequest() title = get_object_or_404(Title, pk=int(title_id)) query = TitleCompoDiff.objects.filter(title=title).order_by("-date") total_diffs = query.count() query = query[params.first_id:params.last_id] diff_list = [] for diff in query: diff_list.append([ diff.new, diff.role.permalink, diff.role.roleType.permalink, print_time_min(diff.date) ]) return datatable_ajax_data(diff_list, params.sEcho, total_diffs)
def details(request, title_id): title = get_object_or_404(Title, pk=int(title_id)) if title.title_compo_diffs.all(): title.lastModified = print_time_min(title.title_compo_diffs.latest('id')) else: title.lastModified = None title.color = ColorThreshold.get_access_color(title.accessLvl) data = { "title" : title, "member_count" : title.members.count(), "colorThresholds" : ColorThreshold.as_json(), 'roles_columns': ROLES_COLUMNS, 'diffs_columns': DIFFS_COLUMNS, 'datatables_defaults': DATATABLES_DEFAULTS } return render_to_response("ecm/hr/titles/title_details.html", data, Ctx(request))
def changes_data(request): try: params = extract_datatable_params(request) except: return HttpResponseBadRequest() titles = TitleCompoDiff.objects.select_related( depth=1).all().order_by("-date") count = titles.count() changes = titles[params.first_id:params.last_id] change_list = [] for c in changes: change_list.append([ c.new, c.title.permalink, c.role.permalink, print_time_min(c.date) ]) return datatable_ajax_data(change_list, params.sEcho, total=count)
def history_data(request): try: params = extract_datatable_params(request) except: return HttpResponseBadRequest() query = MemberDiff.objects.all() if params.column == 0 or params.column > 3: params.column = 3 params.asc = False sort_col = COLUMNS[params.column]['db_field'] # SQL hack for making a case insensitive sort if params.column in (1, 2): sort_col = sort_col + "_nocase" sort_val = db.fix_mysql_quotes('LOWER("%s")' % COLUMNS[params.column]['db_field']) query = query.extra(select={ sort_col : sort_val }) if not params.asc: sort_col = "-" + sort_col query = query.extra(order_by=([sort_col])) if params.search: total_members = query.count() search_args = Q(name__icontains=params.search) | Q(nickname__icontains=params.search) query = query.filter(search_args) filtered_members = query.count() else: total_members = filtered_members = query.count() members = [] for diff in query[params.first_id:params.last_id]: members.append([ diff.new, diff.permalink, truncate_words(diff.nickname, 5), print_time_min(diff.date) ]) return datatable_ajax_data(members, params.sEcho, total_members, filtered_members)
def changes_data(request): try: params = extract_datatable_params(request) except: return HttpResponseBadRequest() titles = TitleCompoDiff.objects.select_related(depth=1).all().order_by("-date") count = titles.count() changes = titles[params.first_id:params.last_id] change_list = [] for c in changes: change_list.append([ c.new, c.title.permalink, c.role.permalink, print_time_min(c.date) ]) return datatable_ajax_data(change_list, params.sEcho, total=count)
def access_changes_data(request): try: params = extract_datatable_params(request) if params.column == 1: lambda_sort = lambda e: e.member.name.lower() else: lambda_sort = lambda e: e.date except: return HttpResponseBadRequest() roles_query = RoleMemberDiff.objects.select_related(depth=1).all() titles_query = TitleMemberDiff.objects.select_related(depth=1).all() if params.search: total_count = roles_query.count() + titles_query.count() roles_search_args = Q(member__name__icontains=params.search) | Q( role__roleName__icontains=params.search) roles_query = roles_query.filter(roles_search_args) titles_search_args = Q(member__name__icontains=params.search) | Q( title__titleName__icontains=params.search) titles_query = titles_query.filter(titles_search_args) filtered_count = roles_query.count() + titles_query.count() else: total_count = filtered_count = roles_query.count( ) + titles_query.count() changes = list(roles_query) + list(titles_query) changes.sort(key=lambda_sort, reverse=not params.asc) changes = changes[params.first_id:params.last_id] change_list = [] for c in changes: change_list.append([ c.new, c.member_permalink, c.access_permalink, print_time_min(c.date) ]) return datatable_ajax_data(change_list, params.sEcho, total_count, filtered_count)
def details(request, title_id): title = get_object_or_404(Title, pk=int(title_id)) if title.title_compo_diffs.all(): title.lastModified = print_time_min( title.title_compo_diffs.latest('id')) else: title.lastModified = None title.color = ColorThreshold.get_access_color(title.accessLvl) data = { "title": title, "member_count": title.members.count(), "colorThresholds": ColorThreshold.as_json(), 'roles_columns': ROLES_COLUMNS, 'diffs_columns': DIFFS_COLUMNS, 'datatables_defaults': DATATABLES_DEFAULTS } return render_to_response("ecm/hr/titles/title_details.html", data, Ctx(request))
def access_changes_data(request): try: params = extract_datatable_params(request) if params.column == 1: lambda_sort = lambda e: e.member.name.lower() else: lambda_sort = lambda e: e.date except: return HttpResponseBadRequest() roles_query = RoleMemberDiff.objects.select_related(depth=1).all() titles_query = TitleMemberDiff.objects.select_related(depth=1).all() if params.search: total_count = roles_query.count() + titles_query.count() roles_search_args = Q(member__name__icontains=params.search) | Q(role__roleName__icontains=params.search) roles_query = roles_query.filter(roles_search_args) titles_search_args = Q(member__name__icontains=params.search) | Q(title__titleName__icontains=params.search) titles_query = titles_query.filter(titles_search_args) filtered_count = roles_query.count() + titles_query.count() else: total_count = filtered_count = roles_query.count() + titles_query.count() changes = list(roles_query) + list(titles_query) changes.sort(key=lambda_sort, reverse=not params.asc) changes = changes[params.first_id:params.last_id] change_list = [] for c in changes: change_list.append([ c.new, c.member_permalink, c.access_permalink, print_time_min(c.date) ]) return datatable_ajax_data(change_list, params.sEcho, total_count, filtered_count)
def access_changes_member_data(request, characterID): try: params = extract_datatable_params(request) except: return HttpResponseBadRequest() roles = RoleMemberDiff.objects.filter(member=characterID).order_by("-id") titles = TitleMemberDiff.objects.filter(member=characterID).order_by("-id") count = roles.count() + titles.count() changes = list(roles) + list(titles) changes.sort(key=lambda e: e.date, reverse=True) changes = changes[params.first_id:params.last_id] change_list = [] for change in changes: change_list.append([ change.new, change.access_permalink, print_time_min(change.date) ]) return datatable_ajax_data(change_list, params.sEcho, count)
def transactions_data(request): try: params = extract_datatable_params(request) REQ = request.GET if request.method == 'GET' else request.POST params.walletID = int(REQ.get('walletID', 0)) params.entryTypeID = int(REQ.get('entryTypeID', -1)) params.entryForID = int(REQ.get('entryForID', -1)) params.amount = request.GET.get('amount', None) params.comparator = request.GET.get('comparator', 'gt') params.from_date = timezone.make_aware( datetime.strptime(REQ.get('from_date', None), DATE_PATTERN), timezone.get_current_timezone()) params.to_date = timezone.make_aware( datetime.strptime(REQ.get('to_date', None), DATE_PATTERN), timezone.get_current_timezone()) except: return HttpResponseBadRequest() query = TransactionEntry.objects.select_related( depth=1).all().order_by('-date') if params.search or params.walletID or params.entryTypeID or params.amount or ( params.from_date and params.to_date): total_entries = query.count() search_args = Q() if params.search: search_args |= Q(clientName__icontains=params.search) station_ids = list( CelestialObject.objects.filter( itemName__icontains=params.search, group=constants.STATIONS_GROUPID).values_list( 'itemID', flat=True)[:100]) item_ids = list( Type.objects.filter( typeName__icontains=params.search).values_list( 'typeID', flat=True)[:100]) search_args |= Q(stationID__in=station_ids) search_args |= Q(typeID__in=item_ids) if is_number(params.search): search_args |= Q(amount__gte=params.search) if params.walletID: search_args &= Q(wallet=params.walletID) if params.entryTypeID != -1: search_args &= Q(transactionType=params.entryTypeID) if params.entryForID != -1: search_args &= Q(transactionFor=params.entryForID) # Add query amount if params.amount: comparator_map = { 'gt': Q(price__gt=params.amount), 'lt': Q(price__lt=params.amount), 'gte': Q(price__gte=params.amount), 'lte': Q(price__lte=params.amount), 'eq': Q(price=params.amount), 'neq': Q(price__lt=params.amount, price__gt=params.amount), } search_args &= comparator_map[params.comparator] # Add query dates if params.from_date and params.to_date: # + 24 hours on the to date search_args &= Q(date__range=(params.from_date, params.to_date + timedelta(days=1))) query = query.filter(search_args) filtered_entries = query.count() else: total_entries = filtered_entries = query.count() query = query[params.first_id:params.last_id] entries = [] my_corp = Corporation.objects.mine() for entry in query: try: amount = print_float(entry.journal.amount, force_sign=True) except AttributeError: amount = '0.0' try: balance = print_float(entry.journal.balance) except AttributeError: balance = '' entries.append([ print_time_min(entry.date), entry.typeName, entry.price, entry.quantity, amount, balance, truncate_words(entry.stationName, 6), entry.wallet.corp_wallets.get(corp=my_corp).name, ]) json_data = { "sEcho": params.sEcho, "iTotalRecords": total_entries, "iTotalDisplayRecords": filtered_entries, "aaData": entries } return HttpResponse(json.dumps(json_data))
def ecm_datetime(value): try: return unicode(print_time_min(value)) except: return unicode(value)
def journal_data(request): try: params = extract_datatable_params(request) REQ = request.GET if request.method == "GET" else request.POST params.walletID = int(REQ.get("walletID", 0)) params.entryTypeID = int(REQ.get("entryTypeID", 0)) params.amount = request.GET.get("amount", None) params.comparator = request.GET.get("comparator", "gt") params.from_date = timezone.make_aware( datetime.strptime(REQ.get("from_date", None), DATE_PATTERN), timezone.get_current_timezone() ) params.to_date = timezone.make_aware( datetime.strptime(REQ.get("to_date", None), DATE_PATTERN), timezone.get_current_timezone() ) except: return HttpResponseBadRequest() orderBy = journal_cols[params.column] if not params.asc: orderBy = "-" + orderBy query = JournalEntry.objects.select_related(depth=1).all().order_by(orderBy) if params.search or params.walletID or params.entryTypeID or params.amount or (params.from_date and params.to_date): total_entries = query.count() search_args = Q() if params.search: search_args |= Q(ownerName1__icontains=params.search) search_args |= Q(ownerName2__icontains=params.search) search_args |= Q(argName1__icontains=params.search) search_args |= Q(reason__icontains=params.search) if is_number(params.search): search_args |= Q(amount__gte=params.search) if params.walletID: search_args &= Q(wallet=params.walletID) if params.entryTypeID: search_args &= Q(type=params.entryTypeID) # Add query amount if params.amount: comparator_map = { "gt": Q(amount__gt=params.amount), "lt": Q(amount__lt=params.amount), "gte": Q(amount__gte=params.amount), "lte": Q(amount__lte=params.amount), "eq": Q(amount=params.amount), "neq": Q(amount__lt=params.amount, amount__gt=params.amount), } search_args &= comparator_map[params.comparator] # Add query dates if params.from_date and params.to_date: # + 24 hours on the to date search_args &= Q(date__range=(params.from_date, params.to_date + timedelta(days=1))) query = query.filter(search_args) filtered_entries = query.count() else: total_entries = filtered_entries = query.count() query = query[params.first_id : params.last_id] entries = [] # to improve performance try: corp = Corporation.objects.mine() except Corporation.DoesNotExist: corp = Corporation(corporationID=0) members = Member.objects.all() other_entries = JournalEntry.objects.select_related().all() for entry in query: try: owner1 = members.get(characterID=entry.ownerID1).permalink except Member.DoesNotExist: owner1 = entry.ownerName1 try: owner2 = members.get(characterID=entry.ownerID2).permalink except Member.DoesNotExist: owner2 = entry.ownerName2 if entry.type_id == EntryType.BOUNTY_PRIZES: rats = [s.split(":") for s in entry.reason.split(",") if ":" in s] rat_list = [] for rat_id, rat_count in rats: rat_list.append("%s x%s" % (Type.objects.get(typeID=rat_id).typeName, rat_count)) reason = "|".join(rat_list) if reason: reason = (u"Killed Rats in %s|" % entry.argName1) + reason elif entry.type_id == EntryType.PLAYER_DONATION: reason = entry.reason[len("DESC: ") :] if reason: reason = u"Description|" + reason elif entry.type_id == EntryType.CORP_WITHDRAWAL: reason = entry.reason[len("DESC: ") :].strip("\n\t'\" ") reason = (u"Cash transfer by %s|" % entry.argName1) + reason try: if int(entry.ownerID1) == corp.corporationID and int(entry.ownerID2) == corp.corporationID: related_entry = other_entries.filter(refID=entry.refID).exclude(id=entry.id)[0] owner2 = related_entry.wallet.corp_wallets.get(corp=corp).name except: pass else: reason = entry.reason wallet_name = entry.wallet.corp_wallets.get(corp=corp).name entries.append( [ print_time_min(entry.date), wallet_name, entry.type.refTypeName, owner1, owner2, print_float(entry.amount, force_sign=True), print_float(entry.balance), reason, ] ) return datatable_ajax_data(entries, params.sEcho, total_entries, filtered_entries)
def mail_list_data(request): try: params = extract_datatable_params(request) REQ = request.GET if request.method == 'GET' else request.POST except: return HttpResponseBadRequest() total_count = Mail.objects.all().count() recfilter = False #These are mutually exclusive if REQ.get('ally') != '0': recipients = Recipient.objects.filter(object_id=REQ.get('ally')) recfilter = True elif REQ.get('corp') != '0': recipients = Recipient.objects.filter(object_id=REQ.get('corp')) recfilter = True elif REQ.get('maillist') != '0': recipients = Recipient.objects.filter(object_id=REQ.get('maillist')) recfilter = True elif REQ.get('to'): recipients = [] mem = Member.objects.filter(name__contains=REQ.get('to')) recipients += Recipient.objects.filter(object_id__in=mem) cor = Corporation.objects.filter(corporationName__contains=REQ.get('to')) recipients += Recipient.objects.filter(object_id__in=cor) all = Alliance.objects.filter(name__contains=REQ.get('to')) recipients += Recipient.objects.filter(object_id__in=all) recfilter = True else: recipients = [] if recfilter == True: mail = Mail.objects.filter(recipients__in=recipients).distinct().order_by(COLUMNS[params.column]['db_field']) else: mail = Mail.objects.all().order_by(COLUMNS[params.column]['db_field']) if REQ.get('from'): mail = mail.filter(sender__name__contains=REQ.get('from')) if not params.asc: mail = mail.reverse() mail_list = [] if params.format == 'csv': for message in mail[params.first_id:params.last_id]: recp = [] for target in message.recipients.all(): recp.append(str(target)) if len(recp) > 1: recipients = ', '.join(recp) else: recipients = recp[0] mail_list.append([print_time_min(message.sentDate), message.sender.name, recipients, message.title, message.body]) return datatable_csv_data(mail_list, filename='mails.csv') else: for message in mail[params.first_id:params.last_id]: recp = [] for target in message.recipients.all(): recp.append(str(target)) if len(recp) > 1: recipients = ', '.join(recp) else: recipients = recp[0] mail_list.append([print_time_min(message.sentDate), message.sender.name, recipients, message.modal_link, message.messageID]) return datatable_ajax_data(mail_list, params.sEcho, total_count, len(mail))
def mail_list_data(request): try: params = extract_datatable_params(request) REQ = request.GET if request.method == 'GET' else request.POST except: return HttpResponseBadRequest() total_count = Mail.objects.all().count() recfilter = False #These are mutually exclusive if REQ.get('ally') != '0': recipients = Recipient.objects.filter(object_id=REQ.get('ally')) recfilter = True elif REQ.get('corp') != '0': recipients = Recipient.objects.filter(object_id=REQ.get('corp')) recfilter = True elif REQ.get('maillist') != '0': recipients = Recipient.objects.filter(object_id=REQ.get('maillist')) recfilter = True elif REQ.get('to'): recipients = [] mem = Member.objects.filter(name__contains=REQ.get('to')) recipients += Recipient.objects.filter(object_id__in=mem) cor = Corporation.objects.filter( corporationName__contains=REQ.get('to')) recipients += Recipient.objects.filter(object_id__in=cor) all = Alliance.objects.filter(name__contains=REQ.get('to')) recipients += Recipient.objects.filter(object_id__in=all) recfilter = True else: recipients = [] if recfilter == True: mail = Mail.objects.filter( recipients__in=recipients).distinct().order_by( COLUMNS[params.column]['db_field']) else: mail = Mail.objects.all().order_by(COLUMNS[params.column]['db_field']) if REQ.get('from'): mail = mail.filter(sender__name__contains=REQ.get('from')) if not params.asc: mail = mail.reverse() mail_list = [] if params.format == 'csv': for message in mail[params.first_id:params.last_id]: recp = [] for target in message.recipients.all(): recp.append(str(target)) if len(recp) > 1: recipients = ', '.join(recp) else: recipients = recp[0] mail_list.append([ print_time_min(message.sentDate), message.sender.name, recipients, message.title, message.body ]) return datatable_csv_data(mail_list, filename='mails.csv') else: for message in mail[params.first_id:params.last_id]: recp = [] for target in message.recipients.all(): recp.append(str(target)) if len(recp) > 1: recipients = ', '.join(recp) else: recipients = recp[0] mail_list.append([ print_time_min(message.sentDate), message.sender.name, recipients, message.modal_link, message.messageID ]) return datatable_ajax_data(mail_list, params.sEcho, total_count, len(mail))
def journal_data(request): try: params = extract_datatable_params(request) REQ = request.GET if request.method == 'GET' else request.POST params.walletID = int(REQ.get('walletID', 0)) params.entryTypeID = int(REQ.get('entryTypeID', 0)) params.amount = request.GET.get('amount',None) params.comparator = request.GET.get('comparator','gt') params.from_date = timezone.make_aware(datetime.strptime(REQ.get('from_date', None), DATE_PATTERN), timezone.get_current_timezone()) params.to_date = timezone.make_aware(datetime.strptime(REQ.get('to_date', None), DATE_PATTERN), timezone.get_current_timezone()) except: return HttpResponseBadRequest() query = JournalEntry.objects.select_related(depth=1).all().order_by('-date') if params.search or params.walletID or params.entryTypeID or params.amount or (params.from_date and params.to_date): total_entries = query.count() search_args = Q() if params.search: search_args |= Q(ownerName1__icontains=params.search) search_args |= Q(ownerName2__icontains=params.search) search_args |= Q(argName1__icontains=params.search) search_args |= Q(reason__icontains=params.search) if is_number(params.search): search_args |= Q(amount__gte=params.search) if params.walletID: search_args &= Q(wallet=params.walletID) if params.entryTypeID: search_args &= Q(type=params.entryTypeID) # Add query amount if params.amount: comparator_map = { 'gt': Q(amount__gt=params.amount), 'lt': Q(amount__lt=params.amount), 'gte': Q(amount__gte=params.amount), 'lte': Q(amount__lte=params.amount), 'eq': Q(amount=params.amount), 'neq': Q(amount__lt=params.amount, amount__gt=params.amount), } search_args &= comparator_map[params.comparator] # Add query dates if params.from_date and params.to_date: # + 24 hours on the to date search_args &= Q(date__range=(params.from_date, params.to_date + timedelta(days=1))) query = query.filter(search_args) filtered_entries = query.count() else: total_entries = filtered_entries = query.count() query = query[params.first_id:params.last_id] entries = [] # to improve performance try: corp = Corporation.objects.mine() except Corporation.DoesNotExist: corp = Corporation(corporationID=0) members = Member.objects.all() other_entries = JournalEntry.objects.select_related().all() for entry in query: try: owner1 = members.get(characterID=entry.ownerID1).permalink except Member.DoesNotExist: owner1 = entry.ownerName1 try: owner2 = members.get(characterID=entry.ownerID2).permalink except Member.DoesNotExist: owner2 = entry.ownerName2 if entry.type_id == EntryType.BOUNTY_PRIZES: rats = [ s.split(':') for s in entry.reason.split(',') if ':' in s ] rat_list = [] for rat_id, rat_count in rats: rat_list.append('%s x%s' % (Type.objects.get(typeID=rat_id).typeName, rat_count)) reason = '|'.join(rat_list) if reason: reason = (u'Killed Rats in %s|' % entry.argName1) + reason elif entry.type_id == EntryType.PLAYER_DONATION: reason = entry.reason[len('DESC: '):] if reason: reason = u'Description|' + reason elif entry.type_id == EntryType.CORP_WITHDRAWAL: reason = entry.reason[len('DESC: '):].strip('\n\t\'" ') reason = (u'Cash transfer by %s|' % entry.argName1) + reason try: if int(entry.ownerID1) == corp.corporationID and int(entry.ownerID2) == corp.corporationID: related_entry = other_entries.filter(refID=entry.refID).exclude(id=entry.id)[0] owner2 = related_entry.wallet.corp_wallets.get(corp=corp).name except: pass else: reason = entry.reason wallet_name = entry.wallet.corp_wallets.get(corp=corp).name entries.append([ print_time_min(entry.date), wallet_name, entry.type.refTypeName, owner1, owner2, print_float(entry.amount, force_sign=True), print_float(entry.balance), reason, ]) #json_data = { # "sEcho" : params.sEcho, # "iTotalRecords" : total_entries, # "iTotalDisplayRecords" : filtered_entries, # "aaData" : entries #} return datatable_ajax_data(entries, params.sEcho, total_entries, filtered_entries)
params.column = 0 # by default, show latest orders first sort_col = COLUMNS[params.column][1] if not params.asc: sort_col = '-' + sort_col query = query.order_by(sort_col) orders = [] for order in query[params.first_id:params.last_id]: items = [ row.catalog_entry.typeName for row in order.rows.all() ] orders.append([ order.permalink(), order.state_text(), truncate_words(', '.join(items), 6), print_float(order.quote) + ' ISK', print_time_min(order.creation_date()), ]) return datatable_ajax_data(data=orders, echo=params.sEcho, total=order_count, filtered=filtered_count) #------------------------------------------------------------------------------ @check_user_access() def create(request): """ Serves URL /shop/orders/create/ """ if request.method == 'POST': items, valid_order = extract_order_items(request) if valid_order: order = Order.objects.create(originator=request.user)
def details_data(request, supply_id): """ Serves URL /industry/catalog/supplies/<supply_id>//data/ """ try: params = extract_datatable_params(request) supply_id = int(supply_id) except Exception, e: return HttpResponseBadRequest(str(e)) query = PriceHistory.objects.filter(supply=supply_id) histories = [] for history in query[params.first_id:params.last_id]: histories.append([ print_time_min(history.date), print_float(history.price), ]) return datatable_ajax_data(data=histories, echo=params.sEcho) #------------------------------------------------------------------------------ @check_user_access() def update_price(request, supply_id): """ Serves URL /industry/catalog/supplies/<supply_id>/updateprice/ """ try: supply = get_object_or_404(Supply, typeID=int(supply_id)) except ValueError: raise Http404()
def transactions_data(request): try: params = extract_datatable_params(request) REQ = request.GET if request.method == "GET" else request.POST params.walletID = int(REQ.get("walletID", 0)) params.entryTypeID = int(REQ.get("entryTypeID", -1)) params.entryForID = int(REQ.get("entryForID", -1)) params.amount = request.GET.get("amount", None) params.comparator = request.GET.get("comparator", "gt") params.from_date = timezone.make_aware( datetime.strptime(REQ.get("from_date", None), DATE_PATTERN), timezone.get_current_timezone() ) params.to_date = timezone.make_aware( datetime.strptime(REQ.get("to_date", None), DATE_PATTERN), timezone.get_current_timezone() ) except: return HttpResponseBadRequest() query = TransactionEntry.objects.select_related(depth=1).all().order_by("-date") if params.search or params.walletID or params.entryTypeID or params.amount or (params.from_date and params.to_date): total_entries = query.count() search_args = Q() if params.search: search_args |= Q(clientName__icontains=params.search) station_ids = list( CelestialObject.objects.filter( itemName__icontains=params.search, group=constants.STATIONS_GROUPID ).values_list("itemID", flat=True)[:100] ) item_ids = list( Type.objects.filter(typeName__icontains=params.search).values_list("typeID", flat=True)[:100] ) search_args |= Q(stationID__in=station_ids) search_args |= Q(typeID__in=item_ids) if is_number(params.search): search_args |= Q(amount__gte=params.search) if params.walletID: search_args &= Q(wallet=params.walletID) if params.entryTypeID != -1: search_args &= Q(transactionType=params.entryTypeID) if params.entryForID != -1: search_args &= Q(transactionFor=params.entryForID) # Add query amount if params.amount: comparator_map = { "gt": Q(price__gt=params.amount), "lt": Q(price__lt=params.amount), "gte": Q(price__gte=params.amount), "lte": Q(price__lte=params.amount), "eq": Q(price=params.amount), "neq": Q(price__lt=params.amount, price__gt=params.amount), } search_args &= comparator_map[params.comparator] # Add query dates if params.from_date and params.to_date: # + 24 hours on the to date search_args &= Q(date__range=(params.from_date, params.to_date + timedelta(days=1))) query = query.filter(search_args) filtered_entries = query.count() else: total_entries = filtered_entries = query.count() query = query[params.first_id : params.last_id] entries = [] my_corp = Corporation.objects.mine() for entry in query: try: amount = print_float(entry.journal.amount, force_sign=True) except AttributeError: amount = "0.0" try: balance = print_float(entry.journal.balance) except AttributeError: balance = "" entries.append( [ print_time_min(entry.date), entry.typeName, entry.price, entry.quantity, amount, balance, truncate_words(entry.stationName, 6), entry.wallet.corp_wallets.get(corp=my_corp).name, ] ) json_data = { "sEcho": params.sEcho, "iTotalRecords": total_entries, "iTotalDisplayRecords": filtered_entries, "aaData": entries, } return HttpResponse(json.dumps(json_data))
def journal_data(request): try: params = extract_datatable_params(request) REQ = request.GET if request.method == 'GET' else request.POST params.walletID = int(REQ.get('walletID', 0)) params.entryTypeID = int(REQ.get('entryTypeID', 0)) params.amount = request.GET.get('amount', None) params.comparator = request.GET.get('comparator', 'gt') params.from_date = timezone.make_aware( datetime.strptime(REQ.get('from_date', None), DATE_PATTERN), timezone.get_current_timezone()) params.to_date = timezone.make_aware( datetime.strptime(REQ.get('to_date', None), DATE_PATTERN), timezone.get_current_timezone()) except: return HttpResponseBadRequest() orderBy = journal_cols[params.column] if not params.asc: orderBy = "-" + orderBy query = JournalEntry.objects.select_related( depth=1).all().order_by(orderBy) if params.search or params.walletID or params.entryTypeID or params.amount or ( params.from_date and params.to_date): total_entries = query.count() search_args = Q() if params.search: search_args |= Q(ownerName1__icontains=params.search) search_args |= Q(ownerName2__icontains=params.search) search_args |= Q(argName1__icontains=params.search) search_args |= Q(reason__icontains=params.search) if is_number(params.search): search_args |= Q(amount__gte=params.search) if params.walletID: search_args &= Q(wallet=params.walletID) if params.entryTypeID: search_args &= Q(type=params.entryTypeID) # Add query amount if params.amount: comparator_map = { 'gt': Q(amount__gt=params.amount), 'lt': Q(amount__lt=params.amount), 'gte': Q(amount__gte=params.amount), 'lte': Q(amount__lte=params.amount), 'eq': Q(amount=params.amount), 'neq': Q(amount__lt=params.amount, amount__gt=params.amount), } search_args &= comparator_map[params.comparator] # Add query dates if params.from_date and params.to_date: # + 24 hours on the to date search_args &= Q(date__range=(params.from_date, params.to_date + timedelta(days=1))) query = query.filter(search_args) filtered_entries = query.count() else: total_entries = filtered_entries = query.count() query = query[params.first_id:params.last_id] entries = [] # to improve performance try: corp = Corporation.objects.mine() except Corporation.DoesNotExist: corp = Corporation(corporationID=0) members = Member.objects.all() other_entries = JournalEntry.objects.select_related().all() for entry in query: try: owner1 = members.get(characterID=entry.ownerID1).permalink except Member.DoesNotExist: owner1 = entry.ownerName1 try: owner2 = members.get(characterID=entry.ownerID2).permalink except Member.DoesNotExist: owner2 = entry.ownerName2 if entry.type_id == EntryType.BOUNTY_PRIZES: rats = [s.split(':') for s in entry.reason.split(',') if ':' in s] rat_list = [] for rat_id, rat_count in rats: rat_list.append( '%s x%s' % (Type.objects.get(typeID=rat_id).typeName, rat_count)) reason = '|'.join(rat_list) if reason: reason = (u'Killed Rats in %s|' % entry.argName1) + reason elif entry.type_id == EntryType.PLAYER_DONATION: reason = entry.reason[len('DESC: '):] if reason: reason = u'Description|' + reason elif entry.type_id == EntryType.CORP_WITHDRAWAL: reason = entry.reason[len('DESC: '):].strip('\n\t\'" ') reason = (u'Cash transfer by %s|' % entry.argName1) + reason try: if int(entry.ownerID1) == corp.corporationID and int( entry.ownerID2) == corp.corporationID: related_entry = other_entries.filter( refID=entry.refID).exclude(id=entry.id)[0] owner2 = related_entry.wallet.corp_wallets.get( corp=corp).name except: pass else: reason = entry.reason wallet_name = entry.wallet.corp_wallets.get(corp=corp).name entries.append([ print_time_min(entry.date), wallet_name, entry.type.refTypeName, owner1, owner2, print_float(entry.amount, force_sign=True), print_float(entry.balance), reason, ]) return datatable_ajax_data(entries, params.sEcho, total_entries, filtered_entries)