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 recommendation_label: string;
public css_class: string;
public access_level: number;
public restriction: string[];
public allow_support: boolean;
public allow_create_poll: boolean;
public allow_submitter_edit: boolean;

View File

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

View File

@ -41,18 +41,18 @@ interface StatePerm {
}
/**
* Defines the structure of access levels
* Defines the structure of AmendmentIntoFinal
*/
interface AccessLevel {
level: number;
interface AmendmentIntoFinal {
merge: MergeAmendment;
label: string;
}
/**
* Defines the structure of access levels
* Defines the structre of restrictions
*/
interface AmendmentIntoFinal {
merge: MergeAmendment;
interface Restriction {
key: string;
label: string;
}
@ -108,20 +108,20 @@ export class WorkflowDetailComponent extends BaseViewComponent implements OnInit
type: 'check'
},
{ 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: 'Next states', selector: 'next_states_id', type: 'state' }
] as StatePerm[];
/**
* Determines possible access levels
* Determines possible restrictions
*/
public accessLevels = [
{ level: 0, label: '0: All users' },
{ level: 1, label: '1: Submitters, authorized users and managers' },
{ level: 2, label: '2: Authorized users and managers for motions and metadata' },
{ level: 3, label: '3: Only managers for motions' }
] as AccessLevel[];
public restrictions = [
{ key: 'managers_only', label: 'Managers only' },
{ key: 'motions.can_see_internal', label: 'Can see internal (perm)' },
{ key: 'motions.can_manage_metadata', label: 'Can manage metadata (perm)' },
{ key: 'is_submitter', label: 'Is submitter' }
] as Restriction[];
/**
* 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
*
* @param level The new access level
* @param restrictions The new restrictions
* @param state the state to change
*/
public onSetAccesLevel(level: number, state: WorkflowState): void {
this.workflowRepo.updateState({ access_level: level }, state).then(() => {}, this.raiseError);
public onSetRestriction(restriction: string, state: WorkflowState): void {
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
if (
this.operator.hasPerms('motions.can_manage', 'motions.can_manage_metadata') ||
state.access_level === 0
state.restriction.length === 0
) {
if (state.isFinalState) {
finalStates.push(state.id);

View File

@ -50,7 +50,10 @@ class MotionAccessPermissions(BaseAccessPermissions):
if value == "managers_only":
# permission remains false
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):
permission = True
break

View File

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

View File

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

View File

@ -5,6 +5,6 @@ from django.db import migrations
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")]

View File

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