Skip to content

Instantly share code, notes, and snippets.

@ngokevin
Created January 13, 2014 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngokevin/8408474 to your computer and use it in GitHub Desktop.
Save ngokevin/8408474 to your computer and use it in GitHub Desktop.
def create(self, request, *args, **kwargs):
thread = get_object_or_404(CommunicationThread, id=kwargs['thread_id'])
# Validate note.
form = forms.CreateCommNoteForm(request.DATA)
if not form.is_valid():
return Response(form.errors, status=status.HTTP_400_BAD_REQUEST)
# Validate attachment.
attachment_formset = None
if request.FILES:
attachment_formset = forms.CommAttachmentFormSet(
data=request.POST or None, files=request.FILES or None,
prefix='attachment')
if not attachment_formset.is_valid():
return Response(attachment_formset.errors,
status=status.HTTP_400_BAD_REQUEST)
# Create notes.
thread, note = create_comm_note(
thread.addon, thread.version, self.request.amo_user,
form.cleaned_data['body'],
note_type=form.cleaned_data['note_type'])
self.attach_as_reply(note)
# Create attachment.
if attachment_formset:
create_attachments(note, attachment_formset)
return Response(
NoteSerializer(note, context={'request': request}).data,
status=status.HTTP_201_CREATED)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment