Changeset e5f0831 in tailbone


Ignore:
Timestamp:
06/22/20 16:00:33 (8 weeks ago)
Author:
Lance Edgar <lance@…>
Branches:
master
Children:
bb11263
Parents:
6463df7
Message:

Add ability to download roles / permissions matrix as Excel file

Location:
tailbone
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • tailbone/views/roles.py

    r6463df7 re5f0831  
    2727from __future__ import unicode_literals, absolute_import
    2828
     29import os
     30
    2931import six
    3032from sqlalchemy import orm
     33from openpyxl.styles import Font, PatternFill
    3134
    3235from rattail.db import model
    3336from rattail.db.auth import (has_permission, grant_permission, revoke_permission,
    3437                             administrator_role, guest_role, authenticated_role)
     38from rattail.excel import ExcelWriter
    3539
    3640import colander
     
    279283        return roles
    280284
     285    def download_permissions_matrix(self):
     286        """
     287        View which renders the complete role / permissions matrix data into an
     288        Excel spreadsheet, and returns that file.
     289        """
     290        roles = self.Session.query(model.Role)\
     291                            .order_by(model.Role.name)\
     292                            .all()
     293
     294        permissions = self.get_available_permissions()
     295
     296        # prep the excel writer
     297        path = os.path.join(self.rattail_config.workdir(),
     298                            'permissions-matrix.xlsx')
     299        writer = ExcelWriter(path, None)
     300        sheet = writer.sheet
     301
     302        # write header
     303        sheet.cell(row=1, column=1, value="")
     304        for i, role in enumerate(roles, 2):
     305            sheet.cell(row=1, column=i, value=role.name)
     306
     307        # font and fill pattern for permission group rows
     308        bold = Font(bold=True)
     309        group_fill = PatternFill(patternType='solid',
     310                                 fgColor='d9d9d9',
     311                                 bgColor='d9d9d9')
     312
     313        # now we'll write the rows
     314        writing_row = 2
     315        for groupkey in sorted(permissions, key=lambda k: permissions[k]['label'].lower()):
     316            group = permissions[groupkey]
     317
     318            # group label is bold, with fill pattern
     319            cell = sheet.cell(row=writing_row, column=1, value=group['label'])
     320            cell.font = bold
     321            cell.fill = group_fill
     322
     323            # continue fill pattern for rest of group row
     324            for col, role in enumerate(roles, 2):
     325                cell = sheet.cell(row=writing_row, column=col)
     326                cell.fill = group_fill
     327
     328            # okay, that row is done
     329            writing_row += 1
     330
     331            # now we list each perm in the group
     332            perms = group['perms']
     333            for key in sorted(perms, key=lambda p: perms[p]['label'].lower()):
     334                sheet.cell(row=writing_row, column=1, value=perms[key]['label'])
     335
     336                # and show an 'X' for any role which has this perm
     337                for col, role in enumerate(roles, 2):
     338                    if has_permission(self.Session(), role, key, include_guest=False):
     339                        sheet.cell(row=writing_row, column=col, value="X")
     340
     341                writing_row += 1
     342
     343        writer.auto_resize()
     344        writer.auto_freeze()
     345        writer.save()
     346        return self.file_response(path)
     347
    281348    @classmethod
    282349    def defaults(cls, config):
     
    287354    @classmethod
    288355    def _role_defaults(cls, config):
     356        route_prefix = cls.get_route_prefix()
     357        url_prefix = cls.get_url_prefix()
    289358        permission_prefix = cls.get_permission_prefix()
    290359
     
    297366                                       "Edit Role(s) to which current user belongs")
    298367
     368        # download permissions matrix
     369        config.add_tailbone_permission(permission_prefix, '{}.download_permissions_matrix'.format(permission_prefix),
     370                                       "Download complete Role/Permissions matrix")
     371        config.add_route('{}.download_permissions_matrix'.format(route_prefix), '{}/permissions-matrix'.format(url_prefix),
     372                         request_method='GET')
     373        config.add_view(cls, attr='download_permissions_matrix', route_name='{}.download_permissions_matrix'.format(route_prefix),
     374                        permission='{}.download_permissions_matrix'.format(permission_prefix))
     375
    299376
    300377class PermissionsWidget(dfwidget.Widget):
Note: See TracChangeset for help on using the changeset viewer.