Skip to content

Commit 41f63e3

Browse files
committed
file uploading working now
1 parent a43aff4 commit 41f63e3

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

files/urls.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from django.urls import path
22

33

4-
from files.views import FileUploadView
4+
from files.views import FileView
55

66
app_name = "industries"
77

88
urlpatterns = [
9-
path("", FileUploadView.as_view()),
9+
path("", FileView.as_view()),
10+
path("<int:pk>", FileView.as_view()),
1011
]

files/views.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
)
1818

1919

20-
class FileUploadView(APIView):
20+
class FileView(APIView):
2121
permission_classes = [permissions.AllowAny]
2222

2323
@transaction.atomic
@@ -46,17 +46,21 @@ def post(self, request):
4646
with file.open(mode="rb") as file_object:
4747
r = requests.put(
4848
url,
49-
headers={
50-
"X-Auth-Token": token,
51-
"X-Object-Meta-Content-Type": file_object.content_type,
52-
},
53-
files={"file": (file.name, file_object, file.content_type)},
49+
headers={"X-Auth-Token": token, "Content-Type": file_object.content_type},
50+
data=file_object.read(),
5451
)
5552
if r.status_code != 201:
5653
return Response("Failed to upload file", status=status.HTTP_409_CONFLICT)
5754
self._save_to_db(user, url)
5855
return Response({"url": url}, status=status.HTTP_201_CREATED)
5956

57+
def delete(self, request, pk):
58+
try:
59+
UserFile.objects.get(pk=pk).delete()
60+
except UserFile.DoesNotExist:
61+
return Response(status=status.HTTP_404_NOT_FOUND)
62+
return Response(status=status.HTTP_200_OK)
63+
6064
@classmethod
6165
def _save_to_db(cls, user, url):
6266
"""creates userfile object for file uploads"""

0 commit comments

Comments
 (0)