Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.80 KB | None | 0 0
  1. from app import appbuilder, db
  2. from app.models.core import Announcement, Cryptocurrency, Day, Wallet, WalletCryptocurrency, Trade, Ticket, Following
  3. from app.models.security import GraniteUser
  4. from flask_appbuilder import ModelView, action, BaseView, has_access, expose
  5. from flask_appbuilder.charts.views import DirectByChartView
  6. from flask_appbuilder.models.sqla.filters import FilterEqualFunction
  7. from flask_appbuilder.models.sqla.interface import SQLAInterface
  8. from flask_login import current_user
  9. from werkzeug.utils import redirect
  10.  
  11.  
  12. def get_user_id():
  13. return current_user.id
  14.  
  15.  
  16. class AnnouncementModelView(ModelView):
  17. datamodel = SQLAInterface(Announcement)
  18.  
  19. # list_widget = ListThumbnail
  20. label_columns = {'text': 'Announcement', 'user_created': 'author'}
  21. list_columns = ['text', 'user_created', 'created_on', 'last_edited']
  22. add_columns = ['text']
  23. edit_columns = ['text']
  24.  
  25. show_fieldsets = [
  26. (
  27. 'Announcement',
  28. {'fields': ['text']}
  29. ),
  30. (
  31. 'Info',
  32. {'fields': ['user_created', 'created_on', 'last_edited']}
  33. )
  34. ]
  35.  
  36.  
  37. class DayModelView(ModelView):
  38. datamodel = SQLAInterface(Day)
  39.  
  40. list_title = 'Historical Price Data'
  41.  
  42. list_columns = ['cryptocurrency', 'date', 'open', 'close', 'low', 'high',
  43. 'volume_btc', 'volume_currency']
  44.  
  45.  
  46. class DayLineChartView(DirectByChartView):
  47. datamodel = SQLAInterface(Day)
  48.  
  49. chart_title = 'Closing price chart'
  50.  
  51. chart_type = 'LineChart'
  52.  
  53. definitions = [
  54. {
  55. 'label': 'Close',
  56. 'group': 'date',
  57. 'series': ['close']
  58. },
  59. ]
  60.  
  61.  
  62. class DayCandlestickChartView(DirectByChartView):
  63. datamodel = SQLAInterface(Day)
  64. chart_title = 'Candlestick chart'
  65.  
  66. chart_type = 'CandlestickChart'
  67.  
  68. definitions = [
  69. {
  70. 'label': 'Candlestick',
  71. 'group': 'date',
  72. 'series': ['low', 'open', 'close', 'high']
  73. }
  74. ]
  75.  
  76.  
  77. class CryptocurrencyModelView(ModelView):
  78. datamodel = SQLAInterface(Cryptocurrency)
  79.  
  80. related_views = [DayModelView, DayLineChartView, DayCandlestickChartView]
  81.  
  82. @action('follow', 'Follow this currency', 'Are you sure?', 'fa-binoculars')
  83. def follow(self, item):
  84. following = Following()
  85. following.cryptocurrency_id = item.id
  86. following.user_id = current_user.id
  87. try:
  88. db.session.add(following)
  89. db.session.commit()
  90. except Exception:
  91. db.session.rollback()
  92.  
  93. return redirect(self.get_redirect())
  94.  
  95.  
  96. class WalletCryptocurrencyModelView(ModelView):
  97. datamodel = SQLAInterface(WalletCryptocurrency)
  98.  
  99. list_columns = ['wallet_id', 'cryptocurrency', 'amount']
  100.  
  101.  
  102. class WalletModelView(ModelView):
  103. datamodel = SQLAInterface(Wallet)
  104. base_filters = [['user_id', FilterEqualFunction, get_user_id], ]
  105.  
  106. related_views = [WalletCryptocurrencyModelView]
  107. list_columns = ['id', 'type']
  108.  
  109.  
  110. class TradeModelView(ModelView):
  111. datamodel = SQLAInterface(Trade)
  112. base_filters = [['user_created_id', FilterEqualFunction, get_user_id], ]
  113.  
  114. list_columns = ['created_cryptocurrency', 'created_cryptocurrency_amount',
  115. 'wanted_cryptocurrency', 'wanted_cryptocurrency_amount']
  116.  
  117. add_columns = ['created_cryptocurrency', 'created_cryptocurrency_amount',
  118. 'wanted_cryptocurrency', 'wanted_cryptocurrency_amount']
  119.  
  120. def post_add(self, item):
  121. try:
  122. trades = db.session.query(Trade). \
  123. filter(Trade.wanted_cryptocurrency_id == item.created_cryptocurrency_id). \
  124. filter(Trade.created_cryptocurrency_id == item.wanted_cryptocurrency_id). \
  125. filter((Trade.created_cryptocurrency_amount / Trade.wanted_cryptocurrency_amount)
  126. == (item.wanted_cryptocurrency_amount / item.created_cryptocurrency_amount)). \
  127. filter(Trade.id != item.id).filter(Trade.user_created_id != item.user_created_id).all()
  128.  
  129. if trades:
  130. ideal = item.wanted_cryptocurrency_amount
  131.  
  132. hit_index = -1
  133. diff = None
  134. for trade, index in zip(trades, range(len(trades))):
  135. temp_diff = abs(ideal - trade.created_cryptocurrency_amount)
  136. diff, hit_index = (temp_diff, index) if diff is None else (
  137. temp_diff, index) if temp_diff < diff else \
  138. (diff, hit_index)
  139.  
  140. if diff == 0:
  141. # db.session.query(Trade).filter(Trade.id == item.id).delete()
  142. # db.session.query(Trade).filter(Trade.id == trades[hit_index].id).delete()
  143.  
  144. db.session.add(item)
  145. db.session.add(trades[hit_index])
  146.  
  147. items_wallet = db.session.query(Wallet).filter(Wallet.user_id == item.user_created_id).filter(
  148. Wallet.type == 'Crypto').first()
  149. hit_wallet = db.session.query(Wallet).filter(
  150. Wallet.user_id == trades[hit_index].user_created_id).filter(Wallet.type == 'Crypto').first()
  151.  
  152. items_query = db.session.query(WalletCryptocurrency).filter(
  153. WalletCryptocurrency.wallet_id == items_wallet.id).filter(
  154. WalletCryptocurrency.cryptocurrency_id == item.wanted_cryptocurrency_id)
  155. hit_query = db.session.query(WalletCryptocurrency).filter(
  156. WalletCryptocurrency.wallet_id == hit_wallet.id).filter(
  157. WalletCryptocurrency.cryptocurrency_id == item.created_cryptocurrency_id)
  158.  
  159. items_wc = items_query.first()
  160. hit_wc = hit_query.first()
  161.  
  162. # items_query.delete()
  163. # hit_query.delete()
  164.  
  165. items_wc.amount += item.wanted_cryptocurrency_amount
  166. hit_wc.amount += item.created_cryptocurrency_amount
  167.  
  168. db.session.add(items_wc)
  169. db.session.add(hit_wc)
  170. db.session.commit()
  171.  
  172. except Exception as e:
  173. print e
  174. db.rollback()
  175.  
  176.  
  177. class TicketModelView(ModelView):
  178. datamodel = SQLAInterface(Ticket)
  179.  
  180. base_permissions = ['can_list', 'can_show', 'can_add']
  181. # list_widget = ListThumbnail
  182. label_columns = {'user_created': 'author'}
  183. list_columns = ['id', 'title', 'user_created', 'added']
  184.  
  185. add_columns = ['title', 'text']
  186. edit_columns = ['title', 'text']
  187.  
  188. @action('accept_ticket', 'Accept this ticket', 'Are you sure about accepting this ticket?', icon='fa-eye')
  189. def accept_ticket(self, item):
  190. try:
  191. from flask_mail import Mail, Message
  192. except Exception as e:
  193. print e
  194.  
  195. for i in item:
  196. mail = Mail(appbuilder.get_app)
  197. msg = Message()
  198. msg.subject = '#{ticket_id} - Accepted'.format(ticket_id=i.id)
  199. msg.body = \
  200. '<h>#{ticket_id} - {ticket_title} - Accepted</h>\n<span>Ticket accepted, admin will contact shortly</span>'. \
  201. format(ticket_id=i.id, ticket_title=i.title)
  202. user = db.session.query(GraniteUser).filter(GraniteUser.id == i.user_created_id).first()
  203. msg.recipients = [user.email]
  204.  
  205. mail_admin = Mail(appbuilder.get_app)
  206. msg_admin = Message()
  207. msg_admin.subject = 'Accepted ticket #{0}'.format(i.id)
  208. msg_admin.body = 'Contact user {name} at {email}'.format(name=user.username, email=user.email)
  209. msg_admin.recipients = [current_user.email]
  210.  
  211. try:
  212. mail_admin.send(msg_admin)
  213. mail.send(msg)
  214. except Exception as e:
  215. print e
  216.  
  217. return redirect(self.get_redirect())
  218.  
  219.  
  220. class DashboardItem(object):
  221. def __init__(self, name, tag, price, volume):
  222. self.name = name
  223. self.tag = tag
  224. self.price = price
  225. self.volume = volume
  226.  
  227.  
  228. class DashboardView(BaseView):
  229. route_base = '/dashboard'
  230. # template_folder = '../templates'
  231. template = 'dashboard.html'
  232. default_view = 'dashboard'
  233.  
  234. @has_access
  235. @expose('/')
  236. def dashboard(self):
  237. following = db.session.query(Following) \
  238. .filter(Following.user_id == current_user.id) \
  239. .all()
  240. stats = [db.session.query(Day).filter(Day.cryptocurrency_id == item.cryptocurrency_id).first() for item in
  241. following]
  242. details = [db.session.query(Cryptocurrency).filter(Cryptocurrency.id == item.cryptocurrency_id).first() for item
  243. in following]
  244.  
  245. dashboard_items = [DashboardItem(detail.name, detail.tag, stat.close, stat.volume) for detail, stat in
  246. zip(details, stats)]
  247.  
  248. self.extra_args = {'stats': dict(zip([item.tag for item in dashboard_items], dashboard_items))}
  249. return self.render_template(self.template)
  250.  
  251.  
  252. db.create_all()
  253.  
  254. appbuilder.add_view(AnnouncementModelView, 'Announcements', icon='fa-newspaper-o')
  255. appbuilder.add_view_no_menu(DayModelView, 'DayModelView')
  256. appbuilder.add_view_no_menu(DayLineChartView, 'DayLineChartView')
  257. appbuilder.add_view_no_menu(DayCandlestickChartView, 'DayCandlestickChartView')
  258. appbuilder.add_view(CryptocurrencyModelView, 'Cryptocurrencies', icon='fa-btc')
  259. appbuilder.add_view_no_menu(WalletCryptocurrencyModelView, 'WalletCryptocurrencyModelView')
  260. appbuilder.add_view(WalletModelView, 'Wallets', icon='fa-bank')
  261. appbuilder.add_view(TradeModelView, 'Trades', icon='fa-exchange')
  262. appbuilder.add_view(TicketModelView, 'Tickets', icon='fa-envelope-square')
  263. appbuilder.add_view(DashboardView(), 'Dashboard', icon='fa-dashboard')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement