Beispiel #1
0
 def mutate(self, info, **kwargs):
     # Validate if the country given is a valid country
     validate_country_field(**kwargs)
     validate_timezone_field(**kwargs)
     validate_url(**kwargs)
     validate_empty_fields(**kwargs)
     location = LocationModel(**kwargs)
     admin = get_user_from_db()
     email = admin.email
     username = email.split("@")[0]
     admin_name = username.split(".")[0]
     subject = 'A new location has been added'
     template = 'location_success.html'
     payload = {
         'model': LocationModel,
         'field': 'name',
         'value': kwargs['name']
     }
     with SaveContextManager(location, 'Location', payload):
         if not notification.send_email_notification(
                 email=email,
                 subject=subject,
                 template=template,
                 location_name=location.name,
                 user_name=admin_name):
             raise GraphQLError("Location created but email not sent")
         return CreateLocation(location=location)
Beispiel #2
0
    def mutate(self, info, **kwargs):

        user_id = return_logged_in_users_id()
        party_id = kwargs['party_id']
        office_id = kwargs['office_id']

        candidate = CandidateModel(office_id=office_id,
                                   party_id=party_id,
                                   user_id=user_id)

        filter_office_id(info, office_id)
        filter_party_id(info, party_id)
        query = Candidate.get_query(info)

        party_has_candidate = query.filter(
            CandidateModel.party_id == party_id,
            CandidateModel.office_id == office_id).first()
        candidate_query = query.filter(
            CandidateModel.user_id == user_id).first()
        if candidate_query:
            raise GraphQLError('this candidate is already running for office')
        if party_has_candidate:
            raise GraphQLError('party already has a candidate')

        payload = {
            'model': CandidateModel,
            'field': 'user_id',
            'value': user_id
        }
        with SaveContextManager(candidate, 'Candidate name', payload):
            return CreateCandidate(candidate=candidate)
Beispiel #3
0
 def mutate(self, info, **kwargs):
     resource = ResourceModel(**kwargs)
     payload = {
         'model': ResourceModel, 'field': 'name', 'value':  kwargs['name']
     }
     with SaveContextManager(
         resource, 'Resource', payload
     ):
         return CreateResource(resource=resource)
Beispiel #4
0
 def mutate(self, info, **kwargs):
     role = RoleModel(**kwargs)
     payload = {
         'model': RoleModel,
         'field': 'role',
         'value': kwargs['role']
     }
     with SaveContextManager(role, 'Role', payload):
         return CreateRole(role=role)
Beispiel #5
0
 def mutate(self, info, email, **kwargs):
     query_user = User.get_query(info)
     exact_user = query_user.filter(UserModel.email == email).first()
     if not exact_user:
         raise GraphQLError("User not found")
     user_role = UsersRole.query.filter_by(user_id=exact_user.id).first()
     new_role = kwargs.pop('role_id')
     user_role.role_id = new_role
     with SaveContextManager(user_role, 'Role id', new_role):
         return ChangeUserRole(user=exact_user)
Beispiel #6
0
 def mutate(self, info, wing_id, **kwargs):
     validate_empty_fields(**kwargs)
     get_wing = Wing.get_query(info)
     exact_wing = get_wing.filter(WingModel.id == wing_id).first()
     if not exact_wing:
         raise GraphQLError("Wing not found")
     admin_roles.create_update_delete_wing()
     update_entity_fields(exact_wing, **kwargs)
     with SaveContextManager(exact_wing, kwargs['name'], 'Wing'):
         return UpdateWing(wing=exact_wing)
Beispiel #7
0
 def mutate(self, info, **kwargs):
     validate_empty_fields(**kwargs)
     tag = TagModel(**kwargs)
     payload = {
         'model': TagModel, 'field': 'name', 'value':  kwargs['name']
         }
     with SaveContextManager(
        tag, 'Tag', payload
     ):
         return CreateTag(tag=tag)
Beispiel #8
0
 def mutate(self, info, **kwargs):
     validate_empty_fields(**kwargs)
     floor = Floor.query.filter_by(id=kwargs['floor_id']).first()
     if not floor:
         raise GraphQLError("Floor not found")
     admin_roles.check_office_location_create_wing(floor_id=kwargs['floor_id'])  # noqa: E501
     admin_roles.create_update_delete_wing()
     wing = WingModel(**kwargs)
     with SaveContextManager(wing, kwargs['name'], 'Wing'):
         return CreateWing(wing=wing)
Beispiel #9
0
 def mutate(self, info, **kwargs):
     location = Location.query.filter_by(id=kwargs['location_id']).first()
     if not location:
         raise GraphQLError("Location not found")
     admin_roles.create_office(location_id=kwargs['location_id'])
     office = OfficeModel(**kwargs)
     with SaveContextManager(office, kwargs['name'], 'Office'):
         new_office = kwargs['name']
         if not office_created(new_office):
             raise GraphQLError("Office created but Emails not Sent")
         return CreateOffice(office=office)
Beispiel #10
0
    def mutate(self, info, **kwargs):
        validate_entity_types(['state', 'legislative', 'federal', 'local_govenment'], 'office_type', **kwargs)  # noqa
        office = OfficeModel(**kwargs)

        payload = {
            'model': OfficeModel,
            'field': 'office_name',
            'value': kwargs['office_name']
        }
        with SaveContextManager(office, 'Office name', payload):
            return CreateOffice(office=office)
Beispiel #11
0
 def mutate(self, info, **kwargs):
     validate_empty_fields(**kwargs)
     validate_question_type(**kwargs)
     validate_date_time_range(**kwargs)
     question = QuestionModel(**kwargs)
     payload = {
         'model': QuestionModel,
         'field': 'question',
         'value': kwargs['question']
     }
     with SaveContextManager(question, 'Question', payload):
         return CreateQuestion(question=question)
Beispiel #12
0
 def mutate(self, info, **kwargs):
     user = UserModel(**kwargs)
     if not verify_email(user.email):
         raise GraphQLError("This email is not allowed")
     payload = {
         'model': UserModel,
         'field': 'email',
         'value': kwargs['email']
     }
     with SaveContextManager(user, 'User email', payload):
         notification_settings = NotificationModel(user_id=user.id)
         notification_settings.save()
         return CreateUser(user=user)
Beispiel #13
0
    def mutate(self, info, **kwargs):
        validate_entity_types(['admin', 'citizen', 'politician'], 'user_type',
                              **kwargs)  # noqa
        user = UserModel(**kwargs)

        if not verify_email(user.email):
            raise GraphQLError("This email is not allowed")
        payload = {
            'model': UserModel,
            'field': 'email',
            'value': kwargs['email']
        }
        with SaveContextManager(user, 'User email', payload):
            token = user.generate_token()
            return CreateUser(user=user, token=token)
Beispiel #14
0
 def mutate(self, info, **kwargs):
     validate_empty_fields(**kwargs)
     if 'parent_id' in kwargs:
         validate_parent_node_id(parent_id=kwargs['parent_id'])
     if 'tag_id' in kwargs:
         verify_tag_id(tag_id=kwargs['tag_id'])
     node = OfficeStructureModel(**kwargs)
     payload = {
         'model': OfficeStructureModel,
         'field': 'name',
         'value':  kwargs['name']
         }
     with SaveContextManager(
       node, 'node level', payload
     ):
         return CreateNode(node=node)
Beispiel #15
0
    def mutate(self, info, **kwargs):
        query_user = User.get_query(info)
        existing_user = query_user.filter_by(email=kwargs['email']).first()

        if existing_user:
            token = existing_user.generate_token()
            return CreateUser(user=existing_user, token=token)
        user = UserModel(**kwargs)
        if user.email and not verify_email(user.email):
            raise GraphQLError("This email is not allowed")
        payload = {
            'model': UserModel,
            'field': 'email',
            'value': kwargs['email'],  # noqa
        }
        with SaveContextManager(user, 'email', payload):
            token = user.generate_token()
            return CreateUser(user=user, token=token)
Beispiel #16
0
 def mutate(self, info, **kwargs):
     validate_empty_fields(**kwargs)
     validate_question_type(**kwargs)
     validate_date_time_range(**kwargs)
     question_type = kwargs['question_type']
     fields = kwargs
     if question_type == "check" and not kwargs.get('check_options'):
         return GraphQLError(
             "No check options supplied for question type check")
     if question_type != "check":
         fields['check_options'] = None
     payload = {
         'model': QuestionModel,
         'field': 'question',
         'value': kwargs['question']
     }
     question = QuestionModel(**fields)
     with SaveContextManager(question, 'Question', payload):
         return CreateQuestion(question=question)
Beispiel #17
0
 def mutate(self, info, **kwargs):
     location = Location.query.filter_by(id=kwargs['location_id']).first()
     if not location:
         raise GraphQLError("Location not found")
     admin_roles.verify_admin_location(location_id=kwargs['location_id'])
     office = OfficeModel(**kwargs)
     admin = get_user_from_db()
     email = admin.email
     username = email.split("@")[0]
     admin_name = username.split(".")[0]
     payload = {
         'model': OfficeModel, 'field': 'name', 'value':  kwargs['name']
         }
     with SaveContextManager(
        office, 'Office', payload
     ):
         new_office = kwargs['name']
         if not send_email_notification(email, new_office, location.name, admin_name):  # noqa
             raise GraphQLError("Office created but Emails not Sent")
         return CreateOffice(office=office)
Beispiel #18
0
 def mutate(self, info, **kwargs):
     role = RoleModel(**kwargs)
     with SaveContextManager(role, kwargs.get('role'), 'Role'):
         return CreateRole(role=role)
Beispiel #19
0
 def mutate(self, info, **kwargs):
     user = UserModel(**kwargs)
     with SaveContextManager(user, kwargs.get('email'), 'User email'):
         notification_settings = NotificationModel(user_id=user.id)
         notification_settings.save()
         return CreateUser(user=user)