def post(self, ticket_id = None, action = None): if ticket_id is None: "Create a new Survey Ticket" args = self.create_tkt_args() u_ids = args.get('unit_ids', '').split(',') if len(u_ids) == 0: raise APIException("Survey Units MUST be specified", 400) root_svey = api_get_object(Survey.root, args.get('srvy_id')) c_user = User.objects(id = current_user.id).first() if not c_user in root_svey.created_by: raise APIException("Only Root Survey owner may create", 400) units = [api_get_object(SurveyUnit.objects, _) for _ in u_ids] if not set(units).issubset(root_svey.units_as_objects): raise APIException("Must be Units of the Parent Survey", 400) tkt = SurveyTicket() tkt.destined = list(set(sum([_.created_by for _ in units], []))) tkt.origin = User.objects(id = current_user.id).first() tkt.survey_unit = list(set(units)) tkt.payload = { 'original_msg': args.get('tkt_msg'), 'complete': {}, } tkt.save() return tkt.repr tkt = api_get_object(SurveyTicket.objects, ticket_id) if action == "resolve": "Resolve the ticket." tkt.resolve() tkt.save() return tkt.repr elif action == "add_comment": "Add a Comment on the Ticket." swag = self.add_comment_args() c_user = User.objects(id = current_user.id).first() if not c_user in reduce(lambda x, y: x + y, [_.created_by for _ in tkt.survey_unit], []): raise APIException("Must be a member of Survey", 400) cid = tkt.add_comment(swag['msg'], c_user) tkt.save() return tkt.repr raise APIException("Must specify valid option", 400)
def post(self, ticket_id = None, action = None): if ticket_id is None: "Create a new Survey Ticket" args = self.create_tkt_args() u_ids = args.get('unit_ids', '').split(',') if len(u_ids) == 0: raise APIException("Survey Units MUST be specified", 400) root_svey = api_get_object(Survey.root, args.get('srvy_id')) c_user = User.objects(id = current_user.id).first() if not c_user in root_svey.created_by: raise APIException("Only Root Survey owner may create", 400) units = [api_get_object(SurveyUnit.objects, _) for _ in u_ids] if not set(units).issubset(root_svey.units_as_objects): raise APIException("Must be Units of the Parent Survey", 400) tkt = SurveyTicket() tkt.destined = list(set(sum([_.created_by for _ in units], []))) tkt.origin = User.objects(id = current_user.id).first() tkt.survey_unit = list(set(units)) tkt.payload = { 'original_msg': args.get('tkt_msg'), 'complete': {}, } tkt.save() return tkt.repr tkt = api_get_object(SurveyTicket.objects, ticket_id) if action == "mark_done": pass raise APIException("Must specify valid option", 400)
def post(self, notification_id, action): notf = api_get_object(Notification.objects, notification_id) if action == "add_comment": swag = self.add_comment_args() c_user = User.objects(id = current_user.id).first() cid = notf.add_comment(swag['msg'], c_user) notf.save() return notf.repr elif action == "resolve": notf.flagged = False notf.save() return notf.repr raise APIException("Must specify valid option", 400)
def post(self): args = self.post_args() svey = Survey() usr = User.objects(id = current_user.id).first() svey.metadata['name'] = args['s_name'] struct_dict = starter_template opt = [] for option in args['s_tags']: opt.append({ 'checked': False, 'label': option }) struct_dict['fields'][0]['field_options']['options'] = opt svey.struct = struct_dict svey.created_by.append(usr) svey.save() return svey.repr, 200
def post(self): args = self.post_args() svey = Survey() usr = User.objects(id = current_user.id).first() svey.metadata['name'] = args['s_name'] # struct_dict = starter_template # opt = [] # for option in args['s_tags']: # opt.append({ # 'checked': False, # 'label': option # }) # struct_dict['fields'][0]['field_options']['options'] = opt # svey.struct = struct_dict svey.created_by.append(usr) svey.save() return svey.repr, 200
def post(self, survey_id, action): svey = api_get_object(Survey.objects, survey_id) if svey.hidden: raise APIException("This Survey has been deleted", 404) if action == 'struct': args = self.post_args() svey.struct = json.loads(args['swag']) svey.save() ret = { 'id': str(svey), 'saved': True, } return ret, 200 elif action == 'expires': args = self.post_args() dat = args['swag'] svey.expires = dateutil.parser.parse(dat) svey.save() ret = { 'id': str(svey), 'field': action, 'saved': True, } return ret, 200 elif action == 'paused': args = self.post_args() dat = json.loads(args['swag']) if type(dat) is bool: svey.paused = dat svey.save() ret = { 'id': str(svey), 'field': action, 'saved': True, } return ret, 200 raise APIException("TypeError: Invalid swag value.", 400) elif action == 'response_cap': args = self.post_args() dat = json.loads(args['swag']) if type(dat) is int: svey.response_cap = dat svey.save() ret = { 'id': str(svey), 'field': action, 'saved': True, } return ret, 200 raise APIException("TypeError: Invalid swag value.", 400) elif action == 'unit_name': if type(svey) is not SurveyUnit: raise APIException("Action valid for Survey Units only.", 400) args = self.post_args() dat = args['swag'] if not len(dat) > 0: raise APIException("TypeError: Invalid swag value.", 400) svey.unit_name = dat svey.save() ret = { 'id': str(svey), 'field': action, 'saved': True, } return ret, 200 elif action == 'unit_addition': if type(svey) is SurveyUnit: raise APIException("Action valid for Root Survey only.", 400) args = self.post_args() dat = args['swag'] if not len(dat) > 0: raise APIException("TypeError: Invalid swag value.", 400) usvey = SurveyUnit() usr = User.objects(id = current_user.id).first() usvey.unit_name = dat usvey.referenced = svey usvey.created_by.append(usr) usvey.save() usvey.reload() ret = { 'id': str(svey), 'unit': usvey.repr_sm, 'saved': True, } return ret, 200 elif action == 'survey_name': args = self.post_args() dat = args['swag'] if len(dat) > 0: svey.metadata['name'] = dat svey.save() ret = { 'id': str(svey), 'field': action, 'saved': True, } return ret, 200 raise APIException("TypeError: Invalid swag value.", 400) elif action == 'img_upload': try: uploaded_img = AttachmentImage() usr = User.objects(id = current_user.id).first() uploaded_img.owner = usr uploaded_img.file = request.files['swag'] uploaded_img.save() svey.attachments.append(uploaded_img) svey.save() ret = { 'id': str(svey), 'field': action, 'access_id': str(uploaded_img), 'temp_uri': uploaded_img.file, 'metadata': uploaded_img.repr, 'saved': True, } return ret, 200 except Exception as e: raise APIException("Upload Error; {0}".format(str(e)), 400) elif action == 'share': args = self.post_args() dat = args['swag'] try: usr = User.objects.get(email = dat) grp = set(svey.created_by) grp.add(usr) svey.created_by = list(grp) svey.save() ret = { 'id': str(svey), 'action': action, 'user': usr.repr, 'survey': svey.repr_sm } return ret, 200 except DoesNotExist: try: ftract = SurveySharePromise.objects( future_email = dat, future_survey = svey ).first() if not ftract or ftract.active is False: raise ValueError except ValueError: ftract = SurveySharePromise() ftract.future_email = dat ftract.future_survey = svey ftract.save() #: Send the Email Here. Send the URL by `ftract.url` attr. finally: ret = { 'id': str(svey), 'action': action, 'scheduled': True, 'message': ('The user will be added to the survey after ' 'they sign up on the website.'), 'promise': ftract.repr } return ret, 201 raise APIException("Must specify a valid option", 400)
def user(self): return User.objects(email=self.user_id.id).first()
def user(self): return User.objects(email = self.user_id.id).first()
def post(self, survey_id, action): svey = api_get_object(Survey.objects, survey_id) if svey.hidden: raise APIException("This Survey has been deleted", 404) if action == 'struct': args = self.post_args() svey.struct = json.loads(args['swag']) svey.save() ret = { 'id': str(svey), 'saved': True, } return ret, 200 elif action == 'expires': args = self.post_args() dat = args['swag'] svey.expires = dateutil.parser.parse(dat) svey.save() ret = { 'id': str(svey), 'field': action, 'saved': True, } return ret, 200 elif action == 'paused': args = self.post_args() dat = json.loads(args['swag']) if type(dat) is bool: svey.paused = dat svey.save() ret = { 'id': str(svey), 'field': action, 'saved': True, } return ret, 200 raise APIException("TypeError: Invalid swag value.", 400) elif action == 'response_cap': args = self.post_args() dat = json.loads(args['swag']) if type(dat) is int: svey.response_cap = dat svey.save() ret = { 'id': str(svey), 'field': action, 'saved': True, } return ret, 200 raise APIException("TypeError: Invalid swag value.", 400) elif action == 'unit_name': if type(svey) is not SurveyUnit: raise APIException("Action valid for Survey Units only.", 400) args = self.post_args() dat = args['swag'] if not len(dat) > 0: raise APIException("TypeError: Invalid swag value.", 400) svey.unit_name = dat svey.save() ret = { 'id': str(svey), 'field': action, 'saved': True, } return ret, 200 elif action == 'unit_addition': if type(svey) is SurveyUnit: raise APIException("Action valid for Root Survey only.", 400) args = self.post_args() dat = args['swag'] if not len(dat) > 0: raise APIException("TypeError: Invalid swag value.", 400) usvey = SurveyUnit() usr = User.objects(id = current_user.id).first() usvey.unit_name = dat usvey.referenced = svey usvey.created_by.append(usr) usvey.save() usvey.reload() ret = { 'id': str(svey), 'unit': usvey.repr_sm, 'saved': True, } return ret, 200 elif action == 'survey_name': args = self.post_args() dat = args['swag'] if len(dat) > 0: svey.metadata['name'] = dat svey.save() ret = { 'id': str(svey), 'field': action, 'saved': True, } return ret, 200 raise APIException("TypeError: Invalid swag value.", 400) elif action == 'img_upload': try: uploaded_img = AttachmentImage() usr = User.objects(id = current_user.id).first() uploaded_img.owner = usr uploaded_img.file = request.files['swag'] uploaded_img.save() svey.attachments.append(uploaded_img) svey.save() ret = { 'id': str(svey), 'field': action, 'access_id': str(uploaded_img), 'temp_uri': uploaded_img.file, 'metadata': uploaded_img.repr, 'saved': True, } return ret, 200 except Exception as e: raise APIException("Upload Error; {0}".format(str(e)), 400) raise APIException("Must specify a valid option", 400)