# Path: auth/constants.py
# AUTH_ALLOWED = 1
# AUTH_DENIED = 0
# AUTH_ERROR = -1
# AUTH_VALIDATION = 5
# AUTH_VALIDATION_FAILED = 6
# STAGE_COUNTERS = 'counters'
# STAGE_LOGIN = 'login'
# STAGE_LOGOUT = 'logout'
# ROLES = {
#     u'super-admin': u'Super Admin',
#     u'network-admin': u'Network Admin',
#     u'gateway-admin': u'Gateway Admin',
#     u'service': u'Service',
# }
# STATUS_ICONS = {
#     'active': 'bolt',
#     'archived': 'trash',
#     'blocked': 'thumb-down',
#     'ended': 'flag',
#     'expired': 'circle-x',
#     'new': 'file',
# }
# ACTIONS = {
#     'vouchers': [
#         'extend',
#         'block',
#         'unblock',
#         'archive',
#     ],
# }
# 
# Path: auth/forms.py
# def default_megabytes():
# def default_minutes():
# def instances(resource):
#     def func():
# def roles():
#     def conv_String(self, field_args, **extra):
#     def validate_voucher(self, form, field):
# class GatewayConverter(ModelConverter):
# class UserForm(FlaskForm):
# class MyUserForm(FlaskForm):
# class NewVoucherForm(FlaskForm):
# class BroadcastForm(FlaskForm):
# class LoginVoucherForm(FlaskForm):
# 
# Path: auth/models.py
# def set_sqlite_pragma(dbapi_connection, connection_record):
# def generate_code():
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
# def record_change(f):
#     def func(self, **kwargs):
#     def should_expire(self):
#     def should_end(self):
#     def megabytes_are_finished(self):
#     def time_left(self):
#     def end_at(self):
#     def extend(self):
#     def login(self):
#     def end(self):
#     def expire(self):
#     def block(self):
#     def unblock(self):
#     def archive(self):
#     def available_actions(self):
#     def __str__(self):
#     def matches_voucher(self, voucher):
#     def process_request(self):
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
# class Role(db.Model, RoleMixin):
# class User(db.Model, UserMixin):
# class Network(db.Model):
# class Gateway(db.Model):
# class Voucher(db.Model):
# class Auth(db.Model):
# class Change(db.Model):
# class Country(db.Model):
# class Currency(db.Model):
# class Category(db.Model):
# class Product(db.Model):
# class Order(db.Model):
# class OrderItem(db.Model):
# class Transaction(db.Model):
# 
# Path: auth/resources.py
# def mkdir_p(path):
#     def instances(self, where=None, sort=None):
#     def instances(self, where=None, sort=None):
#     def extend(self, voucher, commit=True):
#     def block(self, voucher, commit=True):
#     def unblock(self, voucher, commit=True):
#     def archive(self, voucher, commit=True):
#     def current(self):
#     def logo(self, gateway):
#     def extend(self, voucher):
#     def block(self, voucher):
#     def unblock(self, voucher):
#     def archive(self, voucher):
# def set_scope(sender, item):
# class Manager(PrincipalManager):
# class VoucherManager(Manager):
# class UserResource(PrincipalResource):
#     class Meta:
#     class Schema:
# class GatewayResource(PrincipalResource):
#     class Meta:
#     class Schema:
# class NetworkResource(PrincipalResource):
#     class Meta:
#     class Schema:
# class VoucherResource(PrincipalResource):
#     class Meta:
#     class Schema:
# class CategoryResource(PrincipalResource):
#     class Meta:
#     class Schema:
# class ProductResource(PrincipalResource):
#     class Meta:
#     class Schema:
# class CountryResource(PrincipalResource):
#     class Meta:
#     class Schema:
# class CurrencyResource(PrincipalResource):
#     class Meta:
#     class Schema:
# 
# Path: auth/services.py

# 
# Path: auth/utils.py
# def is_logged_in():
#     return current_user.is_authenticated
# 
# def has_role(*roles):
#     def func():
#         if current_user.is_authenticated:
#             for role in roles:
#                 if current_user.has_role(role):
#                     return True
#         return False
#     return func


import os
import uuid
from auth import constants
from auth.forms import \
    CategoryForm, \
    CountryForm, \
    CurrencyForm, \
    GatewayForm, \
    LoginVoucherForm, \
    MyUserForm, \
    NetworkForm, \
    NewVoucherForm, \
    ProductForm, \
    UserForm
from auth.models import Auth, Category, Country, Currency, Gateway, Network, Product, User, Voucher, db
from auth.resources import logos
from auth.services import \
        environment_dump, \
        healthcheck as healthcheck_service
from auth.utils import is_logged_in, has_role
from flask import \
    Blueprint, \
    abort, \
    current_app, \
    flash, \
    redirect, \
    request, \
    render_template, \
    send_from_directory, \
    session, \
    url_for
from flask_menu import register_menu
from flask_potion.exceptions import ItemNotFound
from flask_security import \
    auth_token_required, \
    current_user, \
    login_required, \
    roles_accepted
from PIL import Image

# from auth.payu import get_transaction, set_transaction, capture



bp = Blueprint('auth', __name__)

RESOURCE_MODELS = {
    'categories': Category,
    'countries': Country,
    'currencies': Currency,
    'gateways': Gateway,
    'networks': Network,
    'products': Product,
    'users': User,
    'vouchers': Voucher,
}


def generate_token():
    """Generate token for the voucher session"""
    return uuid.uuid4().hex


def resource_query(resource):
    """Generate a filtered query for a resource"""
    model = RESOURCE_MODELS[resource]
    query = model.query

    if current_user.has_role('network-admin') or current_user.has_role('gateway-admin'):

# Path: auth/constants.py
# AUTH_ALLOWED = 1
# AUTH_DENIED = 0
# AUTH_ERROR = -1
# AUTH_VALIDATION = 5
# AUTH_VALIDATION_FAILED = 6
# STAGE_COUNTERS = 'counters'
# STAGE_LOGIN = 'login'
# STAGE_LOGOUT = 'logout'
# ROLES = {
#     u'super-admin': u'Super Admin',
#     u'network-admin': u'Network Admin',
#     u'gateway-admin': u'Gateway Admin',
#     u'service': u'Service',
# }
# STATUS_ICONS = {
#     'active': 'bolt',
#     'archived': 'trash',
#     'blocked': 'thumb-down',
#     'ended': 'flag',
#     'expired': 'circle-x',
#     'new': 'file',
# }
# ACTIONS = {
#     'vouchers': [
#         'extend',
#         'block',
#         'unblock',
#         'archive',
#     ],
# }
# 
# Path: auth/forms.py
# def default_megabytes():
# def default_minutes():
# def instances(resource):
#     def func():
# def roles():
#     def conv_String(self, field_args, **extra):
#     def validate_voucher(self, form, field):
# class GatewayConverter(ModelConverter):
# class UserForm(FlaskForm):
# class MyUserForm(FlaskForm):
# class NewVoucherForm(FlaskForm):
# class BroadcastForm(FlaskForm):
# class LoginVoucherForm(FlaskForm):
# 
# Path: auth/models.py
# def set_sqlite_pragma(dbapi_connection, connection_record):
# def generate_code():
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
# def record_change(f):
#     def func(self, **kwargs):
#     def should_expire(self):
#     def should_end(self):
#     def megabytes_are_finished(self):
#     def time_left(self):
#     def end_at(self):
#     def extend(self):
#     def login(self):
#     def end(self):
#     def expire(self):
#     def block(self):
#     def unblock(self):
#     def archive(self):
#     def available_actions(self):
#     def __str__(self):
#     def matches_voucher(self, voucher):
#     def process_request(self):
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
# class Role(db.Model, RoleMixin):
# class User(db.Model, UserMixin):
# class Network(db.Model):
# class Gateway(db.Model):
# class Voucher(db.Model):
# class Auth(db.Model):
# class Change(db.Model):
# class Country(db.Model):
# class Currency(db.Model):
# class Category(db.Model):
# class Product(db.Model):
# class Order(db.Model):
# class OrderItem(db.Model):
# class Transaction(db.Model):
# 
# Path: auth/resources.py
# def mkdir_p(path):
#     def instances(self, where=None, sort=None):
#     def instances(self, where=None, sort=None):
#     def extend(self, voucher, commit=True):
#     def block(self, voucher, commit=True):
#     def unblock(self, voucher, commit=True):
#     def archive(self, voucher, commit=True):
#     def current(self):
#     def logo(self, gateway):
#     def extend(self, voucher):
#     def block(self, voucher):
#     def unblock(self, voucher):
#     def archive(self, voucher):
# def set_scope(sender, item):
# class Manager(PrincipalManager):
# class VoucherManager(Manager):
# class UserResource(PrincipalResource):
#     class Meta:
#     class Schema:
# class GatewayResource(PrincipalResource):
#     class Meta:
#     class Schema:
# class NetworkResource(PrincipalResource):
#     class Meta:
#     class Schema:
# class VoucherResource(PrincipalResource):
#     class Meta:
#     class Schema:
# class CategoryResource(PrincipalResource):
#     class Meta:
#     class Schema:
# class ProductResource(PrincipalResource):
#     class Meta:
#     class Schema:
# class CountryResource(PrincipalResource):
#     class Meta:
#     class Schema:
# class CurrencyResource(PrincipalResource):
#     class Meta:
#     class Schema:
# 
# Path: auth/services.py

# 
# Path: auth/utils.py
# def is_logged_in():
#     return current_user.is_authenticated
# 
# def has_role(*roles):
#     def func():
#         if current_user.is_authenticated:
#             for role in roles:
#                 if current_user.has_role(role):
#                     return True
#         return False
#     return func


# Path: auth/views.py
import os
import uuid
from auth import constants
from auth.forms import \
    CategoryForm, \
    CountryForm, \
    CurrencyForm, \
    GatewayForm, \
    LoginVoucherForm, \
    MyUserForm, \
    NetworkForm, \
    NewVoucherForm, \
    ProductForm, \
    UserForm
from auth.models import Auth, Category, Country, Currency, Gateway, Network, Product, User, Voucher, db
from auth.resources import logos
from auth.services import \
        environment_dump, \
        healthcheck as healthcheck_service
from auth.utils import is_logged_in, has_role
from flask import \
    Blueprint, \
    abort, \
    current_app, \
    flash, \
    redirect, \
    request, \
    render_template, \
    send_from_directory, \
    session, \
    url_for
from flask_menu import register_menu
from flask_potion.exceptions import ItemNotFound
from flask_security import \
    auth_token_required, \
    current_user, \
    login_required, \
    roles_accepted
from PIL import Image


# from auth.payu import get_transaction, set_transaction, capture



bp = Blueprint('auth', __name__)

RESOURCE_MODELS = {
    'categories': Category,
    'countries': Country,
    'currencies': Currency,
    'gateways': Gateway,
    'networks': Network,
    'products': Product,
    'users': User,
    'vouchers': Voucher,
}


def generate_token():
    """Generate token for the voucher session"""
    return uuid.uuid4().hex


def resource_query(resource):
    """Generate a filtered query for a resource"""
    model = RESOURCE_MODELS[resource]
    query = model.query

    if current_user.has_role('network-admin') or current_user.has_role('gateway-admin'):

        if model == Network: