forked from madcowfred/evething
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorders.py
More file actions
135 lines (117 loc) · 5.16 KB
/
Copy pathorders.py
File metadata and controls
135 lines (117 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# ------------------------------------------------------------------------------
# Copyright (c) 2010-2013, EVEthing team
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
# OF SUCH DAMAGE.
# ------------------------------------------------------------------------------
try:
from collections import OrderedDict
except ImportError:
from ordereddict import OrderedDict
from django.contrib.auth.decorators import login_required
from django.db import connection
from thing import queries
from thing.models import *
from thing.stuff import *
# ---------------------------------------------------------------------------
ORDER_SLOT_SKILLS = {
3443: 4, # Trade
3444: 8, # Retail
16596: 16,# Wholesale
18580: 32,# Tycoon
}
# ---------------------------------------------------------------------------
# Market orders
@login_required
def orders(request):
# Retrieve order aggregate data
cursor = connection.cursor()
cursor.execute(queries.order_aggregation, (request.user.id,))
char_orders = OrderedDict()
for row in dictfetchall(cursor):
row['slots'] = 5
char_orders[row['creator_character_id']] = row
# Retrieve trade skills that we're interested in
order_cs = CharacterSkill.objects.filter(
character__apikeys__user=request.user,
skill__in=ORDER_SLOT_SKILLS,
).exclude(
character__apikeys__key_type=APIKey.CORPORATION_TYPE
)
for cs in order_cs:
char_id = cs.character_id
if char_id not in char_orders:
continue
char_orders[char_id]['slots'] += (cs.level * ORDER_SLOT_SKILLS.get(cs.skill_id, 0))
# Calculate free slots
for row in char_orders.values():
row['free_slots'] = row['slots'] - row['corp_orders'] - row['personal_orders']
total_row = {
'free_slots': sum(row['free_slots'] for row in char_orders.values()),
'slots': sum(row['slots'] for row in char_orders.values()),
'personal_orders': sum(row['personal_orders'] for row in char_orders.values()),
'corp_orders': sum(row['corp_orders'] for row in char_orders.values()),
'sell_orders': sum(row['sell_orders'] for row in char_orders.values()),
'total_sells': sum(row['total_sells'] for row in char_orders.values()),
'buy_orders': sum(row['buy_orders'] for row in char_orders.values()),
'total_buys': sum(row['total_buys'] for row in char_orders.values()),
'total_escrow': sum(row['total_escrow'] for row in char_orders.values()),
}
# Retrieve all orders
character_ids = list(Character.objects.filter(
apikeys__user=request.user.id,
apikeys__valid=True,
).exclude(
apikeys__key_type=APIKey.CORPORATION_TYPE,
).distinct().values_list(
'id',
flat=True,
))
corporation_ids = Corporation.get_ids_with_access(request.user, APIKey.CORP_MARKET_ORDERS_MASK)
orders = MarketOrder.objects.filter(
Q(character__in=character_ids, corp_wallet__isnull=True)
|
Q(corp_wallet__corporation__in=corporation_ids)
)
orders = orders.prefetch_related('item', 'station', 'character', 'corp_wallet__corporation')
orders = orders.order_by('station__name', '-buy_order', 'item__name')
# Fetch creator characters as they're not a FK relation
creator_ids = set()
utcnow = datetime.datetime.utcnow()
for order in orders:
creator_ids.add(order.creator_character_id)
order.z_remaining = total_seconds(order.expires - utcnow)
# Bulk query
char_map = Character.objects.in_bulk(creator_ids)
# Sort out possible chars
for order in orders:
order.z_creator_character = char_map.get(order.creator_character_id)
# Render template
return render_page(
'thing/orders.html',
{
'char_orders': char_orders,
'orders': orders,
'total_row': total_row,
},
request,
)
# ---------------------------------------------------------------------------