Ejemplo n.º 1
0
    def get(self):
        # Authenticate user
        voter = auth.get_voter(self)
        status = models.get_admin_status(voter)
        if not status:
            logging.info('Not authorized')
            webapputils.render_page(self, '/templates/message', {
                'status': 'Not Authorized',
                'msg': MSG_NOT_AUTHORIZED
            })
            return

        # Get organization information
        admin = models.Admin.gql('WHERE voter=:1', voter).get()
        logging.info("<Admin: %s>", admin.email)
        org_admin = models.OrganizationAdmin.gql('WHERE admin=:1',
                                                 admin).fetch(None)
        logging.info("<Admin of Organizations: %s>",
                     [oa.organization.name for oa in org_admin])
        if not org_admin:
            logging.info('Not authorized')
            webapputils.render_page(self, '/templates/message', {
                'status': 'Not Authorized',
                'msg': MSG_NOT_AUTHORIZED
            })
            return
        orgs = [org_admin.organization for org_admin in org_admin]
        auth.set_organizations(orgs)

        # Pick one organization to display information about.
        org_param = self.request.get('org')

        if org_param:  # Wants to change current active organization
            org_req = models.Organization.get(org_param)
            auth.set_active_organization(org_req)

        elif auth.get_active_organization(
        ):  # Did not intend a change in the active organization
            pass

        else:  # No active organizations have been set yet
            auth.set_active_organization(orgs[0])

        # Construct page information
        page_data = {}
        page_data['organizations'] = orgs
        page_data['active_org'] = auth.get_active_organization()
        page_data['admins'] = self.admin_list(auth.get_active_organization())
        page_data['elections'] = [
            elec.to_json(True)
            for elec in auth.get_active_organization().elections
        ]
        logging.info(page_data['elections'])
        logging.info(page_data)
        webapputils.render_page(self, PAGE_NAME, page_data)
Ejemplo n.º 2
0
 def update_election(self, election, data):
     out = {'status': 'OK'}
     end_dt = datetime.fromtimestamp(data['times']['end'])
     pub_dt = datetime.fromtimestamp(data['times']['pub'])
     # delay = difference between pub and end in seconds
     res_delay = int((pub_dt - end_dt).total_seconds())
     if not election:
         # User must be trying to create new election
         election = models.Election(
             name=data['name'],
             start=datetime.fromtimestamp(data['times']['start']),
             end=end_dt,
             organization=auth.get_active_organization(),
             universal=data['universal'],
             hidden=data['hidden'],
             result_delay=res_delay,
             description=data['description'])
         election.put()
         out['msg'] = 'Created'
         auth.set_election(election)
     else:
         election.name = data['name']
         election.start = datetime.fromtimestamp(data['times']['start'])
         election.end = end_dt
         election.universal = data['universal']
         election.hidden = data['hidden']
         election.result_delay = res_delay
         election.description = data['description']
         election.put()
         out['msg'] = 'Updated'
     out['election'] = election.to_json()
     self.response.write(json.dumps(out))
Ejemplo n.º 3
0
    def get(self):
        # Authenticate user
        voter = auth.get_voter(self)
        status = models.get_admin_status(voter)
        if not status:
            logging.info('Not authorized')
            webapputils.render_page(self, '/templates/message',
                {'status': 'Not Authorized', 'msg': MSG_NOT_AUTHORIZED})
            return

        # Get organization information
        admin = models.Admin.gql('WHERE voter=:1', voter).get()
        logging.info("<Admin: %s>", admin.email)
        org_admin = models.OrganizationAdmin.gql('WHERE admin=:1',
                                                    admin).fetch(None)
        logging.info("<Admin of Organizations: %s>", [oa.organization.name for oa in org_admin])
        if not org_admin:
            logging.info('Not authorized')
            webapputils.render_page(self, '/templates/message',
                {'status': 'Not Authorized', 'msg': MSG_NOT_AUTHORIZED})
            return
        orgs = [org_admin.organization for org_admin in org_admin]
        auth.set_organizations(orgs)

        # Pick one organization to display information about.
        org_param = self.request.get('org')

        if org_param:   # Wants to change current active organization
            org_req = models.Organization.get(org_param)
            auth.set_active_organization(org_req)

        elif auth.get_active_organization():    # Did not intend a change in the active organization
            pass

        else:   # No active organizations have been set yet
            auth.set_active_organization(orgs[0])

        # Construct page information
        page_data = {}
        page_data['organizations'] = orgs
        page_data['active_org'] = auth.get_active_organization()
        page_data['admins'] = self.admin_list(auth.get_active_organization())
        page_data['elections'] = [elec.to_json(True) for elec in auth.get_active_organization().elections]
        logging.info(page_data['elections'])
        logging.info(page_data)
        webapputils.render_page(self, PAGE_NAME, page_data)
Ejemplo n.º 4
0
    def post(self):
        methods = {
            'get_election': self.get_election,
            'update_election': self.update_election
        }

        # Authenticate user
        org = auth.get_active_organization()
        if not org:
            webapputils.respond(self, 'ERROR', 'Not Authorized')
            return

        # Get election
        election = auth.get_election()

        # Get the method
        data = json.loads(self.request.get('data'))
        method = data['method']
        logging.info('Method: %s\n Data: %s', method, data)
        if method in methods:
            methods[method](election, data)
        else:
            webapputils.respond(self, 'ERROR', 'Unkown method')