def orders():
	page = convert.to_int(request.args.get('page'), 1)
	count = actions.orders.get_orders_count(user_id=g.user.id)
	per_page = app.config.get('ADMIN_ORDERS_PER_PAGE', 20)
	offset, limit, pages = paginate(page, count, per_page)
	ods = actions.orders.get_orders(user_id=g.user.id, offset=offset, limit=limit, full=True)
	return render_template('cabinet/orders.html', orders=ods, pages=pages)
def performers():
	page = convert.to_int(request.args.get('page'), 1)
	count = a.performers.get_performers_count()
	per_page = app.config.get('ADMIN_PERFORMERS_PER_PAGE', 20)
	offset, limit, pages = paginate(page, count, per_page)
	perfs = do_or_abort(a.performers.get_performers, offset=offset, limit=limit, full=True)
	return render_template('admin/performers.html', performers=perfs, pages=pages)
def actions():
	page = convert.to_int(request.args.get('page'), 1)
	count = a.actions.get_actions_count()
	per_page = app.config.get('ADMIN_ACTIONS_PER_PAGE', 20)
	offset, limit, pages = paginate(page, count, per_page)
	acts = do_or_abort(a.actions.get_actions, offset=offset, limit=limit, full=True)
	return render_template('admin/actions.html', actions=acts, pages=pages)
def users_info_balance(id):
	user = do_or_abort(a.users.get_user_by_id, id)
	delete_form = forms.WithdrawalDeleteForm()
	withdrawals = None
	if user.is_customer():
		account = user.customer_account
		form = forms.BalanceForm(request.form)
		if request.method == 'POST' and form.validate():
			a.users.increase_customer_balance(user.id, round(form.amount.data, 2))
			flash(u'Баланс успешно пополнен', 'success')
			return redirect(url_for('.users_info_balance', id=user.id))
	elif user.is_developer():
		account = user.developer_account
		form = forms.BalanceForm(request.form, amount=account.balance)
		if request.method == 'POST' and form.validate():
			a.users.make_user_withdrawal(user.id, round(form.amount.data, 2))
			flash(u'Выплата разработчику успешно создана', 'success')
			return redirect(url_for('.users_info_balance', id=user.id))
		
		withdrawals = a.users.get_user_withdrawals(user.id)
	else:
		flash(u'Пользователь не имеет счета', 'error')
		return redirect(url_for('.users_info', id=user.id))
	
	page = convert.to_int(request.args.get('page'), 1)
	count = a.accounts.get_account_transactions_count(account.id)
	per_page = app.config.get('ADMIN_TRANSACTIONS_PER_PAGE', 20)
	offset, limit, pages = paginate(page, count, per_page)
	transactions = a.accounts.get_account_transactions(account_id=account.id, offset=offset, limit=limit)
	return render_template('admin/users-info-balance.html', transactions=transactions,
		withdrawals=withdrawals, pages=pages, user=user, form=form, delete_form=delete_form)
def apps():
	page = convert.to_int(request.args.get('page'), 1)
	count = actions.apps.get_apps_count(user_id=g.user.id)
	per_page = app.config.get('ADMIN_APPS_PER_PAGE', 20)
	offset, limit, pages = paginate(page, count, per_page)
	aps = do_or_abort(actions.apps.get_apps, user_id=g.user.id,
					offset=offset, limit=limit, full=True)
	return render_template('cabinet/apps.html', apps=aps, pages=pages)
def users_developers():
	role = 'DEVELOPER'
	page = convert.to_int(request.args.get('page'), 1)
	count = a.users.get_users_count(role=role)
	per_page = app.config.get('ADMIN_USERS_PER_PAGE', 20)
	offset, limit, pages = paginate(page, count, per_page)
	usrs = do_or_abort(a.users.get_users, role=role, offset=offset, limit=limit, full=True)
	return render_template('admin/users-developers.html', developers=usrs, pages=pages)
def apps_info_actions(id):
	ap = do_or_abort(a.apps.get_app, id, full=True)
	page = convert.to_int(request.args.get('page'), 1)
	count = a.actions.get_actions_count(appId=ap.id)
	per_page = app.config.get('ADMIN_ACTIONS_PER_PAGE', 20)
	offset, limit, pages = paginate(page, count, per_page)
	acts = do_or_abort(a.actions.get_actions, offset=offset, limit=limit, full=True, appId=ap.id)
	return render_template('admin/apps-info-actions.html', app=ap, actions=acts, pages=pages)
def performers_info_actions(id):
	performer = do_or_abort(a.performers.get_performer, id, full=True)
	page = convert.to_int(request.args.get('page'), 1)
	count = a.actions.get_actions_count(performerId=performer.id)
	per_page = app.config.get('ADMIN_ACTIONS_PER_PAGE', 20)
	offset, limit, pages = paginate(page, count, per_page)
	acts = do_or_abort(a.actions.get_actions, 
		offset=offset, limit=limit, full=True, performerId=performer.id)
	return render_template('admin/performers-info-actions.html', performer=performer, actions=acts, pages=pages)
def users_info_orders(id):
	user = do_or_abort(a.users.get_user_by_id, id)
	if not user.is_customer(): abort(404)
	
	page = convert.to_int(request.args.get('page'), 1)
	count = a.orders.get_orders_count(user_id=user.id)
	per_page = app.config.get('ADMIN_ORDERS_PER_PAGE', 20)
	offset, limit, pages = paginate(page, count, per_page)
	ods = a.orders.get_orders(user_id=user.id, offset=offset, limit=limit, full=True)
	return render_template('admin/users-info-orders.html', user=user, orders=ods, pages=pages)
def users_info_apps(id):
	user = do_or_abort(a.users.get_user_by_id, id)
	if not user.is_developer(): abort(404)
	
	page = convert.to_int(request.args.get('page'), 1)
	count = a.apps.get_apps_count(user_id=user.id)
	per_page = app.config.get('ADMIN_APPS_PER_PAGE', 20)
	offset, limit, pages = paginate(page, count, per_page)
	with_deleted = session.get(SESSION_APPS_SHOW_DELETED, False)
	aps = do_or_abort(a.apps.get_apps, with_deleted=with_deleted,
					user_id=user.id, offset=offset, limit=limit, full=True)
	return render_template('admin/users-info-apps.html', user=user, apps=aps, pages=pages)
def apps():
	page = convert.to_int(request.args.get('page'), 1)
	form = forms.AppsShowDeletedForm(request.form, show=session.get(SESSION_APPS_SHOW_DELETED, False))
	if request.method == 'POST' and form.validate():
		session[SESSION_APPS_SHOW_DELETED] = form.show.data
		page = 1
	
	count = a.apps.get_apps_count(form.show.data)
	per_page = app.config.get('ADMIN_APPS_PER_PAGE', 20)
	offset, limit, pages = paginate(page, count, per_page)
	aps = do_or_abort(a.apps.get_apps, with_deleted=form.show.data,
					offset=offset, limit=limit, full=True)
	return render_template('admin/apps.html', apps=aps, pages=pages, form=form)
def info_balance():
	form = None
	if g.user.is_customer():
		form = forms.BalanceForm(request.form)
		if request.method == 'POST' and form.validate():
			sum = form.amount.data
			url = robokassa.account_pay_url(
				account_id=g.user.customer_account.id,
				sum=round(sum, 2),
				email=g.user.email)
			return redirect(url)
	
	account = g.user.customer_account if g.user.is_customer() else g.user.developer_account
	page = convert.to_int(request.args.get('page'), 1)
	count = actions.accounts.get_account_transactions_count(account.id)
	per_page = app.config.get('ADMIN_TRANSACTIONS_PER_PAGE', 20)
	offset, limit, pages = paginate(page, count, per_page)
	transactions = actions.accounts.get_account_transactions(account_id=account.id, offset=offset, limit=limit)
	return render_template('cabinet/info-balance.html', transactions=transactions, pages=pages, form=form)
def feedback():
	filters = {
		'contacts': (~(Contact.partner == True), ),
		'partners': (Contact.partner == True, )
	}.get(request.args.get('filter', ''), (()) )
	
	if request.method == 'POST':
		contacts = Contact.query.filter(Contact.read == False, *filters)
		for contact in contacts: # For some reason set/execute query not working
			contact.read = True
			contact.save()
		g.feedback_unread = Contact.query.filter(Contact.read == False).count()
	
	page = convert.to_int(request.args.get('page'), 1)
	count = Contact.query.filter(*filters).count()
	per_page = app.config.get('ADMIN_CONTACTS_PER_PAGE', 10)
	offset, limit, pages = paginate(page, count, per_page)
	contacts = Contact.query.filter(*filters).descending(Contact.date).skip(offset).limit(limit)
	return render_template('admin/feedback.html', contacts=contacts.all(), pages=pages)