Created
November 23, 2016 06:11
-
-
Save ragowthaman/101bfc51ae5d881c902dce21e206af00 to your computer and use it in GitHub Desktop.
Image upload via webservice Django/ionic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_resultfile_upload_destination(instance, filename): | |
return "complaint/00_generic/farmerid_{farmerid}/{file}".format(farmerid=instance.farmer.id, file=filename) | |
class Complaint(models.Model): | |
farmer = models.ForeignKey(Farmer) | |
file = models.FileField(blank=True, default='', upload_to=get_resultfile_upload_destination) | |
text = models.TextField(blank=True, default='') | |
timestamp = models.DateTimeField(auto_now=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ComplaintSerializer(serializers.ModelSerializer): | |
class Meta: | |
model = Complaint | |
fields = ('farmer', 'file', 'text') | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
url(r'^webservice/register/complaint/$', 'instance.views.register_generic_complaint_via_webservice'), | |
// url => http://localhost:8000/instance/webservice/register/complaint/ | |
// curl upload test | |
// curl -i -X PUT -S -u "username:password" -F "file=@/Users/gramasamy/Desktop/Mythili_newspaper.jpeg" -F "farmer=6" localhost:8000/instance/webservice/register/complaint/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@api_view(['GET', 'PUT']) | |
def register_generic_complaint_via_webservice(request): | |
""" | |
Record the generic complaint coming from mobile applications. | |
:param request: | |
:return: success | |
""" | |
# do the operations | |
if request.method == "PUT": | |
serializer = ComplaintSerializer(data=request.data) | |
print serializer | |
if serializer.is_valid(): | |
print "serializer is valid" | |
serializer.save() | |
return Response(status=status.HTTP_201_CREATED) | |
else: | |
print "serializer is NOT valid" | |
print serializer.error_messages | |
print serializer.error | |
print serializer.data | |
return Response(serializer.errors, status=status.HTTP_204_NO_CONTENT) | |
elif request.method == 'GET': | |
serializer = ComplaintSerializer(Complaint.objects.all()[0]) | |
return Response(serializer.data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment