client and small changes in the serializer

This commit is contained in:
FinnStutzenstein 2019-04-01 14:35:02 +02:00
parent 6f24b7c169
commit 23264849c9
9 changed files with 76 additions and 42 deletions

View File

@ -21,7 +21,7 @@ export class WorkflowState extends Deserializer {
public name: string; public name: string;
public recommendation_label: string; public recommendation_label: string;
public css_class: string; public css_class: string;
public access_level: number; public restriction: string[];
public allow_support: boolean; public allow_support: boolean;
public allow_create_poll: boolean; public allow_create_poll: boolean;
public allow_submitter_edit: boolean; public allow_submitter_edit: boolean;

View File

@ -103,13 +103,24 @@
</div> </div>
<div <div
class="clickable-cell" class="clickable-cell"
*ngIf="perm.type === 'accessLevel'" *ngIf="perm.type === 'restriction'"
[matMenuTriggerFor]="accessLevelMenu" [matMenuTriggerFor]="restrictionMenu"
[matMenuTriggerData]="{ state: state }" [matMenuTriggerData]="{ state: state }"
matTooltip="{{ accessLevels[state.access_level].label | translate }}"
> >
<div class="inner-table"> <div class="inner-table">
{{ accessLevels[state.access_level].label | translate }} <div *ngIf="!state.restriction.length">
-
</div>
<div *ngIf="state.restriction.length">
<span
*ngFor="
let restriction of state.restriction;
let last = last
"
>
{{ getRestrictionLabel(restriction) | translate }}<span *ngIf="!last">,&nbsp;</span>
</span>
</div>
</div> </div>
</div> </div>
</mat-cell> </mat-cell>
@ -179,12 +190,12 @@
</ng-template> </ng-template>
</mat-menu> </mat-menu>
<!-- Select access level menu --> <!-- Select restriction menu -->
<mat-menu matMenuContent #accessLevelMenu="matMenu"> <mat-menu matMenuContent #restrictionMenu="matMenu">
<ng-template let-state="state" matMenuContent> <ng-template let-state="state" matMenuContent>
<button mat-menu-item *ngFor="let level of accessLevels" (click)="onSetAccesLevel(level.level, state)"> <button mat-menu-item *ngFor="let restriction of restrictions" (click)="onSetRestriction(restriction.key, state)">
<mat-icon *ngIf="state.access_level === level.level">check</mat-icon> <mat-icon *ngIf="state.restriction.includes(restriction.key)">check</mat-icon>
<span>{{ level.label | translate }}</span> <span>{{ restriction.label | translate }}</span>
</button> </button>
</ng-template> </ng-template>
</mat-menu> </mat-menu>

View File

@ -41,18 +41,18 @@ interface StatePerm {
} }
/** /**
* Defines the structure of access levels * Defines the structure of AmendmentIntoFinal
*/ */
interface AccessLevel { interface AmendmentIntoFinal {
level: number; merge: MergeAmendment;
label: string; label: string;
} }
/** /**
* Defines the structure of access levels * Defines the structre of restrictions
*/ */
interface AmendmentIntoFinal { interface Restriction {
merge: MergeAmendment; key: string;
label: string; label: string;
} }
@ -108,20 +108,20 @@ export class WorkflowDetailComponent extends BaseViewComponent implements OnInit
type: 'check' type: 'check'
}, },
{ name: 'Show amendment in parent motoin', selector: 'merge_amendment_into_final', type: 'amendment' }, { name: 'Show amendment in parent motoin', selector: 'merge_amendment_into_final', type: 'amendment' },
{ name: 'Access level', selector: 'access_level', type: 'accessLevel' }, { name: 'Restrictions', selector: 'restriction', type: 'restriction' },
{ name: 'Label color', selector: 'css_class', type: 'color' }, { name: 'Label color', selector: 'css_class', type: 'color' },
{ name: 'Next states', selector: 'next_states_id', type: 'state' } { name: 'Next states', selector: 'next_states_id', type: 'state' }
] as StatePerm[]; ] as StatePerm[];
/** /**
* Determines possible access levels * Determines possible restrictions
*/ */
public accessLevels = [ public restrictions = [
{ level: 0, label: '0: All users' }, { key: 'managers_only', label: 'Managers only' },
{ level: 1, label: '1: Submitters, authorized users and managers' }, { key: 'motions.can_see_internal', label: 'Can see internal (perm)' },
{ level: 2, label: '2: Authorized users and managers for motions and metadata' }, { key: 'motions.can_manage_metadata', label: 'Can manage metadata (perm)' },
{ level: 3, label: '3: Only managers for motions' } { key: 'is_submitter', label: 'Is submitter' }
] as AccessLevel[]; ] as Restriction[];
/** /**
* Determines possible "Merge amendments into final" * Determines possible "Merge amendments into final"
@ -279,11 +279,27 @@ export class WorkflowDetailComponent extends BaseViewComponent implements OnInit
/** /**
* Sets an access level to the given workflow state * Sets an access level to the given workflow state
* *
* @param level The new access level * @param restrictions The new restrictions
* @param state the state to change * @param state the state to change
*/ */
public onSetAccesLevel(level: number, state: WorkflowState): void { public onSetRestriction(restriction: string, state: WorkflowState): void {
this.workflowRepo.updateState({ access_level: level }, state).then(() => {}, this.raiseError); const restrictions = state.restriction.map(r => r);
const restrictionIndex = restrictions.findIndex(r => r === restriction);
if (restrictionIndex < 0) {
restrictions.push(restriction);
} else {
restrictions.splice(restrictionIndex, 1);
}
this.workflowRepo.updateState({ restriction: restrictions }, state).then(() => {}, this.raiseError);
}
/**
* @returns the restriction label for the given restriction
*/
public getRestrictionLabel(restriction: string): string {
const entry = this.restrictions.find(r => r.key === restriction);
return entry ? entry.label : '';
} }
/** /**

View File

@ -246,7 +246,7 @@ export class MotionFilterListService extends BaseFilterListService<Motion, ViewM
// filter out restricted states for unpriviledged users // filter out restricted states for unpriviledged users
if ( if (
this.operator.hasPerms('motions.can_manage', 'motions.can_manage_metadata') || this.operator.hasPerms('motions.can_manage', 'motions.can_manage_metadata') ||
state.access_level === 0 state.restriction.length === 0
) { ) {
if (state.isFinalState) { if (state.isFinalState) {
finalStates.push(state.id); finalStates.push(state.id);

View File

@ -50,7 +50,10 @@ class MotionAccessPermissions(BaseAccessPermissions):
if value == "managers_only": if value == "managers_only":
# permission remains false # permission remains false
break break
elif value in ("motions.can_see_internal", "motions.can_manage_metadata"): elif value in (
"motions.can_see_internal",
"motions.can_manage_metadata",
):
if await async_has_perm(user_id, value): if await async_has_perm(user_id, value):
permission = True permission = True
break break

View File

@ -1,13 +1,13 @@
# Generated by Django 2.1.7 on 2019-03-20 15:49 # Generated by Django 2.1.7 on 2019-03-20 15:49
from django.db import migrations
import jsonfield.encoder import jsonfield.encoder
import jsonfield.fields import jsonfield.fields
from django.db import migrations
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [("motions", "0022_auto_20190320_0840")] dependencies = [("motions", "0023_remove_motion_log")]
operations = [ operations = [
migrations.AddField( migrations.AddField(

View File

@ -1,8 +1,6 @@
# Generated by Django 2.1.7 on 2019-03-20 15:49 # Generated by Django 2.1.7 on 2019-03-20 15:49
from django.db import migrations from django.db import migrations
import jsonfield.encoder
import jsonfield.fields
def copy_access_level(apps, schema_editor): def copy_access_level(apps, schema_editor):
@ -31,6 +29,6 @@ def copy_access_level(apps, schema_editor):
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [("motions", "0023_state_restriction_1")] dependencies = [("motions", "0024_state_restriction_1")]
operations = [migrations.RunPython(copy_access_level)] operations = [migrations.RunPython(copy_access_level)]

View File

@ -5,6 +5,6 @@ from django.db import migrations
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [("motions", "0023_state_restriction_2")] dependencies = [("motions", "0024_state_restriction_2")]
operations = [migrations.RemoveField(model_name="state", name="access_level")] operations = [migrations.RemoveField(model_name="state", name="access_level")]

View File

@ -14,6 +14,7 @@ from ..utils.rest_api import (
Field, Field,
IdPrimaryKeyRelatedField, IdPrimaryKeyRelatedField,
IntegerField, IntegerField,
JSONField,
ModelSerializer, ModelSerializer,
SerializerMethodField, SerializerMethodField,
ValidationError, ValidationError,
@ -95,6 +96,8 @@ class StateSerializer(ModelSerializer):
Serializer for motion.models.State objects. Serializer for motion.models.State objects.
""" """
restriction = JSONField()
class Meta: class Meta:
model = State model = State
fields = ( fields = (
@ -123,17 +126,20 @@ class StateSerializer(ModelSerializer):
"title": "Motion workflow state restriction field schema", "title": "Motion workflow state restriction field schema",
"description": "An array containing one or more explicit strings to control restriction for motions in this state.", "description": "An array containing one or more explicit strings to control restriction for motions in this state.",
"type": "array", "type": "array",
"enum": [ "items": {
"motions.can_see_internal", "type": "string",
"motions.can_manage_metadata", "enum": [
"is_submitter", "motions.can_see_internal",
"managers_only", "motions.can_manage_metadata",
], "is_submitter",
"managers_only",
],
},
} }
# Validate value. # Validate value.
try: try:
jsonschema.validate(request.data, schema) jsonschema.validate(value, schema)
except jsonschema.ValidationError as err: except jsonschema.ValidationError as err:
raise ValidationError({"detail": str(err)}) raise ValidationError({"detail": str(err)})
return value return value