# 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):


import errno
import flask
import os
from auth.models import Network, User, Gateway, Voucher, Category, Product, Country, Currency, db
from flask_potion import Api, fields, signals
from flask_potion.routes import Relation, Route, ItemRoute
from flask_potion.contrib.principals import PrincipalResource, PrincipalManager
from flask_security import current_user
from flask_uploads import UploadSet, IMAGES
from PIL import Image



super_admin_only = 'super-admin'
network_or_above = ['super-admin', 'network-admin']
gateway_or_above = ['super-admin', 'network-admin', 'gateway-admin']

api = Api(prefix='/api')
logos = UploadSet('logos', IMAGES)

def mkdir_p(path):
    try:
        os.makedirs(path)
    except OSError as exc: # Python >2.5
        if exc.errno == errno.EEXIST and os.path.isdir(path):
            pass
        else: raise

class Manager(PrincipalManager):
    def instances(self, where=None, sort=None):
        query = PrincipalManager.instances(self, where, sort)

        if current_user.has_role('network-admin') or current_user.has_role('gateway-admin'):
            if self.model == Network:
                query = query.filter_by(id=current_user.network_id)
            elif self.model in [ Gateway, User ]:
                query = query.filter_by(network_id=current_user.network_id)

        if current_user.has_role('network-admin'):
            if self.model == Voucher:

# 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
import errno
import flask
import os
from auth.models import Network, User, Gateway, Voucher, Category, Product, Country, Currency, db
from flask_potion import Api, fields, signals
from flask_potion.routes import Relation, Route, ItemRoute
from flask_potion.contrib.principals import PrincipalResource, PrincipalManager
from flask_security import current_user
from flask_uploads import UploadSet, IMAGES
from PIL import Image




super_admin_only = 'super-admin'
network_or_above = ['super-admin', 'network-admin']
gateway_or_above = ['super-admin', 'network-admin', 'gateway-admin']

api = Api(prefix='/api')
logos = UploadSet('logos', IMAGES)

def mkdir_p(path):
    try:
        os.makedirs(path)
    except OSError as exc: # Python >2.5
        if exc.errno == errno.EEXIST and os.path.isdir(path):
            pass
        else: raise

class Manager(PrincipalManager):
    def instances(self, where=None, sort=None):
        query = PrincipalManager.instances(self, where, sort)

        if current_user.has_role('network-admin') or current_user.has_role('gateway-admin'):
            if self.model == Network:
                query = query.filter_by(id=current_user.network_id)
            elif self.model in [ Gateway, User ]:
                query = query.filter_by(network_id=current_user.network_id)

        if current_user.has_role('network-admin'):
            if self.model == Voucher:

                query = query.join(Voucher.gateway).join(Gateway.network).filter(Network.id == current_user.network_id)