Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from app import appbuilder, db
- from app.models.core import Announcement, Cryptocurrency, Day, Wallet, WalletCryptocurrency, Trade, Ticket, Following
- from app.models.security import GraniteUser
- from flask_appbuilder import ModelView, action, BaseView, has_access, expose
- from flask_appbuilder.charts.views import DirectByChartView
- from flask_appbuilder.models.sqla.filters import FilterEqualFunction
- from flask_appbuilder.models.sqla.interface import SQLAInterface
- from flask_login import current_user
- from werkzeug.utils import redirect
- def get_user_id():
- return current_user.id
- class AnnouncementModelView(ModelView):
- datamodel = SQLAInterface(Announcement)
- # list_widget = ListThumbnail
- label_columns = {'text': 'Announcement', 'user_created': 'author'}
- list_columns = ['text', 'user_created', 'created_on', 'last_edited']
- add_columns = ['text']
- edit_columns = ['text']
- show_fieldsets = [
- (
- 'Announcement',
- {'fields': ['text']}
- ),
- (
- 'Info',
- {'fields': ['user_created', 'created_on', 'last_edited']}
- )
- ]
- class DayModelView(ModelView):
- datamodel = SQLAInterface(Day)
- list_title = 'Historical Price Data'
- list_columns = ['cryptocurrency', 'date', 'open', 'close', 'low', 'high',
- 'volume_btc', 'volume_currency']
- class DayLineChartView(DirectByChartView):
- datamodel = SQLAInterface(Day)
- chart_title = 'Closing price chart'
- chart_type = 'LineChart'
- definitions = [
- {
- 'label': 'Close',
- 'group': 'date',
- 'series': ['close']
- },
- ]
- class DayCandlestickChartView(DirectByChartView):
- datamodel = SQLAInterface(Day)
- chart_title = 'Candlestick chart'
- chart_type = 'CandlestickChart'
- definitions = [
- {
- 'label': 'Candlestick',
- 'group': 'date',
- 'series': ['low', 'open', 'close', 'high']
- }
- ]
- class CryptocurrencyModelView(ModelView):
- datamodel = SQLAInterface(Cryptocurrency)
- related_views = [DayModelView, DayLineChartView, DayCandlestickChartView]
- @action('follow', 'Follow this currency', 'Are you sure?', 'fa-binoculars')
- def follow(self, item):
- following = Following()
- following.cryptocurrency_id = item.id
- following.user_id = current_user.id
- try:
- db.session.add(following)
- db.session.commit()
- except Exception:
- db.session.rollback()
- return redirect(self.get_redirect())
- class WalletCryptocurrencyModelView(ModelView):
- datamodel = SQLAInterface(WalletCryptocurrency)
- list_columns = ['wallet_id', 'cryptocurrency', 'amount']
- class WalletModelView(ModelView):
- datamodel = SQLAInterface(Wallet)
- base_filters = [['user_id', FilterEqualFunction, get_user_id], ]
- related_views = [WalletCryptocurrencyModelView]
- list_columns = ['id', 'type']
- class TradeModelView(ModelView):
- datamodel = SQLAInterface(Trade)
- base_filters = [['user_created_id', FilterEqualFunction, get_user_id], ]
- list_columns = ['created_cryptocurrency', 'created_cryptocurrency_amount',
- 'wanted_cryptocurrency', 'wanted_cryptocurrency_amount']
- add_columns = ['created_cryptocurrency', 'created_cryptocurrency_amount',
- 'wanted_cryptocurrency', 'wanted_cryptocurrency_amount']
- def post_add(self, item):
- try:
- trades = db.session.query(Trade). \
- filter(Trade.wanted_cryptocurrency_id == item.created_cryptocurrency_id). \
- filter(Trade.created_cryptocurrency_id == item.wanted_cryptocurrency_id). \
- filter((Trade.created_cryptocurrency_amount / Trade.wanted_cryptocurrency_amount)
- == (item.wanted_cryptocurrency_amount / item.created_cryptocurrency_amount)). \
- filter(Trade.id != item.id).filter(Trade.user_created_id != item.user_created_id).all()
- if trades:
- ideal = item.wanted_cryptocurrency_amount
- hit_index = -1
- diff = None
- for trade, index in zip(trades, range(len(trades))):
- temp_diff = abs(ideal - trade.created_cryptocurrency_amount)
- diff, hit_index = (temp_diff, index) if diff is None else (
- temp_diff, index) if temp_diff < diff else \
- (diff, hit_index)
- if diff == 0:
- # db.session.query(Trade).filter(Trade.id == item.id).delete()
- # db.session.query(Trade).filter(Trade.id == trades[hit_index].id).delete()
- db.session.add(item)
- db.session.add(trades[hit_index])
- items_wallet = db.session.query(Wallet).filter(Wallet.user_id == item.user_created_id).filter(
- Wallet.type == 'Crypto').first()
- hit_wallet = db.session.query(Wallet).filter(
- Wallet.user_id == trades[hit_index].user_created_id).filter(Wallet.type == 'Crypto').first()
- items_query = db.session.query(WalletCryptocurrency).filter(
- WalletCryptocurrency.wallet_id == items_wallet.id).filter(
- WalletCryptocurrency.cryptocurrency_id == item.wanted_cryptocurrency_id)
- hit_query = db.session.query(WalletCryptocurrency).filter(
- WalletCryptocurrency.wallet_id == hit_wallet.id).filter(
- WalletCryptocurrency.cryptocurrency_id == item.created_cryptocurrency_id)
- items_wc = items_query.first()
- hit_wc = hit_query.first()
- # items_query.delete()
- # hit_query.delete()
- items_wc.amount += item.wanted_cryptocurrency_amount
- hit_wc.amount += item.created_cryptocurrency_amount
- db.session.add(items_wc)
- db.session.add(hit_wc)
- db.session.commit()
- except Exception as e:
- print e
- db.rollback()
- class TicketModelView(ModelView):
- datamodel = SQLAInterface(Ticket)
- base_permissions = ['can_list', 'can_show', 'can_add']
- # list_widget = ListThumbnail
- label_columns = {'user_created': 'author'}
- list_columns = ['id', 'title', 'user_created', 'added']
- add_columns = ['title', 'text']
- edit_columns = ['title', 'text']
- @action('accept_ticket', 'Accept this ticket', 'Are you sure about accepting this ticket?', icon='fa-eye')
- def accept_ticket(self, item):
- try:
- from flask_mail import Mail, Message
- except Exception as e:
- print e
- for i in item:
- mail = Mail(appbuilder.get_app)
- msg = Message()
- msg.subject = '#{ticket_id} - Accepted'.format(ticket_id=i.id)
- msg.body = \
- '<h>#{ticket_id} - {ticket_title} - Accepted</h>\n<span>Ticket accepted, admin will contact shortly</span>'. \
- format(ticket_id=i.id, ticket_title=i.title)
- user = db.session.query(GraniteUser).filter(GraniteUser.id == i.user_created_id).first()
- msg.recipients = [user.email]
- mail_admin = Mail(appbuilder.get_app)
- msg_admin = Message()
- msg_admin.subject = 'Accepted ticket #{0}'.format(i.id)
- msg_admin.body = 'Contact user {name} at {email}'.format(name=user.username, email=user.email)
- msg_admin.recipients = [current_user.email]
- try:
- mail_admin.send(msg_admin)
- mail.send(msg)
- except Exception as e:
- print e
- return redirect(self.get_redirect())
- class DashboardItem(object):
- def __init__(self, name, tag, price, volume):
- self.name = name
- self.tag = tag
- self.price = price
- self.volume = volume
- class DashboardView(BaseView):
- route_base = '/dashboard'
- # template_folder = '../templates'
- template = 'dashboard.html'
- default_view = 'dashboard'
- @has_access
- @expose('/')
- def dashboard(self):
- following = db.session.query(Following) \
- .filter(Following.user_id == current_user.id) \
- .all()
- stats = [db.session.query(Day).filter(Day.cryptocurrency_id == item.cryptocurrency_id).first() for item in
- following]
- details = [db.session.query(Cryptocurrency).filter(Cryptocurrency.id == item.cryptocurrency_id).first() for item
- in following]
- dashboard_items = [DashboardItem(detail.name, detail.tag, stat.close, stat.volume) for detail, stat in
- zip(details, stats)]
- self.extra_args = {'stats': dict(zip([item.tag for item in dashboard_items], dashboard_items))}
- return self.render_template(self.template)
- db.create_all()
- appbuilder.add_view(AnnouncementModelView, 'Announcements', icon='fa-newspaper-o')
- appbuilder.add_view_no_menu(DayModelView, 'DayModelView')
- appbuilder.add_view_no_menu(DayLineChartView, 'DayLineChartView')
- appbuilder.add_view_no_menu(DayCandlestickChartView, 'DayCandlestickChartView')
- appbuilder.add_view(CryptocurrencyModelView, 'Cryptocurrencies', icon='fa-btc')
- appbuilder.add_view_no_menu(WalletCryptocurrencyModelView, 'WalletCryptocurrencyModelView')
- appbuilder.add_view(WalletModelView, 'Wallets', icon='fa-bank')
- appbuilder.add_view(TradeModelView, 'Trades', icon='fa-exchange')
- appbuilder.add_view(TicketModelView, 'Tickets', icon='fa-envelope-square')
- appbuilder.add_view(DashboardView(), 'Dashboard', icon='fa-dashboard')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement