<commit_before>import flask.views
from pms.modelo.usuarioControlador import validar, getUsuarios, eliminarUsuario, getUsuario, crearUsuario, getUsuarioById, editarUsuario, comprobarUsuario, usuarioIsLider
from pms.modelo.proyectoControlador import getProyectosFiltrados, getProyectosPaginados, getCantProyectos, comprobarProyecto, crearProyecto, getProyectos, eliminarProyecto, getProyectoId, inicializarProyecto, getProyecto
from pms.modelo.peticionControlador import crearPeticion, getPeticion, eliminarPeticion, editarPeticion
from pms.modelo.itemControlador import getVersionesItemParaSolicitud
from datetime import datetime
import pms.vista.required
from pms.modelo.rolControlador import getProyectosDeUsuario
from pms import app
from pms.vista.paginar import calculoDeSiguiente, calculoDeAnterior, calculoPrimeraPag
TAM_PAGINA=5

class AdmSolicitud(flask.views.MethodView):
    """
    Gestiona y Ejecuta la Vista de Administrar Solicitudes
    """
    @pms.vista.required.login_required
    def get(self):
        """
        Ejecuta el template admSolicitud.html
        """
        global TAM_PAGINA
        flask.session.pop('aux1',None)
        flask.session.pop('aux2',None)
        flask.session.pop('aux3',None)
        flask.session.pop('aux4',None)

        p=getProyectoId(flask.session['proyectoid'])
        ls=p.solicitudes
        return flask.render_template('admSolicitud.html',solicitudes=ls, infopag="Pagina 1 de 1", buscar=False)
    @pms.vista.required.login_required
    def post(self):
        """
        Ejecuta el template admSolicitud.html
        """
        if flask.request.form['fil']!="":
            infopag=""
            if 'buscar' in flask.request.form:
                flask.session['pagina']=1
                p=getProyectosPaginados(flask.session['pagina']-1,TAM_PAGINA, flask.request.form['fil'])
                flask.session['filtro']=flask.request.form['fil']
                if(p!=None):#Si devolvio algo
                    infopag=calculoPrimeraPag(getProyectosFiltrados(flask.session['filtro']).count())
            if 'sgte' in flask.request.form:
                infopag=calculoDeSiguiente(getCantProyectos(flask.session['filtro']))
                p=getProyectosPaginados(flask.session['pagina']-1,TAM_PAGINA, flask.session['filtro'])
            if 'anterior' in flask.request.form:
                infopag=calculoDeAnterior(getCantProyectos(flask.session['filtro']))
                p=getProyectosPaginados(flask.session['pagina']-1,TAM_PAGINA, flask.session['filtro'])

            return flask.render_template('admProyecto.html',proyectos=p,infopag=infopag,buscar=True)
        else:
            return flask.redirect(flask.url_for('admproyecto'))

class Crearsolicitud(flask.views.MethodView):
    """
    Vista de Crear Solicitud
    """
    iversiones=[]
    acciones=[]
    def getItemsSolicitud(self):
        self.iversiones=getVersionesItemParaSolicitud(flask.session['proyectoid'])
        self.acciones=[]
        aux=[]
        aux.append("Editar")
        aux.append(True)
        self.acciones.append(aux)
        aux=[]
        aux.append("Eliminar")
        aux.append(False)
        self.acciones.append(aux)
        aux=[]
        aux.append("Crear Relacion")
        aux.append(False)
        self.acciones.append(aux)
        aux=[]
        aux.append("Eliminar Relacion")
        aux.append(False)
        self.acciones.append(aux)

    @pms.vista.required.login_required
    def get(self):

        self.getItemsSolicitud()
        return flask.render_template('crearSolicitud.html', versiones=self.iversiones, acciones=self.acciones)
    @pms.vista.required.login_required
    def post(self):
        """
        Ejecuta la funcion de Crear Solicitud
        """
        self.getItemsSolicitud()
        ag=[]#lista items para pasarle a la funcion que crea la solicitud
        items=[]
        for u in self.iversiones:
            aux=[]
            aux.append(u[0])
            if u[0].nombre in flask.request.form:
                check=True
                ag.append(u[0])
            else:
                check=False
            aux.append(check)
            items.append(aux)

        acc=0#lista de acciones para pasarle a la funcion que crea la solicitud
        acciones=[]
        aux=[]
        aux.append("Editar")
        if "Editar"in flask.request.form:
            acc=1
            aux.append(True)
        else:
            aux.append(False)
        acciones.append(aux)

        aux=[]
        aux.append("Eliminar")
        if "Eliminar" in flask.request.form:
            acc=acc+10
            aux.append(True)
        else:
            aux.append(False)
        acciones.append(aux)

        aux=[]
        aux.append("Crear Relacion")
        if "Crear Relacion" in flask.request.form:
            acc=acc+100
            aux.append(True)
        else:
            aux.append(False)
        acciones.append(aux)

        aux=[]
        aux.append("Eliminar Relacion")
        if "Eliminar Relacion" in flask.request.form:
            acc=acc+1000
            aux.append(True)
        else:
            aux.append(False)
        acciones.append(aux)

        error=False
        if len(ag)==0:
            flask.flash(u"Debe seleccionar al menos un Item", "item")
            error=True
        if acc==0:
            flask.flash(u"Debe seleccionar al menos una Accion", "accion")
            error=True
        if flask.request.form['comentario']=="":
            flask.flash(u"El campo no puede estar vacio","comentario")
            error=True
        if error:
            return flask.render_template('crearSolicitud.html', versiones=items, acciones=acciones)
        crearPeticion(flask.session['proyectoid'],flask.request.form['comentario'],flask.session['usuarioid'],ag,acc )

        flask.flash(u"CREACION EXITOSA","text-success")
        return flask.redirect(flask.url_for('admsolicitud'))

class EditarSolicitud(flask.views.MethodView):
    """
    Gestiona la Vista de Editar Tipo de Item
    """
    @pms.vista.required.login_required
    def get(self):
        return flask.redirect('/admsolicitud/'+str(flask.session['proyectoid']))

    @pms.vista.required.login_required
    def post(self):
        self.getItemsSolicitud()
        ag=[]#lista items para pasarle a la funcion que crea la solicitud
        items=[]
        for u in self.iversiones:
            aux=[]
            aux.append(u[0])
            if u[0].nombre in flask.request.form:
                check=True
                ag.append(u[0])
            else:
                check=False
            aux.append(check)
            items.append(aux)

        l=[]#lista de acciones para pasarle a la funcion que crea la solicitud
        acciones=[]
        aux=[]
        aux.append("Editar")
        if "Editar"in flask.request.form:
            l.insert(0, True)
            aux.append(True)
        else:
            l.insert(0, False)
            aux.append(False)
        acciones.append(aux)

        aux=[]
        aux.append("Eliminar")
        if "Eliminar" in flask.request.form:
            l.insert(1, True)
            aux.append(True)
        else:
            l.insert(1, False)
            aux.append(False)
        acciones.append(aux)

        aux=[]
        aux.append("Crear Relacion")
        if "Crear Relacion" in flask.request.form:
            l.insert(2, True)
            aux.append(True)
        else:
            l.insert(2, False)
            aux.append(False)
        acciones.append(aux)

        aux=[]
        aux.append("Eliminar Relacion")
        if "Eliminar Relacion" in flask.request.form:
            l.insert(3, True)
            aux.append(True)
        else:
            l.insert(3, False)
            aux.append(False)
        acciones.append(aux)

        error=False
        if len(ag)==0:
            flask.flash(u"Debe seleccionar al menos un Item", "item")
            error=True
        if not True in l:
            flask.flash(u"Debe seleccionar al menos una Accion", "accion")
            error=True
        if flask.request.form['comentario']=="":
            flask.flash(u"El campo no puede estar vacio","comentario")
            error=True
        if error:
            return flask.render_template('editarSolicitud.html', versiones=items, acciones=acciones)
        #editarSolicitud(flask.session['solicitudid'], ag, l, flask.request.form['comentario'])
        flask.flash(u"EDICION EXITOSA","text-success")
        return flask.redirect(flask.url_for('admsolicitud'))

class EliminarSolicitud(flask.views.MethodView):
    """
    Gestiona la Vista de Editar Tipo de Item
    """
    @pms.vista.required.login_required
    def get(self):
        return flask.redirect('/admsolicitud/'+str(flask.session['proyectoid']))

    @pms.vista.required.login_required
    def post(self):
        eliminarPeticion(flask.session['solicitudid'])
        flask.flash(u"ELIMINACION EXITOSA","text-success")
        return flask.redirect(flask.url_for('admsolicitud'))

@app.route('/admsolicitud/editar/<s>')
@pms.vista.required.login_required
def edSolicitud(s=None):
    """
    Funcion que llama a la Vista de Editar Solicitud responde al boton de 'Editar' de Administrar Solicitud
    recibe el de la solicitud a editarse
    """
    #soli=getSolicitud(s)
    flask.session['solicitudid']=s
    iversiones=getVersionesItemParaSolicitud(flask.session['proyectoid'])
    """for i in i versiones:
        if i[0] in soli.items:
            i[1]=True
    acc=[]
    aux=[]
    aux.append("Editar")
    if soli.acciones%10==1:
        aux.append(True)
    else:
        aux.append(False)"""

    return flask.render_template('editarSolicitud.html')

@app.route('/admsolicitud/eliminar/<s>')
@pms.vista.required.login_required
def eSolicitud(s=None):
    """
    Funcion que despliega la Vista de Eliminar Solicitud, llama a eliminarSolicitud.html
    responde al boton de 'Eliminar' de Administrar Solicitud
    recibe el id de la Solicitud a eliminarce
    """
    flask.session.pop('aux1',None)
    flask.session.pop('aux2',None)
    soli=getPeticion(s)
    acc=[]
    acc.insert(0, ["Editar",soli.acciones%10==1])
    acc.insert(1,["Eliminar",soli.acciones%100>=10])
    acc.insert(2,["Crear Relacion",soli.acciones%1000==100])
    acc.insert(3,["Eliminar Relacion",soli.acciones%10000==1000])
    """aux=[]
    aux.append("Editar")
    if soli.acciones%10==1:
        acc.append(True)
    else:
        acc.append(True)
    acc.append(aux)

    aux=[]
    aux.append("Eliminar")
    if soli.acciones%100>=10:
        acc.append(True)
    else:
        acc.append(False)
    acc.append(aux)

    aux=[]
    aux.append("Crear Relacion")
    if soli.acciones%1000>=100:
        acc.append(True)
    else:
        acc.append(False)
    acc.append(aux)

    aux=[]
    aux.append("Eliminar Relacion")
    if soli.acciones%10000>=1000:
        acc.append(True)
    else:
        acc.append(False)
    acc.append(aux)"""

    flask.session['solicitudid']=s
    return flask.render_template('eliminarSolicitud.html',s=soli, acciones=acc)

@app.route('/admsolicitud/consultar/<s>')
@pms.vista.required.login_required
def consultarSolicitud(s=None):
    """
    Funcion que despliega pagina de consulta de Solicitud llama a consultarSolicitud.html
    responde al boton de 'Consultar' de Administrar Solicitud
    recibe el id de la solicitud a consultar
    """
    #soli=getSolicitud(s)
    return flask.render_template('consultarSolicitud.html')

<commit_msg>balita<commit_after>import flask.views
from pms.modelo.usuarioControlador import validar, getUsuarios, eliminarUsuario, getUsuario, crearUsuario, getUsuarioById, editarUsuario, comprobarUsuario, usuarioIsLider
from pms.modelo.proyectoControlador import getProyectosFiltrados, getProyectosPaginados, getCantProyectos, comprobarProyecto, crearProyecto, getProyectos, eliminarProyecto, getProyectoId, inicializarProyecto, getProyecto
from pms.modelo.peticionControlador import crearPeticion, getPeticion, eliminarPeticion, editarPeticion
from pms.modelo.itemControlador import getVersionesItemParaSolicitud
from datetime import datetime
import pms.vista.required
from pms.modelo.rolControlador import getProyectosDeUsuario
from pms import app
from pms.vista.paginar import calculoDeSiguiente, calculoDeAnterior, calculoPrimeraPag
TAM_PAGINA=5

class AdmSolicitud(flask.views.MethodView):
    """
    Gestiona y Ejecuta la Vista de Administrar Solicitudes
    """
    @pms.vista.required.login_required
    def get(self):
        """
        Ejecuta el template admSolicitud.html
        """
        global TAM_PAGINA
        flask.session.pop('aux1',None)
        flask.session.pop('aux2',None)
        flask.session.pop('aux3',None)
        flask.session.pop('aux4',None)

        p=getProyectoId(flask.session['proyectoid'])
        ls=p.solicitudes
        return flask.render_template('admSolicitud.html',solicitudes=ls, infopag="Pagina 1 de 1", buscar=False)
    @pms.vista.required.login_required
    def post(self):
        """
        Ejecuta el template admSolicitud.html
        """
        if flask.request.form['fil']!="":
            infopag=""
            if 'buscar' in flask.request.form:
                flask.session['pagina']=1
                p=getProyectosPaginados(flask.session['pagina']-1,TAM_PAGINA, flask.request.form['fil'])
                flask.session['filtro']=flask.request.form['fil']
                if(p!=None):#Si devolvio algo
                    infopag=calculoPrimeraPag(getProyectosFiltrados(flask.session['filtro']).count())
            if 'sgte' in flask.request.form:
                infopag=calculoDeSiguiente(getCantProyectos(flask.session['filtro']))
                p=getProyectosPaginados(flask.session['pagina']-1,TAM_PAGINA, flask.session['filtro'])
            if 'anterior' in flask.request.form:
                infopag=calculoDeAnterior(getCantProyectos(flask.session['filtro']))
                p=getProyectosPaginados(flask.session['pagina']-1,TAM_PAGINA, flask.session['filtro'])

            return flask.render_template('admProyecto.html',proyectos=p,infopag=infopag,buscar=True)
        else:
            return flask.redirect(flask.url_for('admproyecto'))

class Crearsolicitud(flask.views.MethodView):
    """
    Vista de Crear Solicitud
    """
    iversiones=[]
    def getItemsSolicitud(self):
        self.iversiones=getVersionesItemParaSolicitud(flask.session['proyectoid'])

    @pms.vista.required.login_required
    def get(self):
        acciones=[]
        acciones.insert(0, ["Editar",True])
        acciones.insert(1,["Eliminar",False])
        acciones.insert(2,["Crear Relacion",False])
        acciones.insert(3,["Eliminar Relacion",False])
        self.getItemsSolicitud()
        return flask.render_template('crearSolicitud.html', versiones=self.iversiones, acciones=acciones)
    @pms.vista.required.login_required
    def post(self):
        """
        Ejecuta la funcion de Crear Solicitud
        """
        self.getItemsSolicitud()
        ag=[]#lista items para pasarle a la funcion que crea la solicitud
        items=[]
        for u in self.iversiones:
            aux=[]
            aux.append(u[0])
            if u[0].nombre in flask.request.form:
                check=True
                ag.append(u[0])
            else:
                check=False
            aux.append(check)
            items.append(aux)

        acc=0#lista de acciones para pasarle a la funcion que crea la solicitud
        acciones=[]
        aux=[]
        aux.append("Editar")
        if "Editar"in flask.request.form:
            acc=1
            aux.append(True)
        else:
            aux.append(False)
        acciones.append(aux)

        aux=[]
        aux.append("Eliminar")
        if "Eliminar" in flask.request.form:
            acc=acc+10
            aux.append(True)
        else:
            aux.append(False)
        acciones.append(aux)

        aux=[]
        aux.append("Crear Relacion")
        if "Crear Relacion" in flask.request.form:
            acc=acc+100
            aux.append(True)
        else:
            aux.append(False)
        acciones.append(aux)

        aux=[]
        aux.append("Eliminar Relacion")
        if "Eliminar Relacion" in flask.request.form:
            acc=acc+1000
            aux.append(True)
        else:
            aux.append(False)
        acciones.append(aux)

        error=False
        if len(ag)==0:
            flask.flash(u"Debe seleccionar al menos un Item", "item")
            error=True
        if acc==0:
            flask.flash(u"Debe seleccionar al menos una Accion", "accion")
            error=True
        if flask.request.form['comentario']=="":
            flask.flash(u"El campo no puede estar vacio","comentario")
            error=True
        if error:
            return flask.render_template('crearSolicitud.html', versiones=items, acciones=acciones)
        crearPeticion(flask.session['proyectoid'],flask.request.form['comentario'],flask.session['usuarioid'],ag,acc )

        flask.flash(u"CREACION EXITOSA","text-success")
        return flask.redirect(flask.url_for('admsolicitud'))

class EditarSolicitud(flask.views.MethodView):
    """
    Gestiona la Vista de Editar Tipo de Item
    """
    @pms.vista.required.login_required
    def get(self):
        return flask.redirect('/admsolicitud/'+str(flask.session['proyectoid']))

    @pms.vista.required.login_required
    def post(self):
        self.getItemsSolicitud()
        ag=[]#lista items para pasarle a la funcion que crea la solicitud
        items=[]
        for u in self.iversiones:
            aux=[]
            aux.append(u[0])
            if u[0].nombre in flask.request.form:
                check=True
                ag.append(u[0])
            else:
                check=False
            aux.append(check)
            items.append(aux)

        l=[]#lista de acciones para pasarle a la funcion que crea la solicitud
        acciones=[]
        aux=[]
        aux.append("Editar")
        if "Editar"in flask.request.form:
            l.insert(0, True)
            aux.append(True)
        else:
            l.insert(0, False)
            aux.append(False)
        acciones.append(aux)

        aux=[]
        aux.append("Eliminar")
        if "Eliminar" in flask.request.form:
            l.insert(1, True)
            aux.append(True)
        else:
            l.insert(1, False)
            aux.append(False)
        acciones.append(aux)

        aux=[]
        aux.append("Crear Relacion")
        if "Crear Relacion" in flask.request.form:
            l.insert(2, True)
            aux.append(True)
        else:
            l.insert(2, False)
            aux.append(False)
        acciones.append(aux)

        aux=[]
        aux.append("Eliminar Relacion")
        if "Eliminar Relacion" in flask.request.form:
            l.insert(3, True)
            aux.append(True)
        else:
            l.insert(3, False)
            aux.append(False)
        acciones.append(aux)

        error=False
        if len(ag)==0:
            flask.flash(u"Debe seleccionar al menos un Item", "item")
            error=True
        if not True in l:
            flask.flash(u"Debe seleccionar al menos una Accion", "accion")
            error=True
        if flask.request.form['comentario']=="":
            flask.flash(u"El campo no puede estar vacio","comentario")
            error=True
        if error:
            return flask.render_template('editarSolicitud.html', versiones=items, acciones=acciones)
        #editarSolicitud(flask.session['solicitudid'], ag, l, flask.request.form['comentario'])
        flask.flash(u"EDICION EXITOSA","text-success")
        return flask.redirect(flask.url_for('admsolicitud'))

class EliminarSolicitud(flask.views.MethodView):
    """
    Gestiona la Vista de Editar Tipo de Item
    """
    @pms.vista.required.login_required
    def get(self):
        return flask.redirect('/admsolicitud/'+str(flask.session['proyectoid']))

    @pms.vista.required.login_required
    def post(self):
        eliminarPeticion(flask.session['solicitudid'])
        flask.flash(u"ELIMINACION EXITOSA","text-success")
        return flask.redirect(flask.url_for('admsolicitud'))

@app.route('/admsolicitud/editar/<s>')
@pms.vista.required.login_required
def edSolicitud(s=None):
    """
    Funcion que llama a la Vista de Editar Solicitud responde al boton de 'Editar' de Administrar Solicitud
    recibe el de la solicitud a editarse
    """
    #soli=getSolicitud(s)
    flask.session['solicitudid']=s
    iversiones=getVersionesItemParaSolicitud(flask.session['proyectoid'])
    """for i in i versiones:
        if i[0] in soli.items:
            i[1]=True
    acc=[]
    aux=[]
    aux.append("Editar")
    if soli.acciones%10==1:
        aux.append(True)
    else:
        aux.append(False)"""

    return flask.render_template('editarSolicitud.html')

@app.route('/admsolicitud/eliminar/<s>')
@pms.vista.required.login_required
def eSolicitud(s=None):
    """
    Funcion que despliega la Vista de Eliminar Solicitud, llama a eliminarSolicitud.html
    responde al boton de 'Eliminar' de Administrar Solicitud
    recibe el id de la Solicitud a eliminarce
    """
    flask.session.pop('aux1',None)
    flask.session.pop('aux2',None)
    soli=getPeticion(s)
    acc=[]
    acc.insert(0, ["Editar",soli.acciones%10==1])
    acc.insert(1,["Eliminar",soli.acciones%100>=10])
    acc.insert(2,["Crear Relacion",soli.acciones%1000==100])
    acc.insert(3,["Eliminar Relacion",soli.acciones%10000==1000])

    flask.session['solicitudid']=s
    return flask.render_template('eliminarSolicitud.html',s=soli, acciones=acc)

@app.route('/admsolicitud/consultar/<s>')
@pms.vista.required.login_required
def consultarSolicitud(s=None):
    """
    Funcion que despliega pagina de consulta de Solicitud llama a consultarSolicitud.html
    responde al boton de 'Consultar' de Administrar Solicitud
    recibe el id de la solicitud a consultar
    """
    #soli=getSolicitud(s)
    return flask.render_template('consultarSolicitud.html')

<|endoftext|>


<commit_before>from functools import partial
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from crispy_forms.bootstrap import FormActions
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy as _l
from multiupload.fields import MultiFileField

class HelperMixin(object):
    form_helper_cls = FormHelper

    def __init__(self, *args, **kwargs):
        super(HelperMixin, self).__init__(*args, **kwargs)
        self.helper = getattr(self, 'helper', self.form_helper_cls(self))

class SingleButtonMixin(HelperMixin):
    action_text = _l('Save')
    form_helper_cls = FormHelper

    def __init__(self, *args, **kwargs):
        super(SingleButtonMixin, self).__init__(*args, **kwargs)
        self.helper.layout.append(
            FormActions(
                Submit('action', self.action_text, css_class="btn-primary"),
            )
        )

class SaveButtonMixin(HelperMixin):
    def __init__(self, *args, **kwargs):
        super(SaveButtonMixin, self).__init__(*args, **kwargs)
        self.helper.layout.append(
            FormActions(
                Submit('save_changes', _('Update'), css_class="btn-primary"),
                Submit('cancel', _('Cancel')),
            )
        )

class FormHorizontalMixin(HelperMixin):
    def __init__(self, *args, **kwargs):
        super(FormHorizontalMixin, self).__init__(*args, **kwargs)
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-lg-3'
        self.helper.field_class = 'col-lg-9'

class FileMixin(forms.Form):  # TODO: Generalize
    files = MultiFileField(label=_("Attachments"))
    attachment_cls = None

    def save(self, commit=True, *args, **kwargs):
        obj = super(FileMixin, self).save(commit=False, *args, **kwargs)
        attachments = []
        for each in self.cleaned_data['files']:
            attachments.append(self.attachment_cls(file=each, letter=obj))
        self.attachment_cls.objects.bulk_create(attachments)
        return obj

class PartialMixin(object):
    @classmethod
    def partial(cls, *args, **kwargs):
        return partial(cls, *args, **kwargs)

class AuthorMixin(object):
    def save(self, commit=True, *args, **kwargs):
        obj = super(AuthorMixin, self).save(commit=False, *args, **kwargs)
        if obj.pk:  # update
            obj.modified_by = self.user
        else:  # new
            obj.created_by = self.user
        if commit:
            obj.save()
        return obj
<commit_msg>Fix django-multiupload requirements - close #108<commit_after>from functools import partial
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from crispy_forms.bootstrap import FormActions
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy as _l
from multiupload.fields import MultiFileField

class HelperMixin(object):
    form_helper_cls = FormHelper

    def __init__(self, *args, **kwargs):
        super(HelperMixin, self).__init__(*args, **kwargs)
        self.helper = getattr(self, 'helper', self.form_helper_cls(self))

class SingleButtonMixin(HelperMixin):
    action_text = _l('Save')
    form_helper_cls = FormHelper

    def __init__(self, *args, **kwargs):
        super(SingleButtonMixin, self).__init__(*args, **kwargs)
        self.helper.layout.append(
            FormActions(
                Submit('action', self.action_text, css_class="btn-primary"),
            )
        )

class SaveButtonMixin(HelperMixin):
    def __init__(self, *args, **kwargs):
        super(SaveButtonMixin, self).__init__(*args, **kwargs)
        self.helper.layout.append(
            FormActions(
                Submit('save_changes', _('Update'), css_class="btn-primary"),
                Submit('cancel', _('Cancel')),
            )
        )

class FormHorizontalMixin(HelperMixin):
    def __init__(self, *args, **kwargs):
        super(FormHorizontalMixin, self).__init__(*args, **kwargs)
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-lg-3'
        self.helper.field_class = 'col-lg-9'

class FileMixin(forms.Form):  # TODO: Generalize
    files = MultiFileField(label=_("Attachments"), required=False)
    attachment_cls = None

    def save(self, commit=True, *args, **kwargs):
        obj = super(FileMixin, self).save(commit=False, *args, **kwargs)
        attachments = []
        for each in self.cleaned_data['files']:
            attachments.append(self.attachment_cls(file=each, letter=obj))
        self.attachment_cls.objects.bulk_create(attachments)
        return obj

class PartialMixin(object):
    @classmethod
    def partial(cls, *args, **kwargs):
        return partial(cls, *args, **kwargs)

class AuthorMixin(object):
    def save(self, commit=True, *args, **kwargs):
        obj = super(AuthorMixin, self).save(commit=False, *args, **kwargs)
        if obj.pk:  # update
            obj.modified_by = self.user
        else:  # new
            obj.created_by = self.user
        if commit:
            obj.save()
        return obj
<|endoftext|>


<commit_before>#!/usr/bin/env python3

import geojson, csv, dateutil, datetime, time, os, zipfile, pytz, json, shutil, urllib, math, subprocess
from housepy import config, log, util, strings, emailer, net
from mongo import db

EMAILS = ",".join(config['geo_emails']) if config['geo_emails'] is not None else None

def main():
    log.info("beacon_sender...")
    if config['geo_emails'] is None or not len(config['geo_emails']):
        log.info("--> no emails")
        return
    text = []
    for satellite in config['satellites']:
        try:
            last_beacon = list(db.features.find({'properties.FeatureType': "beacon", 'properties.Satellite': {'$eq': satellite}}).sort('properties.t_utc', -1).limit(1))[0]
            datetime = last_beacon['properties']['DateTime']
            lon, lat = last_beacon['geometry']['coordinates']
            satellite = last_beacon['properties']['Satellite']
            google = "https://www.google.com/maps/@%s,%s,15z" % (latitude, longitude)
            text.append("%s\n%s\n%f,%f\n%s" % (satellite, datetime, lat, lon, google))
            log.info("--> last reported beacon (%s on %s) at: %f,%f" % (satellite, datetime, lat, lon))
        except Exception as e:
            log.error("Could not get update: %s" % log.exc(e))
    try:
        log.info("Emailing to %s..." % EMAILS)
        text = "\n\n".join(text)
        emailer.send(EMAILS, "Core beacon location", text)
    except Exception as e:
        log.error("Could not email: %s" % log.exc(e))

main()
<commit_msg>adding google link to emails<commit_after>#!/usr/bin/env python3

import geojson, csv, dateutil, datetime, time, os, zipfile, pytz, json, shutil, urllib, math, subprocess
from housepy import config, log, util, strings, emailer, net
from mongo import db

EMAILS = ",".join(config['geo_emails']) if config['geo_emails'] is not None else None

def main():
    log.info("beacon_sender...")
    if config['geo_emails'] is None or not len(config['geo_emails']):
        log.info("--> no emails")
        return
    text = []
    for satellite in config['satellites']:
        try:
            last_beacon = list(db.features.find({'properties.FeatureType': "beacon", 'properties.Satellite': {'$eq': satellite}}).sort('properties.t_utc', -1).limit(1))[0]
            datetime = last_beacon['properties']['DateTime']
            lon, lat = last_beacon['geometry']['coordinates']
            satellite = last_beacon['properties']['Satellite']
            google = "https://www.google.com/maps/@%s,%s,15z" % (lat, lon)
            text.append("%s\n%s\n%f,%f\n%s" % (satellite, datetime, lat, lon, google))
            log.info("--> last reported beacon (%s on %s) at: %f,%f" % (satellite, datetime, lat, lon))
        except Exception as e:
            log.error("Could not get update: %s" % log.exc(e))
    try:
        log.info("Emailing to %s..." % EMAILS)
        text = "\n\n".join(text)
        emailer.send(EMAILS, "OWP beacon report", text)
    except Exception as e:
        log.error("Could not email: %s" % log.exc(e))

main()
<|endoftext|>


<commit_before>import tensorflow as tf
import numpy as np
import pickle
import os
from hyper_parameters import *
import data_handler as dh

def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1, dtype=tf.float32)
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape, dtype=tf.float32)
    return tf.Variable(initial)

def conv5x5_grid(x, w):
    return tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME')  # Pad or fit? (same is pad, fit is valid)

def conv8x1_line(x, w):  # includes ranks, files, and diagonals
    return tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='VALID')

class NeuralNet:
    # TODO: Add close session function to release resources.
    def __init__(self, _weight_values=None, _load_file=None):
        """
            Initializes neural net. Generates session, placeholders, variables,
            and structure.
            Input:
                load_weights [Bool]:
                    If true, the neural net will load weights saved from a file
                    instead of initializing them from a normal distribution.
        """
        self.dir_path = os.path.dirname(__file__)

        # declare layer variables
        self.sess = None
        self.W_grid = None
        self.W_rank = None
        self.W_file = None
        self.W_diag = None
        self.b_grid = None
        self.b_rank = None
        self.b_file = None
        self.b_diag = None
        self.W_fc_1 = None
        self.b_fc_1 = None
        self.W_fc_2 = None
        self.b_fc_2 = None
        self.W_final = None
        self.b_final = None

        # declare other variables
        self.pred_value = None

        # Create tf session and variables
        self.sess = tf.InteractiveSession()
        self.initialize_tf_variables()

        # assign variable values if any - could possibly move this to initialize_tf_variables(), but it would take some restructuring
        self.set_session(_weight_values=_weight_values, _load_file=_load_file)

        # all weights + biases
        self.all_weights = [self.W_grid, self.W_rank, self.W_file, self.W_diag, self.W_fc_1, self.W_fc_2, self.W_final,
                            self.b_grid, self.b_rank, self.b_file, self.b_diag, self.b_fc_1, self.b_fc_2, self.b_final]

        # create placeholders
        self.data = tf.placeholder(tf.float32, shape=[None, 8, 8, NUM_CHANNELS])
        self.data_diags = tf.placeholder(tf.float32, shape=[None, 10, 8, NUM_CHANNELS])
        self.true_value = tf.placeholder(tf.float32, shape=[None])

        # create neural net structure
        self.neural_net()

    def set_session(self, _weight_values=None, _load_file=None):
        """
            Sets tensorflow session and initializes variables.
            Will prioritize setting variables weights with given weights over weights from a file.
            If neither are provided, a weights will be created with a normal distribution.
            Inputs:
                _weight_values[Dict]:
                    dictionary of weight values
                _load_file[string]:
                    file to load dictionary of weights from
        """
        self.sess = tf.get_default_session()
        if self.sess is None:
            # TODO S: Why not just tf.Session()? - from what I've read the only difference is that InteractiveSession sets itself as the default session
            # Although we could probably change it to just sess
            self.sess = tf.InteractiveSession()
            # Assign values to weights
            if _weight_values is not None:
                self.load_weight_values(_weight_values=_weight_values)
            elif _load_file is not None:
                self.load_weight_values(_filename=_load_file)

        return self.sess

    def close_session(self, final=False):
        weight_values = self.get_weight_values()
        self.sess.close()
        return weight_values

    def initialize_tf_variables(self):
        """
            Initializes all weight variables to normal distribution, and all
            bias variables to a constant.
        """
        print "Initializing variables from a normal distribution (this should only happen once at the very start)"
        self.W_grid = weight_variable([5, 5, NUM_CHANNELS, NUM_FEAT])
        self.W_rank = weight_variable([1, 8, NUM_CHANNELS, NUM_FEAT])
        self.W_file = weight_variable([8, 1, NUM_CHANNELS, NUM_FEAT])
        self.W_diag = weight_variable([1, 8, NUM_CHANNELS, NUM_FEAT])

        # biases
        self.b_grid = bias_variable([NUM_FEAT])
        self.b_rank = bias_variable([NUM_FEAT])
        self.b_file = bias_variable([NUM_FEAT])
        self.b_diag = bias_variable([NUM_FEAT])

        # fully connected layer 1, weights + biases
        self.W_fc_1 = weight_variable([90 * NUM_FEAT, NUM_HIDDEN])
        self.b_fc_1 = bias_variable([NUM_HIDDEN])

        # fully connected layer 2, weights + biases
        self.W_fc_2 = weight_variable([NUM_HIDDEN, NUM_HIDDEN])
        self.b_fc_2 = bias_variable([NUM_HIDDEN])

        # Output layer
        self.W_final = weight_variable([NUM_HIDDEN, 1])
        self.b_final = bias_variable([1])
        self.sess.run(tf.initialize_all_variables())

    def load_weight_values(self, _weight_values=None, _filename='weight_values.p'):
        """
            Sets all variables to values loaded from a file
            Input:
                filename[String]:
                    Name of the file to load weight values from
        """
        if _weight_values is None:
            pickle_path = self.dir_path + '/../pickles/' + _filename
            print "Loading weights values from %s" % pickle_path
            weight_values = pickle.load(open(pickle_path, 'rb'))
        else:
            print "Loading weights from provided weight value dictionary"
            weight_values = _weight_values

        self.W_grid.assign(weight_values['W_grid'])
        self.W_rank.assign(weight_values['W_rank'])
        self.W_file.assign(weight_values['W_file'])
        self.W_diag.assign(weight_values['W_diag'])
        self.W_fc_1.assign(weight_values['W_fc_1'])
        self.W_fc_2.assign(weight_values['W_fc_2'])
        self.W_final.assign(weight_values['W_final'])

        self.b_grid.assign(weight_values['b_grid'])
        self.b_rank.assign(weight_values['b_rank'])
        self.b_file.assign(weight_values['b_file'])
        self.b_diag.assign(weight_values['b_diag'])
        self.b_fc_1.assign(weight_values['b_fc_1'])
        self.b_fc_2.assign(weight_values['b_fc_2'])
        self.b_final.assign(weight_values['b_final'])
        self.sess.run(tf.initialize_all_variables())

    def save_weight_values(self, _filename='weight_values.p'):
        """
            Saves all variable values a pickle file
            Input:
                filename[String]:
                    Name of the file to save weight values to
        """

        pickle_path = self.dir_path + '/../pickles/' + _filename
        pickle.dump(self.get_weight_values(), open(pickle_path, 'wb'))

    def get_weight_values(self):
        """
            Returns values of weights as a dictionary
        """
        weight_values = dict()

        weight_values['W_grid'], weight_values['W_rank'], weight_values['W_file'], weight_values['W_diag'], \
            weight_values['W_fc_1'], weight_values['W_fc_2'], weight_values['W_final'], \
            weight_values['b_grid'], weight_values['b_rank'], weight_values['b_file'], weight_values['b_diag'], \
            weight_values['b_fc_1'], weight_values['b_fc_2'], weight_values['b_final'] = \
            self.sess.run([self.W_grid, self.W_rank, self.W_file, self.W_diag, self.W_fc_1, self.W_fc_2, self.W_final,
                           self.b_grid, self.b_rank, self.b_file, self.b_diag, self.b_fc_1, self.b_fc_2, self.b_final])

        return weight_values

    # TODO set as default graph
    def neural_net(self):
        """
            Structure of neural net.
            Sets member variable 'pred_value' to the tensor representing the
            output of neural net.
        """
        batch_size = tf.shape(self.data)[0]

        # outputs to convolutional layer
        a = conv5x5_grid(self.data, self.W_grid)
        # print tf.shape(self.b_grid).eval()
        b = tf.nn.relu(a + self.b_grid)
        o_grid = tf.nn.relu(conv5x5_grid(self.data, self.W_grid) + self.b_grid)
        o_rank = tf.nn.relu(conv8x1_line(self.data, self.W_rank) + self.b_rank)
        o_file = tf.nn.relu(conv8x1_line(self.data, self.W_file) + self.b_file)
        o_diag = tf.nn.relu(conv8x1_line(self.data_diags, self.W_diag) + self.b_diag)

        o_grid = tf.reshape(o_grid, [batch_size, 64 * NUM_FEAT])
        o_rank = tf.reshape(o_rank, [batch_size, 8 * NUM_FEAT])
        o_file = tf.reshape(o_file, [batch_size, 8 * NUM_FEAT])
        o_diag = tf.reshape(o_diag, [batch_size, 10 * NUM_FEAT])

        # output of convolutional layer
        o_conn = tf.concat(1, [o_grid, o_rank, o_file, o_diag])

        # output of fully connected layer 1
        o_fc_1 = tf.nn.relu(tf.matmul(o_conn, self.W_fc_1) + self.b_fc_1)

        # output of fully connected layer 2
        o_fc_2 = tf.nn.relu(tf.matmul(o_fc_1, self.W_fc_2) + self.b_fc_2)

        # final_output
        self.pred_value = tf.sigmoid(tf.matmul(o_fc_2, self.W_final) + self.b_final)

    def get_weights(self, weight_vars):
        """
        Get the weight values of the input.
            Input:
                weight_vars [List]
                    List of weights to get.
            Output:
                weights [List]
                    Weights & biases.
        """
        return self.sess.run(weight_vars)

    def update_weights(self, weight_vars, weight_vals):
        """
        Updates the neural net weights based on the input.
            Input:
                weight_vars [List]
                    List of weights to be updated
                weight_vals [List]
                    List of values with which to update weights. Must be in same order!
        """
        if len(weight_vars) != len(weight_vals):
            raise Exception("Number of values (%d) is not the same as the number of variables (%d)" %
                            (len(weight_vals), len(weight_vars)))

        # Create assignment for each weight
        num_weights = len(weight_vals)
        # S: can you not make this using list comprehesion?
        assignments = [None] * num_weights
        for i in range(num_weights):
            assignments[i] = weight_vars[i].assign(weight_vals[i])

        # Run assignment/update
        self.sess.run(assignments)
        # print ([str(x.eval()) for x in weight_vars])

    def get_gradient(self, fen, weights):
        """
        Returns the gradient of the neural net at the output node, with respect to the specified weights.
        board.
            Input:
                fen [String]
                    FEN of board where gradient is to be taken. Must be for white playing next.
                weights [List]
                    List of weight variables.
            Output:
                Gradient [List of floats].
        """

        if dh.black_is_next(fen):
            raise ValueError("Invalid gradient input, white must be next to play.")

        #  declare gradient of predicted (output) value w.r.t. weights + biases
        #  S: use self.all_weights. Set grad as a member variable since you're accessing it so much
        grad = tf.gradients(self.pred_value, weights)

        # calculate gradient
        return self.sess.run(grad, feed_dict=self.board_to_feed(fen))

    def board_to_feed(self, fen):
        """
        Converts the FEN of a SINGLE board to the required feed input for the neural net.
            Input:
                board [String]
                    FEN of board.
            Output:
                feed_dict [Dictionary]
                    Formatted input for neural net.
        """

        fen = fen.split()[0]
        board = dh.fen_to_channels(fen)
        diagonal = dh.get_diagonals(board)

        board = np.array([board])
        diagonal = np.array([diagonal])

        return {self.data: board, self.data_diags: diagonal}

    # TODO S: Maybe combine the following two functions? I think this only gets used in guerilla.py but i'm not sure.
    def evaluate(self, fen):
        """
        Evaluates chess board.
             Input:
                 fen [String]:
                     FEN of chess board.
             Output:
                 Score between 0 and 1. Represents probability of White (current player) winning.
        """

        if dh.black_is_next(fen):
            raise ValueError("Invalid evaluate input, white must be next to play.")

        return self.pred_value.eval(feed_dict=self.board_to_feed(fen), session=self.sess)[0][0]

    def evaluate_board(self, board):
        return self.evaluate(board.fen())
<commit_msg>fix bug where variable assignments were not being run<commit_after>import tensorflow as tf
import numpy as np
import pickle
import os
from hyper_parameters import *
import data_handler as dh

def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1, dtype=tf.float32)
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape, dtype=tf.float32)
    return tf.Variable(initial)

def conv5x5_grid(x, w):
    return tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME')  # Pad or fit? (same is pad, fit is valid)

def conv8x1_line(x, w):  # includes ranks, files, and diagonals
    return tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='VALID')

class NeuralNet:
    # TODO: Add close session function to release resources.
    def __init__(self, _weight_values=None, _load_file=None):
        """
            Initializes neural net. Generates session, placeholders, variables,
            and structure.
            Input:
                load_weights [Bool]:
                    If true, the neural net will load weights saved from a file
                    instead of initializing them from a normal distribution.
        """
        self.dir_path = os.path.dirname(__file__)

        # declare layer variables
        self.sess = None
        self.W_grid = None
        self.W_rank = None
        self.W_file = None
        self.W_diag = None
        self.b_grid = None
        self.b_rank = None
        self.b_file = None
        self.b_diag = None
        self.W_fc_1 = None
        self.b_fc_1 = None
        self.W_fc_2 = None
        self.b_fc_2 = None
        self.W_final = None
        self.b_final = None

        # declare other variables
        self.pred_value = None

        # Create tf session and variables
        self.sess = tf.InteractiveSession()
        self.initialize_tf_variables()

        # assign variable values if any - could possibly move this to initialize_tf_variables(), but it would take some restructuring
        self.set_session(_weight_values=_weight_values, _load_file=_load_file)

        # all weights + biases
        self.all_weights = [self.W_grid, self.W_rank, self.W_file, self.W_diag, self.W_fc_1, self.W_fc_2, self.W_final,
                            self.b_grid, self.b_rank, self.b_file, self.b_diag, self.b_fc_1, self.b_fc_2, self.b_final]

        # create placeholders
        self.data = tf.placeholder(tf.float32, shape=[None, 8, 8, NUM_CHANNELS])
        self.data_diags = tf.placeholder(tf.float32, shape=[None, 10, 8, NUM_CHANNELS])
        self.true_value = tf.placeholder(tf.float32, shape=[None])

        # create neural net structure
        self.neural_net()

    def set_session(self, _weight_values=None, _load_file=None):
        """
            Sets tensorflow session and initializes variables.
            Will prioritize setting variables weights with given weights over weights from a file.
            If neither are provided, a weights will be created with a normal distribution.
            Inputs:
                _weight_values[Dict]:
                    dictionary of weight values
                _load_file[string]:
                    file to load dictionary of weights from
        """
        self.sess = tf.get_default_session()
        if self.sess is None:
            # TODO S: Why not just tf.Session()? - from what I've read the only difference is that InteractiveSession sets itself as the default session
            # Although we could probably change it to just sess
            self.sess = tf.InteractiveSession()
            # Assign values to weights
            if _weight_values is not None:
                self.load_weight_values(_weight_values=_weight_values)
            elif _load_file is not None:
                self.load_weight_values(_filename=_load_file)

        return self.sess

    def close_session(self, final=False):
        weight_values = self.get_weight_values()
        self.sess.close()
        return weight_values

    def initialize_tf_variables(self):
        """
            Initializes all weight variables to normal distribution, and all
            bias variables to a constant.
        """
        print "Initializing variables from a normal distribution (this should only happen once at the very start)"
        self.W_grid = weight_variable([5, 5, NUM_CHANNELS, NUM_FEAT])
        self.W_rank = weight_variable([1, 8, NUM_CHANNELS, NUM_FEAT])
        self.W_file = weight_variable([8, 1, NUM_CHANNELS, NUM_FEAT])
        self.W_diag = weight_variable([1, 8, NUM_CHANNELS, NUM_FEAT])

        # biases
        self.b_grid = bias_variable([NUM_FEAT])
        self.b_rank = bias_variable([NUM_FEAT])
        self.b_file = bias_variable([NUM_FEAT])
        self.b_diag = bias_variable([NUM_FEAT])

        # fully connected layer 1, weights + biases
        self.W_fc_1 = weight_variable([90 * NUM_FEAT, NUM_HIDDEN])
        self.b_fc_1 = bias_variable([NUM_HIDDEN])

        # fully connected layer 2, weights + biases
        self.W_fc_2 = weight_variable([NUM_HIDDEN, NUM_HIDDEN])
        self.b_fc_2 = bias_variable([NUM_HIDDEN])

        # Output layer
        self.W_final = weight_variable([NUM_HIDDEN, 1])
        self.b_final = bias_variable([1])
        self.sess.run(tf.initialize_all_variables())

    def load_weight_values(self, _weight_values=None, _filename='weight_values.p'):
        """
            Sets all variables to values loaded from a file
            Input:
                filename[String]:
                    Name of the file to load weight values from
        """
        if _weight_values is None:
            pickle_path = self.dir_path + '/../pickles/' + _filename
            print "Loading weights values from %s" % pickle_path
            weight_values = pickle.load(open(pickle_path, 'rb'))
        else:
            print "Loading weights from provided weight value dictionary"
            weight_values = _weight_values

        assignments = []

        assignments.append(self.W_grid.assign(weight_values['W_grid']))
        assignments.append(self.W_rank.assign(weight_values['W_rank']))
        assignments.append(self.W_file.assign(weight_values['W_file']))
        assignments.append(self.W_diag.assign(weight_values['W_diag']))
        assignments.append(self.W_fc_1.assign(weight_values['W_fc_1']))
        assignments.append(self.W_fc_2.assign(weight_values['W_fc_2']))
        assignments.append(self.W_final.assign(weight_values['W_final']))

        assignments.append(self.b_grid.assign(weight_values['b_grid']))
        assignments.append(self.b_rank.assign(weight_values['b_rank']))
        assignments.append(self.b_file.assign(weight_values['b_file']))
        assignments.append(self.b_diag.assign(weight_values['b_diag']))
        assignments.append(self.b_fc_1.assign(weight_values['b_fc_1']))
        assignments.append(self.b_fc_2.assign(weight_values['b_fc_2']))
        assignments.append(self.b_final.assign(weight_values['b_final']))

        self.sess.run(tf.initialize_all_variables())
        self.sess.run(assignments)

    def save_weight_values(self, _filename='weight_values.p'):
        """
            Saves all variable values a pickle file
            Input:
                filename[String]:
                    Name of the file to save weight values to
        """

        pickle_path = self.dir_path + '/../pickles/' + _filename
        pickle.dump(self.get_weight_values(), open(pickle_path, 'wb'))

    def get_weight_values(self):
        """
            Returns values of weights as a dictionary
        """
        weight_values = dict()

        weight_values['W_grid'], weight_values['W_rank'], weight_values['W_file'], weight_values['W_diag'], \
            weight_values['W_fc_1'], weight_values['W_fc_2'], weight_values['W_final'], \
            weight_values['b_grid'], weight_values['b_rank'], weight_values['b_file'], weight_values['b_diag'], \
            weight_values['b_fc_1'], weight_values['b_fc_2'], weight_values['b_final'] = \
            self.sess.run([self.W_grid, self.W_rank, self.W_file, self.W_diag, self.W_fc_1, self.W_fc_2, self.W_final,
                           self.b_grid, self.b_rank, self.b_file, self.b_diag, self.b_fc_1, self.b_fc_2, self.b_final])

        return weight_values

    # TODO set as default graph
    def neural_net(self):
        """
            Structure of neural net.
            Sets member variable 'pred_value' to the tensor representing the
            output of neural net.
        """
        batch_size = tf.shape(self.data)[0]

        # outputs to convolutional layer
        a = conv5x5_grid(self.data, self.W_grid)
        # print tf.shape(self.b_grid).eval()
        b = tf.nn.relu(a + self.b_grid)
        o_grid = tf.nn.relu(conv5x5_grid(self.data, self.W_grid) + self.b_grid)
        o_rank = tf.nn.relu(conv8x1_line(self.data, self.W_rank) + self.b_rank)
        o_file = tf.nn.relu(conv8x1_line(self.data, self.W_file) + self.b_file)
        o_diag = tf.nn.relu(conv8x1_line(self.data_diags, self.W_diag) + self.b_diag)

        o_grid = tf.reshape(o_grid, [batch_size, 64 * NUM_FEAT])
        o_rank = tf.reshape(o_rank, [batch_size, 8 * NUM_FEAT])
        o_file = tf.reshape(o_file, [batch_size, 8 * NUM_FEAT])
        o_diag = tf.reshape(o_diag, [batch_size, 10 * NUM_FEAT])

        # output of convolutional layer
        o_conn = tf.concat(1, [o_grid, o_rank, o_file, o_diag])

        # output of fully connected layer 1
        o_fc_1 = tf.nn.relu(tf.matmul(o_conn, self.W_fc_1) + self.b_fc_1)

        # output of fully connected layer 2
        o_fc_2 = tf.nn.relu(tf.matmul(o_fc_1, self.W_fc_2) + self.b_fc_2)

        # final_output
        self.pred_value = tf.sigmoid(tf.matmul(o_fc_2, self.W_final) + self.b_final)

    def get_weights(self, weight_vars):
        """
        Get the weight values of the input.
            Input:
                weight_vars [List]
                    List of weights to get.
            Output:
                weights [List]
                    Weights & biases.
        """
        return self.sess.run(weight_vars)

    def update_weights(self, weight_vars, weight_vals):
        """
        Updates the neural net weights based on the input.
            Input:
                weight_vars [List]
                    List of weights to be updated
                weight_vals [List]
                    List of values with which to update weights. Must be in same order!
        """
        if len(weight_vars) != len(weight_vals):
            raise Exception("Number of values (%d) is not the same as the number of variables (%d)" %
                            (len(weight_vals), len(weight_vars)))

        # Create assignment for each weight
        num_weights = len(weight_vals)
        # S: can you not make this using list comprehesion?
        assignments = [None] * num_weights
        for i in range(num_weights):
            assignments[i] = weight_vars[i].assign(weight_vals[i])

        # Run assignment/update
        self.sess.run(assignments)
        # print ([str(x.eval()) for x in weight_vars])

    def get_gradient(self, fen, weights):
        """
        Returns the gradient of the neural net at the output node, with respect to the specified weights.
        board.
            Input:
                fen [String]
                    FEN of board where gradient is to be taken. Must be for white playing next.
                weights [List]
                    List of weight variables.
            Output:
                Gradient [List of floats].
        """

        if dh.black_is_next(fen):
            raise ValueError("Invalid gradient input, white must be next to play.")

        #  declare gradient of predicted (output) value w.r.t. weights + biases
        #  S: use self.all_weights. Set grad as a member variable since you're accessing it so much
        grad = tf.gradients(self.pred_value, weights)

        # calculate gradient
        return self.sess.run(grad, feed_dict=self.board_to_feed(fen))

    def board_to_feed(self, fen):
        """
        Converts the FEN of a SINGLE board to the required feed input for the neural net.
            Input:
                board [String]
                    FEN of board.
            Output:
                feed_dict [Dictionary]
                    Formatted input for neural net.
        """

        fen = fen.split()[0]
        board = dh.fen_to_channels(fen)
        diagonal = dh.get_diagonals(board)

        board = np.array([board])
        diagonal = np.array([diagonal])

        return {self.data: board, self.data_diags: diagonal}

    # TODO S: Maybe combine the following two functions? I think this only gets used in guerilla.py but i'm not sure.
    def evaluate(self, fen):
        """
        Evaluates chess board.
             Input:
                 fen [String]:
                     FEN of chess board.
             Output:
                 Score between 0 and 1. Represents probability of White (current player) winning.
        """

        if dh.black_is_next(fen):
            raise ValueError("Invalid evaluate input, white must be next to play.")

        return self.pred_value.eval(feed_dict=self.board_to_feed(fen), session=self.sess)[0][0]

    def evaluate_board(self, board):
        return self.evaluate(board.fen())
<|endoftext|>


<commit_before>import re

def getSwaggerPaginationDef(resultsPerPage):
    """Build swagger spec section for pagination"""
    return {
        "name":  "page",
        "type":  "int",
        "in":  "query",
        "description":  "The page number for this paginated query ({} results per page)".format(resultsPerPage)
    }

def buildPaginationHeader(resultCount, resultsPerPage, pageArg, url):
    """Build link header for result pagination"""
    lastPage = resultCount / resultsPerPage

    if pageArg:
        page = int(pageArg)
        next_url = re.sub("page=[0-9]+", "page={}".format(page + 1), url)
        prev_url = re.sub("page=[0-9]+", "page={}".format(page - 1), url)
        first_url = re.sub("page=[0-9]+", "page=1", url)
        last_url = re.sub("page=[0-9]+", "page={}".format(lastPage), url)
    else:
        page = 1
        next_url = url + "?page=2"
        prev_url = ""
        first_url = url + "?page=1"
        last_url = url + "?page={}".format(lastPage)

    if page == 1:
        headerLink = "<{}>; rel=next, <{}>; rel=last".format(next_url, last_url)
    elif page == lastPage:
        headerLink = "<{}>; rel=prev, <{}>; rel=first".format(prev_url, first_url)
    else:
        headerLink = "<{}>; rel=next, <{}>; rel=prev, <{}>; rel=first, <{}>; rel=last".format(next_url, prev_url, first_url, last_url)
    return headerLink
<commit_msg>Pagination links -- fixes #259<commit_after>from urllib.parse import urlparse, urlunparse, parse_qsl, urlencode, ParseResult

def getSwaggerPaginationDef(resultsPerPage):
    """Build swagger spec section for pagination"""
    return {
        "name":  "page",
        "type":  "int",
        "in":  "query",
        "description":  "The page number for this paginated query ({} results per page)".format(resultsPerPage)
    }

def buildPaginationHeader(resultCount, resultsPerPage, pageArg, url):
    """Build link header for result pagination"""
    lastPage = resultCount / resultsPerPage

    url_parts = urlparse(url)
    query = dict(parse_qsl(url_parts.query)) # Use dict parse_qsl instead of parse_qs to ensure 'page' is unique

    first_url = _buildNewUrlWithPage(url_parts, query, page=1)
    last_url = _buildNewUrlWithPage(url_parts, query, page=lastPage)

    if not pageArg:
        next_url = _buildNewUrlWithPage(url_parts, query, page=1)
        prev_url = ""
        headerLink = "<{}>; rel=next, <{}>; rel=last".format(next_url, last_url)
    else:
        page = int(pageArg)
        next_url = _buildNewUrlWithPage(url_parts, query, page + 1)
        prev_url = _buildNewUrlWithPage(url_parts, query, page - 1)

        if page == lastPage:
            headerLink = "<{}>; rel=prev, <{}>; rel=first".format(prev_url, first_url)
        else:
            headerLink = "<{}>; rel=next, <{}>; rel=prev, <{}>; rel=first, <{}>; rel=last".format(next_url, prev_url, first_url, last_url)
    return headerLink

def _buildNewUrlWithPage(url_parts, query, page):
    query['page'] = page
    new_query = urlencode(query)
    newParsedUrl = ParseResult(scheme=url_parts.scheme, netloc=url_parts.netloc, path=url_parts.path,
                               params=url_parts.params, query=new_query, fragment=url_parts.fragment)
    return urlunparse(newParsedUrl)
<|endoftext|>


<commit_before>from math import pi, sin, cos
import sys
import os

from direct.showbase.ShowBase import ShowBase
from direct.task import Task
from direct.actor.Actor import Actor
from direct.interval.IntervalGlobal import Sequence
from panda3d.core import Point3, Mat4, Filename, NodePath

def runPandaTest():
    app = MyApp()
    app.run()

class MyApp(ShowBase):

    def __init__(self):
        ShowBase.__init__(self)

        self.scene = self.loader.loadModel("environment")
        self.scene.reparentTo(self.render)
        self.scene.setScale(0.25, 0.25, 0.25)
        self.scene.setPos(-8, 42, 0)

        # Add a test model of our own creation, to check that we can use our
        # own models.
        self.testModel = self.loader.loadModel(getModelPath("test-model.egg"))
        self.testModel.reparentTo(self.render)
        self.testModel.setPos(-4, 0, 2.5)

        self.pandaActor = Actor("panda-model",
                                {"walk": "panda-walk4"})
        self.pandaActor.setScale(0.005, 0.005, 0.005)
        self.pandaActor.reparentTo(self.render)
        self.pandaActor.loop("walk")

        pandaPosInterval1 = self.pandaActor.posInterval(13,
                                                        Point3(0, -10, 0),
                                                        startPos=Point3(0, 10, 0))
        pandaPosInterval2 = self.pandaActor.posInterval(13,
                                                        Point3(0, 10, 0),
                                                        startPos=Point3(0, -10, 0))
        pandaHprInterval1 = self.pandaActor.hprInterval(3,
                                                        Point3(180, 0, 0),
                                                        startHpr=Point3(0, 0, 0))
        pandaHprInterval2 = self.pandaActor.hprInterval(3,
                                                        Point3(0, 0, 0),
                                                        startHpr=Point3(180, 0, 0))

        self.pandaPace = Sequence(pandaPosInterval1,
                                  pandaHprInterval1,
                                  pandaPosInterval2,
                                  pandaHprInterval2,
                                  name="pandaPace")
        self.pandaPace.loop()

        self.keys = {}
        self.setupEventHandlers()

        # Initialize camera control
        self.cameraHolder = self.render.attachNewNode('CameraHolder')
        self.cameraHolder.setPos(0, 0, 100)
        self.prevCameraHpr = (0, -80, 0)
        self.setCameraCustom()

    def setCameraCustom(self):
        """
        Change to using our custom task to control the camera.
        """

        # Disable the default mouse-based camera control task, so we don't have
        # to fight with it for control of the camera.
        self.disableMouse()

        # Face the camera in the appropriate angle.
        self.camera.setHpr(self.prevCameraHpr)

        # Put it in the same location as the cameraHolder, and make it stay
        # put relative to the cameraHolder (so we can move the camera around by
        # changing the cameraHolder's position).
        self.camera.reparentTo(self.cameraHolder)
        self.camera.setPos(0, 0, 0)

        # Substitute our own camera control task.
        self.taskMgr.add(self.updateCameraTask, "UpdateCameraTask")

        self.usingCustomCamera = True

    def setCameraDefault(self):
        """
        Change to using the default mouse-based camera controls.
        """

        self.taskMgr.remove("UpdateCameraTask")

        # Save current location for when this control style is restored.
        self.prevCameraPos = self.camera.getPos()
        self.prevCameraHpr = self.camera.getHpr()

        # Use the existing camera location, rather than jumping back to the one
        # from last time the default camera controller was active.
        # Copied from https://www.panda3d.org/manual/index.php/Mouse_Support
        mat = Mat4(camera.getMat())
        mat.invertInPlace()
        self.mouseInterfaceNode.setMat(mat)
        self.enableMouse()

        self.usingCustomCamera = False

    def toggleCameraStyle(self):
        """
        Switch to whichever style of camera control isn't currently active.
        """

        if self.usingCustomCamera:
            self.setCameraDefault()
        else:
            self.setCameraCustom()

    def spinCameraTask(self, task):
        """
        Spin the camera in a circle.
        """

        angleDegrees = task.time * 6.0
        angleRadians = angleDegrees * (pi / 180.0)
        self.camera.setPos(20 * sin(angleRadians), -20 * cos(angleRadians), 3)
        self.camera.setHpr(angleDegrees, 0, 0)
        return Task.cont

    def updateCameraTask(self, task):
        """
        Move the camera sensibly.
        """

        dt = globalClock.getDt()
        translateSpeed = 30 * dt
        rotateSpeed = 50 * dt

        forward = translateSpeed * (self.keys["arrow_up"] - self.keys["arrow_down"])
        sideways = translateSpeed * (self.keys["arrow_right"] - self.keys["arrow_left"])
        # Check if the mouse is over the window.
        if base.mouseWatcherNode.hasMouse():
            # Get the position. Each coordinate is normalized to the interval [-1, 1].
            mousePos = base.mouseWatcherNode.getMouse()
            xPos, yPos = mousePos.getX(), mousePos.getY()
            # Only move if the mouse is close to the edge.
            if abs(xPos) > 0.4:
                sideways += 2 * translateSpeed * xPos
            if abs(yPos) > 0.4:
                forward += 2 * translateSpeed * yPos
        self.cameraHolder.setPos(self.cameraHolder, sideways, forward, 0)

        rotate = rotateSpeed * (self.keys["a"] - self.keys["d"])
        self.cameraHolder.setHpr(self.cameraHolder, rotate, 0, 0)

        return Task.cont

    def zoomCamera(self, inward):
        """
        Zoom in or out.
        """

        dt = globalClock.getDt()
        zoomSpeed = 100 * dt

        zoom = -zoomSpeed if inward else zoomSpeed
        self.cameraHolder.setPos(self.cameraHolder, 0, 0, zoom)

    def setupEventHandlers(self):
        def pushKey(key, value):
            self.keys[key] = value

        for key in ["arrow_up", "arrow_left", "arrow_right", "arrow_down",
                    "w", "a", "d", "s"]:
            self.keys[key] = False
            self.accept(key, pushKey, [key, True])
            self.accept("shift-%s" % key, pushKey, [key, True])
            self.accept("%s-up" % key, pushKey, [key, False])

        # Camera toggle
        self.accept("f3",       self.toggleCameraStyle, [])
        self.accept("shift-f3", self.toggleCameraStyle, [])

        # Handle mouse wheel
        self.accept("wheel_up", self.zoomCamera, [True])
        self.accept("wheel_down", self.zoomCamera, [False])

def getModelPath(modelname):
    """
    Return the Panda3D path for a model.
    """

    # https://www.panda3d.org/manual/index.php/Loading_Models
    mydir = os.path.abspath(sys.path[0])
    mydir = Filename.fromOsSpecific(mydir).getFullpath()

    # Can't use os.path.join here because mydir isn't an OS-specific path.
    if not mydir.endswith("/"):
        mydir += "/"
    return mydir + "assets/models/" + modelname
<commit_msg>Detect mouse clicks; print world coordinates.<commit_after>from math import pi, sin, cos
import sys
import os

from direct.showbase.ShowBase import ShowBase
from direct.task import Task
from direct.actor.Actor import Actor
from direct.interval.IntervalGlobal import Sequence

import panda3d.core as core
# Backward compatibility; please switch to using core.*
Point3   = core.Point3
Mat4     = core.Mat4
Filename = core.Filename
NodePath = core.NodePath

def runPandaTest():
    app = MyApp()
    app.run()

class MyApp(ShowBase):

    def __init__(self):
        ShowBase.__init__(self)

        self.scene = self.loader.loadModel("environment")
        self.scene.reparentTo(self.render)
        self.scene.setScale(0.25, 0.25, 0.25)
        self.scene.setPos(-8, 42, 0)

        # Used for collision checking.
        groundCollisionPlane = core.CollisionPlane(core.LPlanef(
            core.Vec3(0, 0, 1), core.Point3(0, 0, 0)))

        # I don't actually know what sort of object this is...
        self.groundPlaneNP = self.render.attachNewNode(
            core.CollisionNode("groundCollisionNode"))
        self.groundPlaneNP.node().addSolid(groundCollisionPlane)

        # For detecting clicks
        mouseClickNode = core.CollisionNode("mouseRay")
        self.mouseClickNP = self.camera.attachNewNode(mouseClickNode)
        # TODO: Do we need to mouseClickNode.setFromCollideMask() here?
        self.mouseClickRay = core.CollisionRay()
        mouseClickNode.addSolid(self.mouseClickRay)

        # Bunch of objects that I don't entirely understand
        self.mouseClickHandler = core.CollisionHandlerQueue()
        self.mouseClickTraverser = core.CollisionTraverser("mouse click")
        self.mouseClickTraverser.addCollider(self.mouseClickNP,
            self.mouseClickHandler)

        # Add a test model of our own creation, to check that we can use our
        # own models.
        self.testModel = self.loader.loadModel(getModelPath("test-model.egg"))
        self.testModel.reparentTo(self.render)
        self.testModel.setPos(-4, 0, 2.5)

        self.pandaActor = Actor("panda-model",
                                {"walk": "panda-walk4"})
        self.pandaActor.setScale(0.005, 0.005, 0.005)
        self.pandaActor.reparentTo(self.render)
        self.pandaActor.loop("walk")

        pandaPosInterval1 = self.pandaActor.posInterval(13,
                                                        Point3(0, -10, 0),
                                                        startPos=Point3(0, 10, 0))
        pandaPosInterval2 = self.pandaActor.posInterval(13,
                                                        Point3(0, 10, 0),
                                                        startPos=Point3(0, -10, 0))
        pandaHprInterval1 = self.pandaActor.hprInterval(3,
                                                        Point3(180, 0, 0),
                                                        startHpr=Point3(0, 0, 0))
        pandaHprInterval2 = self.pandaActor.hprInterval(3,
                                                        Point3(0, 0, 0),
                                                        startHpr=Point3(180, 0, 0))

        self.pandaPace = Sequence(pandaPosInterval1,
                                  pandaHprInterval1,
                                  pandaPosInterval2,
                                  pandaHprInterval2,
                                  name="pandaPace")
        self.pandaPace.loop()

        self.keys = {}
        self.setupEventHandlers()

        # Initialize camera control
        self.cameraHolder = self.render.attachNewNode('CameraHolder')
        self.cameraHolder.setPos(0, 0, 100)
        self.prevCameraHpr = (0, -80, 0)
        self.setCameraCustom()

    def setCameraCustom(self):
        """
        Change to using our custom task to control the camera.
        """

        # Disable the default mouse-based camera control task, so we don't have
        # to fight with it for control of the camera.
        self.disableMouse()

        # Face the camera in the appropriate angle.
        self.camera.setHpr(self.prevCameraHpr)

        # Put it in the same location as the cameraHolder, and make it stay
        # put relative to the cameraHolder (so we can move the camera around by
        # changing the cameraHolder's position).
        self.camera.reparentTo(self.cameraHolder)
        self.camera.setPos(0, 0, 0)

        # Substitute our own camera control task.
        self.taskMgr.add(self.updateCameraTask, "UpdateCameraTask")

        self.usingCustomCamera = True

    def setCameraDefault(self):
        """
        Change to using the default mouse-based camera controls.
        """

        self.taskMgr.remove("UpdateCameraTask")

        # Save current location for when this control style is restored.
        self.prevCameraPos = self.camera.getPos()
        self.prevCameraHpr = self.camera.getHpr()

        # Use the existing camera location, rather than jumping back to the one
        # from last time the default camera controller was active.
        # Copied from https://www.panda3d.org/manual/index.php/Mouse_Support
        mat = Mat4(camera.getMat())
        mat.invertInPlace()
        self.mouseInterfaceNode.setMat(mat)
        self.enableMouse()

        self.usingCustomCamera = False

    def toggleCameraStyle(self):
        """
        Switch to whichever style of camera control isn't currently active.
        """

        if self.usingCustomCamera:
            self.setCameraDefault()
        else:
            self.setCameraCustom()

    def spinCameraTask(self, task):
        """
        Spin the camera in a circle.
        """

        angleDegrees = task.time * 6.0
        angleRadians = angleDegrees * (pi / 180.0)
        self.camera.setPos(20 * sin(angleRadians), -20 * cos(angleRadians), 3)
        self.camera.setHpr(angleDegrees, 0, 0)
        return Task.cont

    def updateCameraTask(self, task):
        """
        Move the camera sensibly.
        """

        dt = globalClock.getDt()
        translateSpeed = 30 * dt
        rotateSpeed = 50 * dt

        forward = translateSpeed * (self.keys["arrow_up"] - self.keys["arrow_down"])
        sideways = translateSpeed * (self.keys["arrow_right"] - self.keys["arrow_left"])
        # Check if the mouse is over the window.
        if base.mouseWatcherNode.hasMouse():
            # Get the position. Each coordinate is normalized to the interval [-1, 1].
            mousePos = base.mouseWatcherNode.getMouse()
            xPos, yPos = mousePos.getX(), mousePos.getY()
            # Only move if the mouse is close to the edge.
            if abs(xPos) > 0.4:
                sideways += 2 * translateSpeed * xPos
            if abs(yPos) > 0.4:
                forward += 2 * translateSpeed * yPos
        self.cameraHolder.setPos(self.cameraHolder, sideways, forward, 0)

        rotate = rotateSpeed * (self.keys["a"] - self.keys["d"])
        self.cameraHolder.setHpr(self.cameraHolder, rotate, 0, 0)

        return Task.cont

    def zoomCamera(self, inward):
        """
        Zoom in or out.
        """

        dt = globalClock.getDt()
        zoomSpeed = 100 * dt

        zoom = -zoomSpeed if inward else zoomSpeed
        self.cameraHolder.setPos(self.cameraHolder, 0, 0, zoom)

    def handleMouseClick(self):
        # Make sure the mouse is inside the screen
        if self.mouseWatcherNode.hasMouse():
            # Get the screen coordinates of the mouse, normalized to [-1, 1].
            mousePos = self.mouseWatcherNode.getMouse()

            # Make the ray extend from the camera, in the direction of the
            # mouse.
            self.mouseClickRay.setFromLens(self.camNode, mousePos)

            self.mouseClickTraverser.traverse(self.render)
            for entry in self.mouseClickHandler.getEntries():
                if entry.hasInto():
                    if entry.getIntoNodePath() == self.groundPlaneNP:
                        print "You clicked the ground at {}.".format(
                            entry.getSurfacePoint(self.render))
                        # TODO: Move monolith

    def setupEventHandlers(self):
        def pushKey(key, value):
            self.keys[key] = value

        for key in ["arrow_up", "arrow_left", "arrow_right", "arrow_down",
                    "w", "a", "d", "s"]:
            self.keys[key] = False
            self.accept(key, pushKey, [key, True])
            self.accept("shift-%s" % key, pushKey, [key, True])
            self.accept("%s-up" % key, pushKey, [key, False])

        # Camera toggle
        self.accept("f3",       self.toggleCameraStyle, [])
        self.accept("shift-f3", self.toggleCameraStyle, [])

        # Handle mouse wheel
        self.accept("wheel_up", self.zoomCamera, [True])
        self.accept("wheel_down", self.zoomCamera, [False])

        # Handle clicking
        self.accept("mouse1", self.handleMouseClick, [])

def getModelPath(modelname):
    """
    Return the Panda3D path for a model.
    """

    # https://www.panda3d.org/manual/index.php/Loading_Models
    mydir = os.path.abspath(sys.path[0])
    mydir = Filename.fromOsSpecific(mydir).getFullpath()

    # Can't use os.path.join here because mydir isn't an OS-specific path.
    if not mydir.endswith("/"):
        mydir += "/"
    return mydir + "assets/models/" + modelname
<|endoftext|>


<commit_before>"""(c) All rights reserved. ECOLE POLYTECHNIQUE FEDERALE DE LAUSANNE, Switzerland, VPSI, 2017"""
from utils import Utils

class Box:
    """A Jahia Box. Can be of type text, infoscience, etc."""

    # WP box types
    TYPE_TEXT = "text"
    TYPE_COLORED_TEXT = "coloredText"
    TYPE_INFOSCIENCE = "infoscience"
    TYPE_ACTU = "actu"
    TYPE_MEMENTO = "memento"
    TYPE_FAQ = "faq"
    TYPE_TOGGLE = "toggle"
    TYPE_INCLUDE = "include"
    TYPE_CONTACT = "contact"
    TYPE_XML = "xml"
    TYPE_LINKS = "links"
    TYPE_RSS = "rss"
    TYPE_FILES = "files"

    # Mapping of known box types from Jahia to WP
    types = {
        "epfl:textBox": TYPE_TEXT,
        "epfl:coloredTextBox": TYPE_COLORED_TEXT,
        "epfl:infoscienceBox": TYPE_INFOSCIENCE,
        "epfl:actuBox": TYPE_ACTU,
        "epfl:mementoBox": TYPE_MEMENTO,
        "epfl:faqContainer": TYPE_FAQ,
        "epfl:toggleBox": TYPE_TOGGLE,
        "epfl:htmlBox": TYPE_INCLUDE,
        "epfl:contactBox": TYPE_CONTACT,
        "epfl:xmlBox": TYPE_XML,
        "epfl:linksBox": TYPE_LINKS,
        "epfl:rssBox": TYPE_RSS,
        "epfl:filesBox": TYPE_FILES
    }

    def __init__(self, site, page_content, element, multibox=False):
        self.site = site
        self.page_content = page_content
        self.type = ""
        self.set_type(element)
        self.title = Utils.get_tag_attribute(element, "boxTitle", "jahia:value")
        self.content = ""
        self.set_content(element, multibox)

    def set_type(self, element):
        """
        Sets the box type
        """

        type = element.getAttribute("jcr:primaryType")

        if type in self.types:
            self.type = self.types[type]
        else:
            self.type = type

    def set_content(self, element, multibox=False):
        """set the box attributes"""

        # text
        if self.TYPE_TEXT == self.type or self.TYPE_COLORED_TEXT == self.type:
            self.set_box_text(element, multibox)
        # infoscience
        elif self.TYPE_INFOSCIENCE == self.type:
            self.set_box_infoscience(element)
        # actu
        elif self.TYPE_ACTU == self.type:
            self.set_box_actu(element)
        # memento
        elif self.TYPE_MEMENTO == self.type:
            self.set_box_memento(element)
        # faq
        elif self.TYPE_FAQ == self.type:
            self.set_box_faq(element)
        # toggle
        elif self.TYPE_TOGGLE == self.type:
            self.set_box_toggle(element)
        # include
        elif self.TYPE_INCLUDE == self.type:
            self.set_box_include(element)
        # contact
        elif self.TYPE_CONTACT == self.type:
            self.set_box_contact(element)
        # xml
        elif self.TYPE_XML == self.type:
            self.set_box_xml(element)
        # links
        elif self.TYPE_LINKS == self.type:
            self.set_box_links(element)
        # rss
        elif self.TYPE_RSS == self.type:
            self.set_box_rss(element)
        # files
        elif self.TYPE_FILES == self.type:
            self.set_box_files(element)
        # unknown
        else:
            self.set_box_unknown(element)

    def set_box_text(self, element, multibox=False):
        """set the attributes of a text box
            A text box can have two forms, either it contains just a <text> tag
            or it contains a <comboListList> which contains <comboList> tags which
            contain <text>, <filesList>, <linksList> tags. The last two tags may miss from time
            to time because the jahia export is not perfect.
            FIXME: For now <filesList> are ignored because we did not find a site where
            it is used yet.
        """

        if not multibox:
            content = Utils.get_tag_attribute(element, "text", "jahia:value")
            linksList = element.getElementsByTagName("linksList")
            if linksList:
                content += self._parse_links_to_list(linksList[0])
        else:
            # Concatenate HTML content of many boxes
            content = ""
            comboLists = element.getElementsByTagName("comboList")
            for element in comboLists:
                content += Utils.get_tag_attribute(element, "text", "jahia:value")
                # linksList contain <link> tags exactly like linksBox, so we can just reuse
                # the same code used to parse linksBox.
                content += self._parse_links_to_list(element)

        self.content = content

    def set_box_actu(self, element):
        """set the attributes of an actu box"""
        url = Utils.get_tag_attribute(element, "url", "jahia:value")

        self.content = "[actu url=%s]" % url

    def set_box_memento(self, element):
        """set the attributes of a memento box"""
        url = Utils.get_tag_attribute(element, "url", "jahia:value")

        self.content = "[memento url=%s]" % url

    def set_box_infoscience(self, element):
        """set the attributes of a infoscience box"""
        url = Utils.get_tag_attribute(element, "url", "jahia:value")

        self.content = "[epfl_infoscience url=%s]" % url

    def set_box_faq(self, element):
        """set the attributes of a faq box"""
        self.question = Utils.get_tag_attribute(element, "question", "jahia:value")

        self.answer = Utils.get_tag_attribute(element, "answer", "jahia:value")

        self.content = "
%s

%s
" % (self.question, self.answer)

    def set_box_toggle(self, element):
        """set the attributes of a toggle box"""
        self.opened = Utils.get_tag_attribute(element, "opened", "jahia:value")

        self.content = Utils.get_tag_attribute(element, "content", "jahia:value")

    def set_box_include(self, element):
        """set the attributes of an include box"""
        url = Utils.get_tag_attribute(element, "url", "jahia:value")

        self.content = "[include url=%s]" % url

    def set_box_contact(self, element):
        """set the attributes of a contact box"""
        text = Utils.get_tag_attribute(element, "text", "jahia:value")

        self.content = text

    def set_box_xml(self, element):
        """set the attributes of a xml box"""
        xml = Utils.get_tag_attribute(element, "xml", "jahia:value")
        xslt = Utils.get_tag_attribute(element, "xslt", "jahia:value")

        self.content = "[xml xml=%s xslt=%s]" % (xml, xslt)

    def set_box_rss(self, element):
        """set the attributes of an rss box"""

        # Jahia options
        url = Utils.get_tag_attribute(element, "url", "jahia:value")
        nb_items = Utils.get_tag_attribute(element, "nbItems", "jahia:value")
        hide_title = Utils.get_tag_attribute(element, "hideTitle", "jahia:value")
        detail_items = Utils.get_tag_attribute(element, "detailItems", "jahia:value")

        # check if we have at least an url
        if not url:
            return

        # some values are in JSP tag, with use a default value instead
        if not nb_items.isdigit():
            nb_items = "5"

        # feedzy-rss options
        feeds = url
        max = nb_items
        feed_title = "yes"
        summary = "yes"

        if hide_title == "true":
            feed_title = "no"

        if detail_items != "true":
            summary = "no"

        self.content = "[feedzy-rss feeds=\"{}\" max=\"{}\" feed_title=\"{}\" summary=\"{}\" refresh=\"12_hours\"]"\
            .format(feeds, max, feed_title, summary)

    def set_box_links(self, element):
        """set the attributes of a links box"""
        self.content = self._parse_links_to_list(element)

    def set_box_unknown(self, element):
        """set the attributes of an unknown box"""
        self.content = "[%s]" % element.getAttribute("jcr:primaryType")

    def set_box_files(self, element):
        """set the attributes of a files box"""
        elements = element.getElementsByTagName("file")
        content = "
"
        for e in elements:
            if e.ELEMENT_NODE != e.nodeType:
                continue
            # URL is like /content/sites/<site_name>/files/file
            # splitted gives ['', content, sites, <site_name>, files, file]
            # result of join is files/file and we add the missing '/' in front.
            file_url = '/'.join(e.getAttribute("jahia:value").split("/")[4:])
            file_url = '/' + file_url
            file_name = file_url.split("/")[-1]
            content += '
{}
'.format(file_url, file_name)
        content += "
"
        self.content = content

    def _parse_links_to_list(self, element):
        """Handles link tags that can be found in linksBox and textBox"""
        elements = element.getElementsByTagName("link")
        content = "
"
        for e in elements:
            if e.ELEMENT_NODE != e.nodeType:
                continue
            for jahia_tag in e.childNodes:
                if jahia_tag.ELEMENT_NODE != jahia_tag.nodeType:
                    continue
                if jahia_tag.tagName == "jahia:link":
                    # It happens that a link references a page that does not exist anymore
                    # observed on site dii
                    try:
                        page = self.site.pages_by_uuid[jahia_tag.getAttribute("jahia:reference")]
                    except KeyError as e:
                        continue
                    content += '
{}
'.format(page.pid, jahia_tag.getAttribute("jahia:title"))
                elif jahia_tag.tagName == "jahia:url":
                    url = jahia_tag.getAttribute("jahia:value")
                    title = jahia_tag.getAttribute("jahia:title")
                    content += '
{}
'.format(url, title)
        content += "
"

        if content == "

":
            return ""

        return content

    def __str__(self):
        return self.type + " " + self.title
<commit_msg>Added support for snippets<commit_after>"""(c) All rights reserved. ECOLE POLYTECHNIQUE FEDERALE DE LAUSANNE, Switzerland, VPSI, 2017"""
from utils import Utils

class Box:
    """A Jahia Box. Can be of type text, infoscience, etc."""

    # WP box types
    TYPE_TEXT = "text"
    TYPE_COLORED_TEXT = "coloredText"
    TYPE_INFOSCIENCE = "infoscience"
    TYPE_ACTU = "actu"
    TYPE_MEMENTO = "memento"
    TYPE_FAQ = "faq"
    TYPE_TOGGLE = "toggle"
    TYPE_INCLUDE = "include"
    TYPE_CONTACT = "contact"
    TYPE_XML = "xml"
    TYPE_LINKS = "links"
    TYPE_RSS = "rss"
    TYPE_FILES = "files"
    TYPE_SNIPPETS = "snippets"

    # Mapping of known box types from Jahia to WP
    types = {
        "epfl:textBox": TYPE_TEXT,
        "epfl:coloredTextBox": TYPE_COLORED_TEXT,
        "epfl:infoscienceBox": TYPE_INFOSCIENCE,
        "epfl:actuBox": TYPE_ACTU,
        "epfl:mementoBox": TYPE_MEMENTO,
        "epfl:faqContainer": TYPE_FAQ,
        "epfl:toggleBox": TYPE_TOGGLE,
        "epfl:htmlBox": TYPE_INCLUDE,
        "epfl:contactBox": TYPE_CONTACT,
        "epfl:xmlBox": TYPE_XML,
        "epfl:linksBox": TYPE_LINKS,
        "epfl:rssBox": TYPE_RSS,
        "epfl:filesBox": TYPE_FILES,
        "epfl:snippetsBox": TYPE_SNIPPETS
    }

    def __init__(self, site, page_content, element, multibox=False):
        self.site = site
        self.page_content = page_content
        self.type = ""
        self.set_type(element)
        self.title = Utils.get_tag_attribute(element, "boxTitle", "jahia:value")
        self.content = ""
        self.set_content(element, multibox)

    def set_type(self, element):
        """
        Sets the box type
        """

        type = element.getAttribute("jcr:primaryType")

        if type in self.types:
            self.type = self.types[type]
        else:
            self.type = type

    def set_content(self, element, multibox=False):
        """set the box attributes"""

        # text
        if self.TYPE_TEXT == self.type or self.TYPE_COLORED_TEXT == self.type:
            self.set_box_text(element, multibox)
        # infoscience
        elif self.TYPE_INFOSCIENCE == self.type:
            self.set_box_infoscience(element)
        # actu
        elif self.TYPE_ACTU == self.type:
            self.set_box_actu(element)
        # memento
        elif self.TYPE_MEMENTO == self.type:
            self.set_box_memento(element)
        # faq
        elif self.TYPE_FAQ == self.type:
            self.set_box_faq(element)
        # toggle
        elif self.TYPE_TOGGLE == self.type:
            self.set_box_toggle(element)
        # include
        elif self.TYPE_INCLUDE == self.type:
            self.set_box_include(element)
        # contact
        elif self.TYPE_CONTACT == self.type:
            self.set_box_contact(element)
        # xml
        elif self.TYPE_XML == self.type:
            self.set_box_xml(element)
        # links
        elif self.TYPE_LINKS == self.type:
            self.set_box_links(element)
        # rss
        elif self.TYPE_RSS == self.type:
            self.set_box_rss(element)
        # files
        elif self.TYPE_FILES == self.type:
            self.set_box_files(element)
        # snippets
        elif self.TYPE_SNIPPETS == self.type:
            self.set_box_snippets(element)
        # unknown
        else:
            self.set_box_unknown(element)

    def set_box_text(self, element, multibox=False):
        """set the attributes of a text box
            A text box can have two forms, either it contains just a <text> tag
            or it contains a <comboListList> which contains <comboList> tags which
            contain <text>, <filesList>, <linksList> tags. The last two tags may miss from time
            to time because the jahia export is not perfect.
            FIXME: For now <filesList> are ignored because we did not find a site where
            it is used yet.
        """

        if not multibox:
            content = Utils.get_tag_attribute(element, "text", "jahia:value")
            linksList = element.getElementsByTagName("linksList")
            if linksList:
                content += self._parse_links_to_list(linksList[0])
        else:
            # Concatenate HTML content of many boxes
            content = ""
            comboLists = element.getElementsByTagName("comboList")
            for element in comboLists:
                content += Utils.get_tag_attribute(element, "text", "jahia:value")
                # linksList contain <link> tags exactly like linksBox, so we can just reuse
                # the same code used to parse linksBox.
                content += self._parse_links_to_list(element)

        self.content = content

    def set_box_actu(self, element):
        """set the attributes of an actu box"""
        url = Utils.get_tag_attribute(element, "url", "jahia:value")

        self.content = "[actu url=%s]" % url

    def set_box_memento(self, element):
        """set the attributes of a memento box"""
        url = Utils.get_tag_attribute(element, "url", "jahia:value")

        self.content = "[memento url=%s]" % url

    def set_box_infoscience(self, element):
        """set the attributes of a infoscience box"""
        url = Utils.get_tag_attribute(element, "url", "jahia:value")

        self.content = "[epfl_infoscience url=%s]" % url

    def set_box_faq(self, element):
        """set the attributes of a faq box"""
        self.question = Utils.get_tag_attribute(element, "question", "jahia:value")

        self.answer = Utils.get_tag_attribute(element, "answer", "jahia:value")

        self.content = "
%s

%s
" % (self.question, self.answer)

    def set_box_toggle(self, element):
        """set the attributes of a toggle box"""
        self.opened = Utils.get_tag_attribute(element, "opened", "jahia:value")

        self.content = Utils.get_tag_attribute(element, "content", "jahia:value")

    def set_box_include(self, element):
        """set the attributes of an include box"""
        url = Utils.get_tag_attribute(element, "url", "jahia:value")

        self.content = "[include url=%s]" % url

    def set_box_contact(self, element):
        """set the attributes of a contact box"""
        text = Utils.get_tag_attribute(element, "text", "jahia:value")

        self.content = text

    def set_box_xml(self, element):
        """set the attributes of a xml box"""
        xml = Utils.get_tag_attribute(element, "xml", "jahia:value")
        xslt = Utils.get_tag_attribute(element, "xslt", "jahia:value")

        self.content = "[xml xml=%s xslt=%s]" % (xml, xslt)

    def set_box_rss(self, element):
        """set the attributes of an rss box"""

        # Jahia options
        url = Utils.get_tag_attribute(element, "url", "jahia:value")
        nb_items = Utils.get_tag_attribute(element, "nbItems", "jahia:value")
        hide_title = Utils.get_tag_attribute(element, "hideTitle", "jahia:value")
        detail_items = Utils.get_tag_attribute(element, "detailItems", "jahia:value")

        # check if we have at least an url
        if not url:
            return

        # some values are in JSP tag, with use a default value instead
        if not nb_items.isdigit():
            nb_items = "5"

        # feedzy-rss options
        feeds = url
        max = nb_items
        feed_title = "yes"
        summary = "yes"

        if hide_title == "true":
            feed_title = "no"

        if detail_items != "true":
            summary = "no"

        self.content = "[feedzy-rss feeds=\"{}\" max=\"{}\" feed_title=\"{}\" summary=\"{}\" refresh=\"12_hours\"]"\
            .format(feeds, max, feed_title, summary)

    def set_box_links(self, element):
        """set the attributes of a links box"""
        self.content = self._parse_links_to_list(element)

    def set_box_unknown(self, element):
        """set the attributes of an unknown box"""
        self.content = "[%s]" % element.getAttribute("jcr:primaryType")

    def set_box_files(self, element):
        """set the attributes of a files box"""
        elements = element.getElementsByTagName("file")
        content = "
"
        for e in elements:
            if e.ELEMENT_NODE != e.nodeType:
                continue
            # URL is like /content/sites/<site_name>/files/file
            # splitted gives ['', content, sites, <site_name>, files, file]
            # result of join is files/file and we add the missing '/' in front.
            file_url = '/'.join(e.getAttribute("jahia:value").split("/")[4:])
            file_url = '/' + file_url
            file_name = file_url.split("/")[-1]
            content += '
{}
'.format(file_url, file_name)
        content += "
"
        self.content = content

    def set_box_snippets(self, element):
        """set the attributes of a snippets box"""

        snippets = element.getElementsByTagName("snippetListList")[0].getElementsByTagName("snippetList")

        for snippet in snippets:
            title = Utils.get_tag_attribute(snippet, "title", "jahia:value")
            subtitle = Utils.get_tag_attribute(snippet, "subtitle", "jahia:value")
            description = Utils.get_tag_attribute(snippet, "description", "jahia:value")
            image = Utils.get_tag_attribute(snippet, "image", "jahia:value")
            big_image = Utils.get_tag_attribute(snippet, "bigImage", "jahia:value")
            enable_zoom = Utils.get_tag_attribute(snippet, "enableImageZoom", "jahia:value")

            # escape
            title = title.replace('"','\\"')
            subtitle = subtitle.replace('"','\\"')
            description = description.replace('"','\\"')

            self.content = "[epfl_snippets title=\"{}\" subtitle=\"{}\" image=\"{}\"" \
                           "big_image=\"{}\" enable_zoom=\"{}\" description=\"{}\"]".format(
                title, subtitle, image, big_image, enable_zoom, description
            )

    def _parse_links_to_list(self, element):
        """Handles link tags that can be found in linksBox and textBox"""
        elements = element.getElementsByTagName("link")
        content = "
"
        for e in elements:
            if e.ELEMENT_NODE != e.nodeType:
                continue
            for jahia_tag in e.childNodes:
                if jahia_tag.ELEMENT_NODE != jahia_tag.nodeType:
                    continue
                if jahia_tag.tagName == "jahia:link":
                    # It happens that a link references a page that does not exist anymore
                    # observed on site dii
                    try:
                        page = self.site.pages_by_uuid[jahia_tag.getAttribute("jahia:reference")]
                    except KeyError as e:
                        continue
                    content += '
{}
'.format(page.pid, jahia_tag.getAttribute("jahia:title"))
                elif jahia_tag.tagName == "jahia:url":
                    url = jahia_tag.getAttribute("jahia:value")
                    title = jahia_tag.getAttribute("jahia:title")
                    content += '
{}
'.format(url, title)
        content += "
"

        if content == "

":
            return ""

        return content

    def __str__(self):
        return self.type + " " + self.title
<|endoftext|>


<commit_before>"""\

	<title>RED: <%(html_uri)s></title>
	<link rel="stylesheet" type='text/css' href="red_style.css">
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<meta name="ROBOTS" content="INDEX, NOFOLLOW" />

Resource Expert Droid

<form method="GET">
<input type="text" name="uri" value="%(html_uri)s" id="uri"/>

</form>

"""<commit_msg>fix link listing css position<commit_after>"""\

	<title>RED: <%(html_uri)s></title>
	<link rel="stylesheet" type='text/css' href="red_style.css">
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<meta name="ROBOTS" content="INDEX, NOFOLLOW" />

Resource Expert Droid

<form method="GET">
<input type="text" name="uri" value="%(html_uri)s" id="uri"/>

</form>

"""<|endoftext|>