forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_organization.py
More file actions
111 lines (95 loc) · 3.95 KB
/
create_organization.py
File metadata and controls
111 lines (95 loc) · 3.95 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
import graphene
from graphql import GraphQLError
from app import logger
from db import db_session
from functions.auth_wrappers import require_token
from functions.auth_functions import is_super_admin
from functions.input_validators import cleanse_input
from functions.slugify import slugify_value
from models import Organizations
from scalars.organization_acronym import Acronym
class CreateOrganizationInput(graphene.InputObjectType):
"""
Input object type containing all the fields for the create organization
mutation
"""
acronym = Acronym(description="Acronym of organization.", required=True)
name = graphene.String(description="Full name of organization.", required=True)
zone = graphene.String(
description="The zone which the organization belongs to.", required=True
)
sector = graphene.String(
description="The sector which the organization belongs to.", required=True
)
province = graphene.String(
description="The province in which the organization is located in.",
required=True,
)
city = graphene.String(
description="The city in which the organization is located in.", required=True,
)
class CreateOrganization(graphene.Mutation):
"""
Mutation allows the creation of an organization inside the database.
"""
class Arguments:
input = CreateOrganizationInput(
required=True,
description="Input object containing all the fields required for"
" the createOrganization mutation",
)
# If the update passed or failed
status = graphene.Boolean()
@require_token
def mutate(self, info, **kwargs):
user_id = kwargs.get("user_id")
user_roles = kwargs.get("user_roles")
name = cleanse_input(kwargs.get("input", {}).get("name"))
acronym = cleanse_input(kwargs.get("input", {}).get("acronym"))
zone = cleanse_input(kwargs.get("input", {}).get("zone"))
sector = cleanse_input(kwargs.get("input", {}).get("sector"))
province = cleanse_input(kwargs.get("input", {}).get("province"))
city = cleanse_input(kwargs.get("input", {}).get("city"))
if is_super_admin(user_roles=user_roles):
# Check to see if organization already exists
slug = slugify_value(name)
org_orm = (
db_session.query(Organizations)
.filter(Organizations.slug == slug)
.first()
)
if org_orm is not None:
logger.warning(
f"Super admin: {user_id} tried to create an organization, but it already exists."
)
raise GraphQLError("Error, unable to create organization.")
# Generate org tags
org_tags = {
"zone": zone,
"sector": sector,
"province": province,
"city": city,
}
# Create new org entry in db
new_org = Organizations(name=name, acronym=acronym, org_tags=org_tags)
# Add new org entry into the session
db_session.add(new_org)
# Push update to db and return status
try:
db_session.commit()
logger.info(
f"Super admin: {user_id} successfully created a new org: {new_org.slug}."
)
return CreateOrganization(status=True)
except Exception as e:
db_session.rollback()
db_session.flush()
logger.error(
f"Database error occured when {user_id} tried to create a new organization {str(e)}"
)
raise GraphQLError("Error, unable to create organization.")
else:
logger.warning(
f"User: {user_id} tried to create a new organization, but is not a super admin."
)
raise GraphQLError("Error, unable to create organization.")