-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodels.py
137 lines (107 loc) · 4.41 KB
/
models.py
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
136
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2014 george
#
# Distributed under terms of the MIT license.
from google.appengine.ext import ndb, db
from datetime import datetime, timedelta
import random
class FoodDailyInfo(ndb.Model):
date = ndb.DateProperty()
wholesale_price = ndb.FloatProperty(indexed=False)
price = ndb.FloatProperty(indexed=False)
amount = ndb.FloatProperty(indexed=False)
class Food(ndb.Model):
next_one = ndb.StringProperty(indexed=False)
prev = ndb.StringProperty(indexed=False)
point = ndb.FloatProperty()
infos = ndb.LocalStructuredProperty(FoodDailyInfo, repeated=True)
image = ndb.StringProperty(indexed=False)
name = ndb.StringProperty()
type = ndb.StringProperty(choices=['meat', 'fish', 'vegetable'])
lowset_price = ndb.FloatProperty()
lowset_wholesale_price = ndb.FloatProperty()
avg_price = ndb.FloatProperty()
avg_wholesale_price = ndb.FloatProperty()
avg_amount = ndb.FloatProperty()
price = ndb.FloatProperty() ## today price
wholesale_price = ndb.FloatProperty() ## today wholesale_price
amount = ndb.FloatProperty() ## today amount
rank = ndb.IntegerProperty() ## today rank
content = ndb.StringProperty()
rich = ndb.BooleanProperty(default=False)
updated = ndb.DateProperty(auto_now=True)
def get_point(self):
point = 0
if self.updated < datetime.now().date():
point = -100
else:
## 市價記分
if self.avg_price:
point += round(1-self.price / self.avg_price, 1) * 10
## 批發價記分
if self.avg_wholesale_price:
point += round(1-self.wholesale_price / self.avg_wholesale_price, 1) * 10
## 交易量記分
if self.avg_amount:
point += round(self.amount / self.avg_amount -1 , 1) * 10
## 產季記分
point += self.rich * 10
if u"進口" in self.name:
point -= 30
today = datetime.today().date()
last_day = max([info.date for info in self.infos])
delta_days = (today - last_day).days
print delta_days
if delta_days < 2:
point += 10
elif delta_days > 10:
point -= 1000
self.point = point + random.random()
return point
def get_recommand(self):
result = []
## 市價記分
if self.avg_price:
point = round(1-self.price / self.avg_price, 1) * 10
if point > 0:
result.append("市價低於過往平均 {}% !!".format(point*10))
## 批發價記分
if self.avg_wholesale_price:
point = round(1-self.wholesale_price / self.avg_wholesale_price, 1) * 10
if point > 0:
result.append("批發價低於過往平均 {}% !!".format(point*10))
## 交易量記分
if self.avg_amount:
point = round(self.amount / self.avg_amount - 1, 1) * 10
if point > 0:
result.append("交易量大 盛產 !!".format(point))
## 產季記分
point = self.rich * 10
if point > 0:
result.append("產季!!")
return "\n".join(result)
def aggregate(self):
count = 0
total_wholesale_price = []
total_amount = []
total_price = []
for info in self.infos:
count+=1
if info.wholesale_price:
total_wholesale_price.append(info.wholesale_price)
if info.price:
total_price.append(info.price)
if info.amount:
total_amount.append(info.amount)
avg_wholesale_price = total_wholesale_price and round(sum(total_wholesale_price) / float(len(total_wholesale_price)), 1) or None
avg_price = total_price and round(sum(total_price) / float(len(total_price)), 1) or None
avg_amount = total_amount and round(sum(total_amount) / float(len(total_amount)), 1) or None
self.avg_wholesale_price = avg_wholesale_price
self.avg_price = avg_price
self.avg_amount = avg_amount
return avg_wholesale_price, avg_price, avg_amount
def push_info(self, date):
info = FoodDailyInfo(date=date, wholesale_price=self.wholesale_price, price=self.price, amount=self.amount)
self.infos.append(info)