This repository was archived by the owner on Nov 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathsection.py
More file actions
149 lines (112 loc) · 5.44 KB
/
Copy pathsection.py
File metadata and controls
149 lines (112 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import graphene
from graphene_sqlalchemy import SQLAlchemyObjectType
from graphene.relay import Node
import assembl.graphql.docstrings as docs
from assembl import models
from assembl.auth import CrudPermissions
from assembl.models.section import SectionTypesEnum
from .langstring import (LangStringEntry, LangStringEntryInput,
langstring_from_input_entries, resolve_langstring,
resolve_langstring_entries,
update_langstring_from_input_entries)
from .permissions_helpers import require_cls_permission, require_instance_permission
from .types import SecureObjectType
from .utils import abort_transaction_on_exception
SectionTypes = graphene.Enum.from_enum(SectionTypesEnum)
class Section(SecureObjectType, SQLAlchemyObjectType):
__doc__ = docs.Section.__doc__
class Meta:
model = models.Section
interfaces = (Node, )
only_fields = ('id', 'section_type', 'order')
title = graphene.String(lang=graphene.String(docs.Default.required_language_input),
description=docs.Section.title)
title_entries = graphene.List(LangStringEntry, description=docs.Section.title_entries)
url = graphene.String(description=docs.Section.url)
def resolve_title(self, args, context, info):
title = resolve_langstring(self.title, args.get('lang'))
return title
def resolve_title_entries(self, args, context, info):
return resolve_langstring_entries(self, 'title')
class CreateSection(graphene.Mutation):
__doc__ = docs.CreateSection.__doc__
class Input:
title_entries = graphene.List(LangStringEntryInput, required=True, description=docs.CreateSection.title_entries)
section_type = graphene.Argument(SectionTypes, description=docs.CreateSection.section_type)
url = graphene.String(description=docs.CreateSection.url)
order = graphene.Float(description=docs.CreateSection.order)
section = graphene.Field(lambda: Section)
@staticmethod
@abort_transaction_on_exception
def mutate(root, args, context, info):
cls = models.Section
discussion_id = context.matchdict['discussion_id']
require_cls_permission(CrudPermissions.CREATE, cls, context)
with cls.default_db.no_autoflush as db:
title_entries = args.get('title_entries')
if len(title_entries) == 0:
raise Exception(
'Resource titleEntries needs at least one entry')
# Better to have this message than
# 'NoneType' object has no attribute 'owner_object'
# when creating the saobj below if title=None
title_langstring = langstring_from_input_entries(title_entries)
kwargs = {}
kwargs['section_type'] = args.get('section_type', 'CUSTOM')
kwargs['url'] = args.get('url')
kwargs['order'] = args.get('order', 10.0)
saobj = cls(
discussion_id=discussion_id,
title=title_langstring,
**kwargs)
db.add(saobj)
db.flush()
return CreateSection(section=saobj)
class DeleteSection(graphene.Mutation):
__doc__ = docs.DeleteSection.__doc__
class Input:
section_id = graphene.ID(required=True, description=docs.DeleteSection.section_id)
success = graphene.Boolean()
@staticmethod
def mutate(root, args, context, info):
section_id = args.get('section_id')
section_id = int(Node.from_global_id(section_id)[1])
section = models.Section.get(section_id)
if section.section_type != SectionTypesEnum.CUSTOM.value:
return DeleteSection(success=False)
require_instance_permission(CrudPermissions.DELETE, section, context)
section.db.delete(section)
section.db.flush()
return DeleteSection(success=True)
class UpdateSection(graphene.Mutation):
__doc__ = docs.UpdateSection.__doc__
class Input:
id = graphene.ID(required=True, description=docs.UpdateSection.id)
title_entries = graphene.List(LangStringEntryInput, description=docs.UpdateSection.title_entries)
url = graphene.String(description=docs.UpdateSection.url)
order = graphene.Float(description=docs.UpdateSection.order)
section = graphene.Field(lambda: Section)
@staticmethod
def mutate(root, args, context, info):
cls = models.Section
section_id = args.get('id')
section_id = int(Node.from_global_id(section_id)[1])
section = cls.get(section_id)
require_instance_permission(CrudPermissions.UPDATE, section, context)
with cls.default_db.no_autoflush as db:
title_entries = args.get('title_entries')
if title_entries is not None and len(title_entries) == 0:
raise Exception(
'section titleEntries needs at least one entry')
# Better to have this message than
# 'NoneType' object has no attribute 'owner_object'
# when creating the saobj below if title=None
update_langstring_from_input_entries(
section, 'title', title_entries)
kwargs = {}
kwargs['url'] = args.get('url', None)
kwargs['order'] = args.get('order', 10.0)
for attr, value in kwargs.items():
setattr(section, attr, value)
db.flush()
return UpdateSection(section=section)