mirror of
https://github.com/AmanTahiliani/PeerNotes.git
synced 2026-08-07 11:55:12 -04:00
Added peer to existing file peers
This commit is contained in:
@@ -132,7 +132,7 @@ def load_data_from_json(apps, schema_editor):
|
|||||||
"Quantum Algorithms",
|
"Quantum Algorithms",
|
||||||
"Quantum Machine Learning",
|
"Quantum Machine Learning",
|
||||||
"Quantum Error Correction",
|
"Quantum Error Correction",
|
||||||
"Quantum Networking"
|
"Quantum Networking",
|
||||||
]
|
]
|
||||||
|
|
||||||
Topic = apps.get_model("api", "Topic")
|
Topic = apps.get_model("api", "Topic")
|
||||||
@@ -145,6 +145,7 @@ def load_data_from_json(apps, schema_editor):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Error:", str(e))
|
print("Error:", str(e))
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
|||||||
@@ -64,6 +64,11 @@ urlpatterns = [
|
|||||||
views.DownvoteFile.as_view(),
|
views.DownvoteFile.as_view(),
|
||||||
name="downvote-file",
|
name="downvote-file",
|
||||||
),
|
),
|
||||||
|
path(
|
||||||
|
"api/files/<int:file_id>/add-peer/",
|
||||||
|
views.AddPeerToFile.as_view(),
|
||||||
|
name="add-peer",
|
||||||
|
),
|
||||||
path("api/files/<int:pk>/", views.FileDetailAPIView.as_view(), name="file-detail"),
|
path("api/files/<int:pk>/", views.FileDetailAPIView.as_view(), name="file-detail"),
|
||||||
path("api/report_user/", views.ReportUserView.as_view(), name="report-user"),
|
path("api/report_user/", views.ReportUserView.as_view(), name="report-user"),
|
||||||
path("api/register/", views.RegisterFile.as_view(), name="file-register"),
|
path("api/register/", views.RegisterFile.as_view(), name="file-register"),
|
||||||
|
|||||||
@@ -235,6 +235,31 @@ class RegisterFile(APIView):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class AddPeerToFile(APIView):
|
||||||
|
authentication_classes = [TokenAuthentication]
|
||||||
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
|
def post(self, request, file_id):
|
||||||
|
try:
|
||||||
|
user = request.user
|
||||||
|
file = File.objects.get(id=file_id)
|
||||||
|
print("File fetched")
|
||||||
|
print(file)
|
||||||
|
print(file.peer_users)
|
||||||
|
if not file.peer_users.filter(id=user.id).exists():
|
||||||
|
file.peer_users.add(user)
|
||||||
|
file.save()
|
||||||
|
return Response(
|
||||||
|
f"User added to file with id {file.id}", status=status.HTTP_200_OK
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error occured while adding peer to file: {e}")
|
||||||
|
return Response(
|
||||||
|
"Unable to register peer to given file id",
|
||||||
|
status=status.HTTP_400_BAD_REQUEST,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class FileFilterView(APIView):
|
class FileFilterView(APIView):
|
||||||
authentication_classes = [TokenAuthentication]
|
authentication_classes = [TokenAuthentication]
|
||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
@@ -283,7 +308,6 @@ class FileFilterView(APIView):
|
|||||||
if semester_id:
|
if semester_id:
|
||||||
queryset = queryset.filter(semester__id=semester_id)
|
queryset = queryset.filter(semester__id=semester_id)
|
||||||
|
|
||||||
|
|
||||||
queryset = queryset.annotate(
|
queryset = queryset.annotate(
|
||||||
upvote_count=Count("upvotes"), downvote_count=Count("downvotes")
|
upvote_count=Count("upvotes"), downvote_count=Count("downvotes")
|
||||||
).order_by(F("downvote_count") - F("upvote_count"))
|
).order_by(F("downvote_count") - F("upvote_count"))
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ def copy_file():
|
|||||||
print(id)
|
print(id)
|
||||||
if "file" not in files:
|
if "file" not in files:
|
||||||
return "File must be sent in request", 400
|
return "File must be sent in request", 400
|
||||||
file = request.files['file']
|
file = request.files["file"]
|
||||||
filename = file.filename.replace(" ", "_")
|
filename = file.filename.replace(" ", "_")
|
||||||
# id = data["file_id"]
|
# id = data["file_id"]
|
||||||
# if not os.path.exists(source_file):
|
# if not os.path.exists(source_file):
|
||||||
@@ -93,6 +93,7 @@ def request_file():
|
|||||||
print("Error:", error)
|
print("Error:", error)
|
||||||
return error, response.status_code
|
return error, response.status_code
|
||||||
|
|
||||||
|
|
||||||
@app.route("/files", methods=["GET"])
|
@app.route("/files", methods=["GET"])
|
||||||
def list_files():
|
def list_files():
|
||||||
files = os.listdir("./uploads")
|
files = os.listdir("./uploads")
|
||||||
@@ -101,6 +102,7 @@ def list_files():
|
|||||||
newFiles.append("_".join(file.split("_", 1)[1:]))
|
newFiles.append("_".join(file.split("_", 1)[1:]))
|
||||||
return {"files": newFiles}, 200
|
return {"files": newFiles}, 200
|
||||||
|
|
||||||
|
|
||||||
@app.route("/request-tests", methods=["GET"])
|
@app.route("/request-tests", methods=["GET"])
|
||||||
def request_test_():
|
def request_test_():
|
||||||
data = request.json
|
data = request.json
|
||||||
|
|||||||
14
test1.py
14
test1.py
@@ -2,6 +2,7 @@ import requests
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
def request_tests():
|
def request_tests():
|
||||||
url = "http://localhost:8080/request-tests"
|
url = "http://localhost:8080/request-tests"
|
||||||
downloaded_files = []
|
downloaded_files = []
|
||||||
@@ -9,15 +10,17 @@ def request_tests():
|
|||||||
for i in range(100):
|
for i in range(100):
|
||||||
filename = f"TestFileTransfer.pdf"
|
filename = f"TestFileTransfer.pdf"
|
||||||
|
|
||||||
payload = json.dumps({
|
payload = json.dumps(
|
||||||
|
{
|
||||||
"id": 5,
|
"id": 5,
|
||||||
"filename": filename,
|
"filename": filename,
|
||||||
"ip": "143.215.87.54",
|
"ip": "143.215.87.54",
|
||||||
"new_filename":f"TestFileTransfer_{i}.pdf"
|
"new_filename": f"TestFileTransfer_{i}.pdf",
|
||||||
})
|
}
|
||||||
|
)
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
'Content-Type': 'application/json',
|
"Content-Type": "application/json",
|
||||||
}
|
}
|
||||||
|
|
||||||
response = requests.get(url, headers=headers, data=payload)
|
response = requests.get(url, headers=headers, data=payload)
|
||||||
@@ -32,9 +35,10 @@ def request_tests():
|
|||||||
print(f"Request {i+1} failed with status code {response.status_code}")
|
print(f"Request {i+1} failed with status code {response.status_code}")
|
||||||
|
|
||||||
if downloaded_files:
|
if downloaded_files:
|
||||||
file_paths = ', '.join(['./uploads/' + file for file in downloaded_files])
|
file_paths = ", ".join(["./uploads/" + file for file in downloaded_files])
|
||||||
return f"1000 files have been downloaded to {file_paths}", 200
|
return f"1000 files have been downloaded to {file_paths}", 200
|
||||||
else:
|
else:
|
||||||
return "No files downloaded", 200
|
return "No files downloaded", 200
|
||||||
|
|
||||||
|
|
||||||
request_tests()
|
request_tests()
|
||||||
Reference in New Issue
Block a user