def sendMessageFromiOS(request): print 'sendMessageFromiOS' try: obj = simplejson.loads(request.raw_post_data) print obj msg = obj["message"] message_text = msg["messageText"] group = Group.objects.get(pk=obj["groupID"]) recipients = extract_recipients(request.user, group, message_text) if "error" in recipients: # send error message back to sender send_message(recipients["error"], None, user, group) data = {"errorMessage": recipients["error"]} return HttpResponse(simplejson.dumps(error_code(50, data)), mimetype='application/json') recipients = recipients["recipients"] # Create Message model instance and send out message # TODO decomp this code chunk message = Message(message_text=message_text, sender=request.user, group=group) create_ios_push_notification(message) message.save() for group_link in recipients: recipient = group_link.user message_link = MessageUserLink(message=message, recipient=recipient) message_link.save() send_message(message_text, request.user, recipient, group) data = {"messageID": message.id} return error_code(0, data) except Exception, e: print e traceback.print_exc() return error_code(1)
def delete_detail(self, request, **kwargs): try: http_request = super(PlanamoModelResource, self).delete_detail( request, **kwargs) if isinstance(http_request, http.HttpNoContent): return error_code(0) else: return error_code(12) except: return error_code(1) # unknown error
def dispatch_detail(self, request, **kwargs): user_pk = kwargs.pop('user_pk') try: kwargs['user'] = User.objects.get(pk=user_pk) except: return error_code(21) return super(GroupUserResource, self).dispatch_detail(request, **kwargs)
def dispatch(self, request_type, request, **kwargs): group_pk = kwargs.pop('group_pk') try: kwargs['group'] = Group.objects.get(pk=group_pk) except: return error_code(31) return super(GroupUserResource, self).dispatch(request_type, request, **kwargs)
def dispatch(self, request_type, request, **kwargs): out = super(PlanamoModelResource, self).dispatch(request_type, request, **kwargs) # Tastypie-generated successful JSON responses will not have the desired # '"code": 0' attribute meaning success try: data = simplejson.loads(out.content) if 'code' not in data: out = error_code(0, data) except Exception, e: print 'response content not in JSON format' print e
def put_list(self, request, **kwargs): """ Put list """ try: deserialized = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json')) deserialized = self.alter_deserialized_list_data(request, deserialized) # pseudo-hydrate a Group bundle to populate the GroupUserLink objects group_resource = GroupResource() bundle = Bundle(obj=kwargs['group'], data=deserialized, request=request) bundle = group_resource.hydrate_m2m_users(bundle) group_resource.save_m2m_users(bundle) data = complete_dehydrate(bundle.data) data["code"] = 0 return HttpResponse(self._meta.serializer.to_json(data), mimetype='application/json') except Exception, e: print 'put_list error' print traceback.print_exc() return error_code(1)
def post_list(self, request, **kwargs): """ Adapted from tastypie/resources.py ``post_list`` Modified to handle case when obj_create returns None """ deserialized = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json')) deserialized = self.alter_deserialized_detail_data(request, deserialized) bundle = self.build_bundle(data=dict_strip_unicode_keys(deserialized), request=request) self.is_valid(bundle, request) updated_bundle = self.obj_create(bundle, request=request, **self.remove_api_resource_names(kwargs)) if updated_bundle is None: # error occurred with obj_create return error_code(1) location = self.get_resource_uri(updated_bundle) if not self._meta.always_return_data: return http.HttpCreated(location=location) else: updated_bundle = self.full_dehydrate(updated_bundle) updated_bundle = self.alter_detail_data_to_serialize(request, updated_bundle) return self.create_response(request, updated_bundle, response_class=http.HttpCreated, location=location)