Page Source

from utils.display import Display, Database
from utils.controls import *
from utils.output import *
import debugging

class GroupingControl(MultipleControl):
    def output_disp(self,display,container):
        if display.fields['grouping'] != display.last_grouping:
            display.last_grouping = display.fields['grouping']
            container.append("<h1>%(grouping)s</h1>\n" % display.fields)

class PersonPhotoControl(MultipleControl):
    def output_disp(self, display, container):
        q = queries.Select(tables=["documents"],
                columns={'keyname' : 'keyname', 'format': 'format'},
                where="""
                category = "photos" AND
                keyname = "%(login)s"
                """ % display.fields)
        q.execute()
        photos = q.fetchall()
        if len(photos) == 1:
            container.append(""" <a href="/people/%(login)s">""" % display.fields)
            container.append("""<img class="person" src="/files/photos/%(keyname)s.%(format)s" """ % photos[0])
            container.append("""title="%(name)s" alt="%(name)s"/></a>\n""" % display.fields)
        else:
            container.append(""" <a href="/people/%(login)s"><img class="person" src="/i/fill.png" title="%(name)s" alt="%(name)s"/></a>\n""" % display.fields)

class PersonInfoControl(MultipleControl):
    def output_disp(self, display, container):
        q = queries.Select(tables=["group_affiliation_login", "group_affiliation"],
                columns={'group_affiliation_name' : 'group_affiliation_name'},
                where="group_affiliation_login.login = \"%(login)s\" AND group_affiliation_login.group_affiliation_id = group_affiliation.id" % display.fields)

        q.execute()
        groups = q.fetchall()
        display.fields['group_list'] = ""
        if len(groups) > 0:
            group_list = []
            for g in groups:
                group_list.append(g['group_affiliation_name'])
            display.fields['group_list'] = "  <div class=\"person-affiliations\"><i>Interests</i>: " + string.join(group_list,", ") + "</div>\n"
        container.append(""" <div class="person-info">\n""")
        container.append("""  <div class="person-name"><a href=\"/people/%(login)s\">%(name)s</a></div>\n""" % display.fields)
        container.append("""  <div class="person-title">%(title)s</div>\n""" % display.fields)
        container.append("""%(group_list)s""" % display.fields)
        container.append(""" </div>\n""")


class FacultyDisplay(Display, Database):
    def is_multiple(self):
        return 1

    def get_multiple_title(self):
        return ""
    multiple_controls = [
    CoalesceContainerControl([
            GroupingControl(),
            '<div class="faculty">\n',
            PersonPhotoControl(),
            PersonInfoControl(),
            '</div>\n'
            ])
    ]

    def make_page(self,page):
        page.append("%s" % self.get_multiple_title())
        self.last_grouping = None
        page.append(self.multiple_database())

    def db_query(self):
        q = queries.Select(tables=['people', 'titles', 'groupings', 'grouping_priorities'],
                columns = { 
                'grouping' : 'grouping_priorities.name',
                'name' : queries.SQL('concat(fname, " ", lname)'),
                'login' : 'people.login',
                'title' : 'titles.title'
                },
                where="""\
                people.login = titles.login AND
                (titles.title LIKE groupings.title OR groupings.title IS NULL) AND
                (titles.context LIKE groupings.context OR groupings.context IS NULL) AND
                groupings.id = grouping_priorities.id AND
                NOT people.retired AND NOT people.pseudo AND
                groupings.id IN (3,8,9,1,10)
                """,
                order=['grouping_priorities.priority', 'lname', 'fname', 'mname'],
                distinct=1)

        q.execute()
        self.query = q
    
    def db_fetch(self):
        self.fields = self.query.fetch()
        return self.fields


def new(req):
    return FacultyDisplay(req)