import logging

from pylons import request, response, session, tmpl_context as c, url, config
from pylons.controllers.util import abort, redirect

from paste.deploy.converters import aslist

from ppdi.model import ModeratorPin
from ppdi.model.meta import Session

from sqlalchemy import and_
from sqlalchemy.exc import SQLAlchemyError

from ppdi.lib.base import BaseController, render

log = logging.getLogger(__name__)

class AdminController(BaseController):

    requires_auth = True

    def index(self):
        # Check if this user is really an admin...
        self.checkAdmin()
        
        c.users = []
        try:
            c.users = Session.query(ModeratorPin.username, ModeratorPin.domain).distinct().all()
        except SQLAlchemyError, e:
            log.critical('Something went wrong... %s' % e)
        
        return render('/derived/main/admin/index.html')
        
    def admin_user(self):
        """Administrate a certain user"""
        self.checkAdmin()
        
        c.username = request.params.get('username').split('@')[0]
        c.domain = request.params.get('username').split('@')[1]
        
        try:
            # Get the user conferences
            c.pins = Session.query(ModeratorPin).filter(and_(ModeratorPin.username==c.username, ModeratorPin.domain==c.domain)).all()
        except SQLAlchemyError, e:
            log.critical("Could not fetch: %s" % e)
        
        return render('/derived/main/admin/admin_user.html')
