def update(self, request, *args, **kwargs): if request.data is None: return rc.BAD_REQUEST pkfield = kwargs.get(self.model._meta.pk.name) or request.data.get( self.model._meta.pk.name) if not pkfield: return rc.BAD_REQUEST item = getOrNone(self.model, pkfield) if not item: return rc.NOT_FOUND if not request.user.profile.has_permission(item, mode="w"): return rc.FORBIDDEN attrs = self.flatten_dict(request) form = ContactForm(contact_type=item.contact_type, instance=item, **attrs) if form.is_valid(): item = form.save(request) return item else: self.status = 400 return form.errors
def create(self, request, *args, **kwargs): if request.data is None: return rc.BAD_REQUEST contact_type = getOrNone(ContactType, request.data.get('contact_type')) if not contact_type or not request.user.profile.has_permission( contact_type, mode='x'): return rc.FORBIDDEN attrs = self.flatten_dict(request) form = ContactForm(contact_type=contact_type, **attrs) if form.is_valid(): contact = form.save(request, contact_type) contact.set_user_from_request(request) return contact else: self.status = 400 return form.errors