Пример #1
0
    def edit_organisation_html(self, REQUEST):
        """ Edit organisation data """
        org_id = REQUEST.form['id']
        if not self.can_edit_organisation():
            _set_session_message(REQUEST, 'error',
                                 ("You are not allowed to "
                                  "edit this organisation"))
            REQUEST.RESPONSE.redirect(self.absolute_url() +
                                      '/organisation?id=' + org_id)
            return None
        nfp_country = self.nfp_for_country()
        if self.checkPermissionEditOrganisations():
            countries = get_country_options()
        else:
            countries = get_country_options(country=nfp_country)

        options = {
            'countries': countries,
            'form_macro': get_template_macro('org_form_fields'),
        }

        session = REQUEST.SESSION
        if SESSION_FORM_DATA in session.keys():
            options['org_info'] = session[SESSION_FORM_DATA]
            del session[SESSION_FORM_DATA]
        else:
            options['org_info'] = self._get_ldap_agent().org_info(org_id)

        org_id = options['org_info']['id']
        self._set_breadcrumbs([(options['org_info']['id'],
                                self.absolute_url() + '/organisation?id=%s' %
                                org_id),
                               ('Edit Organisation', '#')])
        return self._render_template('zpt/orgs_edit.zpt', **options)
Пример #2
0
    def edit_organisation_html(self, REQUEST):
        """ Edit organisation data """
        org_id = REQUEST.form["id"]
        if not self.can_edit_organisation():
            _set_session_message(REQUEST, "error", ("You are not allowed to " "edit this organisation"))
            REQUEST.RESPONSE.redirect(self.absolute_url() + "/organisation?id=" + org_id)
            return None
        nfp_country = self.nfp_for_country()
        if self.checkPermissionEditOrganisations():
            countries = get_country_options()
        else:
            countries = get_country_options(country=nfp_country)

        options = {"countries": countries, "form_macro": get_template_macro("org_form_fields")}

        session = REQUEST.SESSION
        if SESSION_FORM_DATA in session.keys():
            options["org_info"] = session[SESSION_FORM_DATA]
            del session[SESSION_FORM_DATA]
        else:
            options["org_info"] = self._get_ldap_agent().org_info(org_id)

        org_id = options["org_info"]["id"]
        self._set_breadcrumbs(
            [
                (options["org_info"]["id"], self.absolute_url() + "/organisation?id=%s" % org_id),
                ("Edit Organisation", "#"),
            ]
        )
        return self._render_template("zpt/orgs_edit.zpt", **options)
Пример #3
0
    def create_organisation_html(self, REQUEST):
        """ Page for adding an organisation """
        if not self.can_edit_organisations():
            _set_session_message(REQUEST, 'error',
                                 ("You are not allowed to "
                                  "create an organisation"))
            REQUEST.RESPONSE.redirect(self.absolute_url())
            return None
        nfp_country = self.nfp_for_country()
        if self.checkPermissionEditOrganisations():
            countries = get_country_options()
        else:
            countries = get_country_options(country=nfp_country)
        options = {
            'countries': countries,
            'form_macro': get_template_macro('org_form_fields'),
            'create_mode': True,
        }

        session = REQUEST.SESSION
        if SESSION_FORM_DATA in session.keys():
            options['org_info'] = session[SESSION_FORM_DATA]
            del session[SESSION_FORM_DATA]
        else:
            options['org_info'] = {}

        self._set_breadcrumbs([('Create Organisation', '#')])
        return self._render_template('zpt/orgs_create.zpt', **options)
Пример #4
0
 def index_html(self, REQUEST):
     """ Index of organisations """
     country = REQUEST.get("country")
     nfp_country = self.nfp_for_country()
     if self.title != "National Organisations":
         nfp_country = None
     if not (self.checkPermissionView() or nfp_country):
         raise Unauthorized
     agent = self._get_ldap_agent()
     orgs_by_id = agent.all_organisations()
     countries = dict(get_country_options(country=nfp_country or country))
     orgs = []
     for org_id, info in orgs_by_id.iteritems():
         country = countries.get(info["country"])
         if country:
             orgs.append(
                 {
                     "id": org_id,
                     "name": info["name"],
                     "country": country["name"],
                     "country_pub_code": country["pub_code"],
                 }
             )
     orgs.sort(key=operator.itemgetter("id"))
     options = {"sorted_organisations": orgs}
     return self._render_template("zpt/orgs_index.zpt", **options)
Пример #5
0
 def index_html(self, REQUEST):
     """ Index of organisations """
     country = REQUEST.get('country')
     nfp_country = self.nfp_for_country()
     if self.title != 'National Organisations':
         nfp_country = None
     if not (self.checkPermissionView() or nfp_country):
         raise Unauthorized
     agent = self._get_ldap_agent(secondary=True)
     orgs_by_id = agent.all_organisations()
     countries = dict(get_country_options(country=nfp_country or country))
     orgs = []
     for org_id, info in orgs_by_id.iteritems():
         country = countries.get(info['country'])
         if country:
             orgs.append({
                 'id': org_id,
                 'name': info['name'],
                 'country': country['name'],
                 'country_pub_code': country['pub_code']
             })
     orgs.sort(key=operator.itemgetter('id'))
     options = {
         'sorted_organisations': orgs,
     }
     return self._render_template('zpt/orgs_index.zpt', **options)
Пример #6
0
    def create_organisation_html(self, REQUEST):
        """ Page for adding an organisation """
        if not self.can_edit_organisations():
            _set_session_message(REQUEST, "error", ("You are not allowed to " "create an organisation"))
            REQUEST.RESPONSE.redirect(self.absolute_url())
            return None
        nfp_country = self.nfp_for_country()
        if self.checkPermissionEditOrganisations():
            countries = get_country_options()
        else:
            countries = get_country_options(country=nfp_country)
        options = {"countries": countries, "form_macro": get_template_macro("org_form_fields"), "create_mode": True}

        session = REQUEST.SESSION
        if SESSION_FORM_DATA in session.keys():
            options["org_info"] = session[SESSION_FORM_DATA]
            del session[SESSION_FORM_DATA]
        else:
            options["org_info"] = {}

        self._set_breadcrumbs([("Create Organisation", "#")])
        return self._render_template("zpt/orgs_create.zpt", **options)
Пример #7
0
 def index_html(self, REQUEST):
     """ Index of organisations """
     country = REQUEST.get('country')
     nfp_country = self.nfp_for_country()
     if self.title != 'National Organisations':
         nfp_country = None
     if not (self.checkPermissionView() or nfp_country):
         raise Unauthorized
     agent = self._get_ldap_agent(secondary=True)
     orgs_by_id = agent.all_organisations()
     countries = dict(get_country_options(country=nfp_country or country))
     orgs = []
     for org_id, info in orgs_by_id.iteritems():
         country = countries.get(info['country'])
         if country:
             orgs.append({'id': org_id,
                          'name': info['name'],
                          'country': country['name'],
                          'country_pub_code': country['pub_code']})
     orgs.sort(key=operator.itemgetter('id'))
     options = {
         'sorted_organisations': orgs,
     }
     return self._render_template('zpt/orgs_index.zpt', **options)
Пример #8
0
    def export_organisations(self, REQUEST):
        """ Export of organisations """
        if not _is_authenticated(REQUEST):
            raise Unauthorized

        agent = self._get_ldap_agent()
        orgs_by_id = agent.all_organisations()

        if self.checkPermissionEditOrganisations():
            countries = dict(get_country_options())
            nfp_country = 'all'
        else:
            nfp_country = self.nfp_for_country()
            if not (self.checkPermissionView() or nfp_country):
                raise Unauthorized
            countries = dict(get_country_options(country=nfp_country))

        orgs = []
        for org_id, info in orgs_by_id.iteritems():
            country = countries.get(info['country'])
            if country:
                orgs.append({'id': org_id,
                             'name': info['name'],
                             'country': country['name'],
                             'country_pub_code': country['pub_code']})
        orgs.sort(key=operator.itemgetter('id'))

        for d in orgs:
            org_info = agent.org_info(d['id'])
            d.update(org_info)

        wb = xlwt.Workbook()
        org_sheet = wb.add_sheet("Organisations")
        users_sheet = wb.add_sheet("Members")

        style_header = xlwt.XFStyle()
        style_org_header = xlwt.XFStyle()
        style_normal = xlwt.XFStyle()

        normalfont = xlwt.Font()
        headerfont = xlwt.Font()
        headerfont.bold = True
        biggerheaderfont = xlwt.Font()
        biggerheaderfont.bold = True
        biggerheaderfont.height = int(biggerheaderfont.height * 1.3)

        style_header.font = headerfont
        style_normal.font = normalfont
        style_org_header.font = biggerheaderfont

        cols = [
            'id',
            'name',
            'locality',
            'postal_address',
            'fax',
            'email',
        ]
        for i, col in enumerate(cols):
            org_sheet.write(0, i, col.capitalize(), style_header)

        org_sheet.write(0, 6, "Members count", style_header)

        for i, row in enumerate(orgs):
            org_sheet.write(i + 2, 0, row['id'], style_normal)
            org_sheet.write(i + 2, 1, row['name'], style_normal)
            org_sheet.write(i + 2, 2, row['locality'], style_normal)
            org_sheet.write(i + 2, 3, row['postal_address'], style_normal)
            org_sheet.write(i + 2, 4, row['fax'], style_normal)
            org_sheet.write(i + 2, 5, row['email'], style_normal)
            members = agent.members_in_org(row['id'])   # TODO: optimize
            org_sheet.write(i + 2, 6, len(members), style_normal)

        org_sheet.col(1).set_width(9000)
        org_sheet.col(2).set_width(5000)
        org_sheet.col(3).set_width(9000)
        org_sheet.col(4).set_width(4000)
        org_sheet.col(5).set_width(5000)

        row_counter = 0
        for org in orgs:
            org_id, org_name = org['id'], org['name']

            users_sheet.write(row_counter, 0, org_name, style_org_header)
            row = users_sheet.row(row_counter)
            row.height = int(row.height * 1.3)
            row_counter += 2

            org_members = []

            members = agent.members_in_org(org_id)
            for user_id in members:
                try:
                    org_members.append(agent.user_info(user_id))
                except (NO_SUCH_OBJECT, eea.usersdb.UserNotFound):
                    pass
            org_members.sort(key=operator.itemgetter('first_name'))

            cols = [
                'user id',
                'fullname',
                'email',
            ]

            for i, col in enumerate(cols):
                users_sheet.write(row_counter, i, col, style_header)

            for i, member in enumerate(org_members, 1):
                users_sheet.write(row_counter + i, 0, member['uid'])
                users_sheet.write(row_counter + i, 1, member['full_name'])
                users_sheet.write(row_counter + i, 2, member['email'])

            users_sheet.col(0).set_width(4000)
            users_sheet.col(1).set_width(6000)
            users_sheet.col(2).set_width(9000)

            row_counter += i + 2

        out = StringIO()
        wb.save(out)
        out.seek(0)
        out = out.read()

        RESPONSE = REQUEST.RESPONSE

        RESPONSE.setHeader('Content-Type', "application/vnd.ms-excel")
        RESPONSE.setHeader('Content-Length', len(out))
        RESPONSE.setHeader('Pragma', 'public')
        RESPONSE.setHeader('Cache-Control', 'max-age=0')
        RESPONSE.addHeader("content-disposition",
                           "attachment; filename=%s-organisations.xls" %
                           nfp_country)

        return out