# Path: recruit_app/admin.py
# class AuthenticatedModelView(ModelView):
#     column_display_pk = True
# 
#     def is_accessible(self):
#         if current_user.has_role("admin"):
#             return True
#         return False
# 
#     def _handle_view(self, name, **kwargs):
#         if not self.is_accessible():
#             abort(401)
# 
# Path: recruit_app/blacklist/models.py
# class BlacklistCharacter(SurrogatePK, TimeMixin, Model):
#     __tablename__ = 'blacklist_character'
#     # __searchable__ = ['name', 'main_name', 'corporation', 'alliance', 'notes', 'ip_address']
# 
#     name = Column(db.Unicode, nullable=True)
#     main_name = Column(db.Unicode, nullable=True)
#     corporation = Column(db.Unicode)
#     alliance = Column(db.Unicode)
#     notes = Column(db.Unicode)
# 
#     ip_address = Column(db.Unicode)
# 
#     creator_id = ReferenceCol('users', nullable=True)
#     creator = relationship('User', foreign_keys=[creator_id], backref='blacklist_character_entries')
# 
#     def __repr__(self):
#         return '<' + self.name + ': ' + self.notes + '>'
# 
# class BlacklistGSF(TimeMixin, Model):
#     __tablename__ = 'blacklist_gsf'
#     
#     status = Column(db.Unicode)
#     character_id = ReferenceCol('characters', pk_name='character_id', primary_key=True)
#     character = relationship('EveCharacter', foreign_keys=[character_id], backref=backref('blacklist_gsf', cascade="delete"))
#     
#     @staticmethod
#     def getStatus(character):
#         try:
#             entry = BlacklistGSF.query.filter_by(character_id=character.character_id).one()
#         except:
#             # No entry, create a new one
#             entry = BlacklistGSF()
#             entry.character_id = character.character_id
#             entry.status = u'UNKNOWN'
#         
#         if entry.last_update_time is None or (dt.datetime.utcnow() - entry.last_update_time).total_seconds() > 86400: # 1 day cache
#             try:
#                 url = current_app.config['GSF_BLACKLIST_URL'] + character.character_name
#                 r   = requests.post(url)
#                 entry.status = unicode(r.json()[0]['output'])
#                 entry.last_update_time = dt.datetime.utcnow()
#             except: # Will except on NONE for URL or connection issues.  Just keep status as UNKNOWN
#                 pass
#             
#             try:
#                 entry.save()
#             except:
#                 # It's possible that multiple people are viewing the same new app at the same time, causing multiple threads to make the same cache object,
#                 # which throws an IntegrityError.  In that case just ignore the error, this is just a cache anyway.
#                 pass
# 
#         return str(entry.status)
# 
#     def __repr__(self):
#         return '<BlacklistCacheEntry' + ': ' + self.character_id + '>'


from recruit_app.admin import AuthenticatedModelView
from .models import BlacklistCharacter, BlacklistGSF

def register_admin_views(admin, db):
    admin.add_view(BlacklistCharacterAdmin(BlacklistCharacter, db.session, category='BlackList'))
    admin.add_view(BlacklistGSFAdmin(BlacklistGSF, db.session, name='GSF Blacklist Cache', category='BlackList'))

# Useful things: column_list, column_labels, column_searchable_list, column_filters, form_columns, form_ajax_refs
class BlacklistCharacterAdmin(AuthenticatedModelView):
    column_searchable_list = (
        'name',
        'main_name',
        'corporation',
        'alliance',
        'notes',
        'ip_address',
        'creator.email', )
    column_labels = {
        'creator.email': 'Creator', }
    column_list = column_searchable_list
    column_filters = column_list
    form_ajax_refs = {
        'creator': { 'fields': ('email', ) }, }



# Path: recruit_app/admin.py
# class AuthenticatedModelView(ModelView):
#     column_display_pk = True
# 
#     def is_accessible(self):
#         if current_user.has_role("admin"):
#             return True
#         return False
# 
#     def _handle_view(self, name, **kwargs):
#         if not self.is_accessible():
#             abort(401)
# 
# Path: recruit_app/blacklist/models.py
# class BlacklistCharacter(SurrogatePK, TimeMixin, Model):
#     __tablename__ = 'blacklist_character'
#     # __searchable__ = ['name', 'main_name', 'corporation', 'alliance', 'notes', 'ip_address']
# 
#     name = Column(db.Unicode, nullable=True)
#     main_name = Column(db.Unicode, nullable=True)
#     corporation = Column(db.Unicode)
#     alliance = Column(db.Unicode)
#     notes = Column(db.Unicode)
# 
#     ip_address = Column(db.Unicode)
# 
#     creator_id = ReferenceCol('users', nullable=True)
#     creator = relationship('User', foreign_keys=[creator_id], backref='blacklist_character_entries')
# 
#     def __repr__(self):
#         return '<' + self.name + ': ' + self.notes + '>'
# 
# class BlacklistGSF(TimeMixin, Model):
#     __tablename__ = 'blacklist_gsf'
#     
#     status = Column(db.Unicode)
#     character_id = ReferenceCol('characters', pk_name='character_id', primary_key=True)
#     character = relationship('EveCharacter', foreign_keys=[character_id], backref=backref('blacklist_gsf', cascade="delete"))
#     
#     @staticmethod
#     def getStatus(character):
#         try:
#             entry = BlacklistGSF.query.filter_by(character_id=character.character_id).one()
#         except:
#             # No entry, create a new one
#             entry = BlacklistGSF()
#             entry.character_id = character.character_id
#             entry.status = u'UNKNOWN'
#         
#         if entry.last_update_time is None or (dt.datetime.utcnow() - entry.last_update_time).total_seconds() > 86400: # 1 day cache
#             try:
#                 url = current_app.config['GSF_BLACKLIST_URL'] + character.character_name
#                 r   = requests.post(url)
#                 entry.status = unicode(r.json()[0]['output'])
#                 entry.last_update_time = dt.datetime.utcnow()
#             except: # Will except on NONE for URL or connection issues.  Just keep status as UNKNOWN
#                 pass
#             
#             try:
#                 entry.save()
#             except:
#                 # It's possible that multiple people are viewing the same new app at the same time, causing multiple threads to make the same cache object,
#                 # which throws an IntegrityError.  In that case just ignore the error, this is just a cache anyway.
#                 pass
# 
#         return str(entry.status)
# 
#     def __repr__(self):
#         return '<BlacklistCacheEntry' + ': ' + self.character_id + '>'


# Path: recruit_app/blacklist/admin.py
from recruit_app.admin import AuthenticatedModelView
from .models import BlacklistCharacter, BlacklistGSF


def register_admin_views(admin, db):
    admin.add_view(BlacklistCharacterAdmin(BlacklistCharacter, db.session, category='BlackList'))
    admin.add_view(BlacklistGSFAdmin(BlacklistGSF, db.session, name='GSF Blacklist Cache', category='BlackList'))

# Useful things: column_list, column_labels, column_searchable_list, column_filters, form_columns, form_ajax_refs
class BlacklistCharacterAdmin(AuthenticatedModelView):
    column_searchable_list = (
        'name',
        'main_name',
        'corporation',
        'alliance',
        'notes',
        'ip_address',
        'creator.email', )
    column_labels = {
        'creator.email': 'Creator', }
    column_list = column_searchable_list
    column_filters = column_list
    form_ajax_refs = {
        'creator': { 'fields': ('email', ) }, }



class BlacklistGSFAdmin(AuthenticatedModelView):