Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[2878] Model and migrations for roles/permissions
Models for Role and Permissions wrt organisational auth, and a migration to add the two (small) required tables
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from sqlalchemy import * | ||
from migrate import * | ||
|
||
def upgrade(migrate_engine): | ||
metadata = MetaData() | ||
metadata.bind = migrate_engine | ||
migrate_engine.execute(''' | ||
BEGIN; | ||
CREATE TABLE organisationrole ( | ||
id text NOT NULL, | ||
name text | ||
); | ||
CREATE TABLE permission ( | ||
id text NOT NULL, | ||
name text, | ||
description text, | ||
organisationrole_id text NOT NULL | ||
); | ||
ALTER TABLE organisationrole | ||
ADD CONSTRAINT organisationrole_pkey PRIMARY KEY (id); | ||
ALTER TABLE permission | ||
ADD CONSTRAINT permission_pkey PRIMARY KEY (id); | ||
ALTER TABLE permission | ||
ADD CONSTRAINT permission_organisationrole_id_fkey FOREIGN KEY (organisationrole_id) REFERENCES organisationrole(id); | ||
COMMIT; | ||
''' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import datetime | ||
|
||
import sqlalchemy as sa | ||
from sqlalchemy import orm | ||
from sqlalchemy import types, Column, Table, ForeignKey, and_, func | ||
|
||
import meta | ||
import domain_object | ||
import types as _types | ||
import package as _package | ||
|
||
__all__ = ['OrganisationRole', 'Permission', 'permission_table', | ||
'organisationrole_table'] | ||
|
||
organisationrole_table = sa.Table( | ||
'organisationrole', meta.metadata, | ||
Column('id', types.UnicodeText, primary_key=True, | ||
default=_types.make_uuid), | ||
Column('name', types.UnicodeText), | ||
) | ||
|
||
permission_table = Table( | ||
'permission', meta.metadata, | ||
Column('id', types.UnicodeText, primary_key=True, | ||
default=_types.make_uuid), | ||
Column('name', types.UnicodeText), | ||
Column('description', types.UnicodeText), | ||
Column('organisationrole_id', types.UnicodeText, | ||
ForeignKey('organisationrole.id'), nullable=False), | ||
) | ||
|
||
|
||
class OrganisationRole(domain_object.DomainObject): | ||
|
||
@classmethod | ||
def get(cls, name): | ||
return meta.Session.query(OrganisationRole).filter( | ||
OrganisationRole.name == name).first() | ||
|
||
|
||
class Permission(domain_object.DomainObject): | ||
|
||
@classmethod | ||
def get(cls, name): | ||
return meta.Session.query(Permission).filter( | ||
Permission.name == name).first() | ||
|
||
|
||
meta.mapper(Permission, permission_table, properties={ | ||
'role': orm.relation(OrganisationRole) | ||
}) | ||
|
||
meta.mapper(OrganisationRole, organisationrole_table, | ||
properties={'permissions': | ||
orm.relation(Permission, | ||
backref=orm.backref('permission'))}) |