From 621b96a76a4062fa5a76c8c56de84ff454bad4b5 Mon Sep 17 00:00:00 2001 From: Aman Tahiliani Date: Mon, 22 Apr 2024 12:51:22 -0400 Subject: [PATCH] Added peer to existing file peers --- .../api/migrations/0012_auto_20240422_1349.py | 261 +++++++++--------- backend/api/urls.py | 5 + backend/api/views.py | 26 +- file_peer/peer_service.py | 4 +- test1.py | 30 +- 5 files changed, 181 insertions(+), 145 deletions(-) diff --git a/backend/api/migrations/0012_auto_20240422_1349.py b/backend/api/migrations/0012_auto_20240422_1349.py index 77c6ceb..48c6709 100644 --- a/backend/api/migrations/0012_auto_20240422_1349.py +++ b/backend/api/migrations/0012_auto_20240422_1349.py @@ -5,136 +5,136 @@ from django.db import migrations def load_data_from_json(apps, schema_editor): topics = [ - "Introduction to Computer Science", - "Programming Fundamentals", - "Data Structures", - "Algorithms", - "Computer Architecture", - "Operating Systems", - "Database Systems", - "Networking", - "Software Engineering", - "Artificial Intelligence", - "Theory of Computation", - "Compiler Design", - "Distributed Systems", - "Computer Graphics", - "Cryptography", - "Parallel and Concurrent Programming", - "Human-Computer Interaction", - "Computer Vision", - "Web Technologies", - "Embedded Systems", - "Data Mining and Analytics", - "Software Quality Assurance", - "Ethical and Social Implications of Computing", - "Advanced Topics", - "Automata Theory", - "Formal Languages and Automata", - "Computational Complexity", - "Graph Theory", - "Computational Geometry", - "Numerical Analysis", - "Machine Learning", - "Deep Learning", - "Natural Language Processing", - "Reinforcement Learning", - "Computer Security", - "Network Security", - "Information Retrieval", - "Cloud Computing", - "IoT (Internet of Things)", - "Virtual Reality", - "Augmented Reality", - "Mobile Application Development", - "Game Development", - "Computer Animation", - "Operating System Design", - "Real-Time Systems", - "Concurrency Control", - "Transaction Processing", - "Data Warehousing", - "Big Data Technologies", - "Data Science", - "Business Intelligence", - "Quantum Computing", - "Bioinformatics", - "Computational Biology", - "Robotics", - "Computer Ethics", - "Privacy and Security in Computing", - "Digital Forensics", - "Cryptocurrency and Blockchain", - "Social Network Analysis", - "Cloud Security", - "Edge Computing", - "Compiler Optimization", - "High-Performance Computing", - "Grid Computing", - "Fuzzy Logic and Systems", - "Expert Systems", - "Pattern Recognition", - "Knowledge Representation and Reasoning", - "Semantic Web", - "Computer Algebra Systems", - "Geographic Information Systems", - "Wireless Sensor Networks", - "Biometric Authentication", - "Internet Security Protocols", - "Software Testing and Debugging", - "Agile Methodologies", - "DevOps", - "Continuous Integration and Continuous Deployment (CI/CD)", - "Model-Driven Engineering", - "User Interface Design", - "Accessibility in Computing", - "User Experience (UX) Design", - "Game Theory", - "Social Computing", - "Computational Sociology", - "Computational Linguistics", - "Music Information Retrieval", - "Ethics in Artificial Intelligence", - "Algorithmic Game Theory", - "Behavioral Economics and Computing", - "Neuroinformatics", - "Health Informatics", - "Geometric Modeling", - "Scientific Computing", - "Information Theory", - "System Modeling and Simulation", - "Decision Support Systems", - "Database Security and Privacy", - "Knowledge Management Systems", - "Information Visualization", - "Geospatial Data Analysis", - "Enterprise Architecture", - "Supply Chain Management Systems", - "Data Integration and Interoperability", - "Software Reuse and Component-Based Development", - "Legal and Regulatory Issues in Computing", - "Information Assurance", - "Systems Analysis and Design", - "E-commerce Systems", - "E-learning Systems", - "Human-Robot Interaction", - "Biologically Inspired Computing", - "Social Robotics", - "Humanoid Robotics", - "Computational Neuroscience", - "Affective Computing", - "Explainable AI", - "Robot Ethics", - "Cyber-Physical Systems", - "Internet of Medical Things (IoMT)", - "Quantum Information Science", - "Quantum Cryptography", - "Quantum Algorithms", - "Quantum Machine Learning", - "Quantum Error Correction", - "Quantum Networking" -] - + "Introduction to Computer Science", + "Programming Fundamentals", + "Data Structures", + "Algorithms", + "Computer Architecture", + "Operating Systems", + "Database Systems", + "Networking", + "Software Engineering", + "Artificial Intelligence", + "Theory of Computation", + "Compiler Design", + "Distributed Systems", + "Computer Graphics", + "Cryptography", + "Parallel and Concurrent Programming", + "Human-Computer Interaction", + "Computer Vision", + "Web Technologies", + "Embedded Systems", + "Data Mining and Analytics", + "Software Quality Assurance", + "Ethical and Social Implications of Computing", + "Advanced Topics", + "Automata Theory", + "Formal Languages and Automata", + "Computational Complexity", + "Graph Theory", + "Computational Geometry", + "Numerical Analysis", + "Machine Learning", + "Deep Learning", + "Natural Language Processing", + "Reinforcement Learning", + "Computer Security", + "Network Security", + "Information Retrieval", + "Cloud Computing", + "IoT (Internet of Things)", + "Virtual Reality", + "Augmented Reality", + "Mobile Application Development", + "Game Development", + "Computer Animation", + "Operating System Design", + "Real-Time Systems", + "Concurrency Control", + "Transaction Processing", + "Data Warehousing", + "Big Data Technologies", + "Data Science", + "Business Intelligence", + "Quantum Computing", + "Bioinformatics", + "Computational Biology", + "Robotics", + "Computer Ethics", + "Privacy and Security in Computing", + "Digital Forensics", + "Cryptocurrency and Blockchain", + "Social Network Analysis", + "Cloud Security", + "Edge Computing", + "Compiler Optimization", + "High-Performance Computing", + "Grid Computing", + "Fuzzy Logic and Systems", + "Expert Systems", + "Pattern Recognition", + "Knowledge Representation and Reasoning", + "Semantic Web", + "Computer Algebra Systems", + "Geographic Information Systems", + "Wireless Sensor Networks", + "Biometric Authentication", + "Internet Security Protocols", + "Software Testing and Debugging", + "Agile Methodologies", + "DevOps", + "Continuous Integration and Continuous Deployment (CI/CD)", + "Model-Driven Engineering", + "User Interface Design", + "Accessibility in Computing", + "User Experience (UX) Design", + "Game Theory", + "Social Computing", + "Computational Sociology", + "Computational Linguistics", + "Music Information Retrieval", + "Ethics in Artificial Intelligence", + "Algorithmic Game Theory", + "Behavioral Economics and Computing", + "Neuroinformatics", + "Health Informatics", + "Geometric Modeling", + "Scientific Computing", + "Information Theory", + "System Modeling and Simulation", + "Decision Support Systems", + "Database Security and Privacy", + "Knowledge Management Systems", + "Information Visualization", + "Geospatial Data Analysis", + "Enterprise Architecture", + "Supply Chain Management Systems", + "Data Integration and Interoperability", + "Software Reuse and Component-Based Development", + "Legal and Regulatory Issues in Computing", + "Information Assurance", + "Systems Analysis and Design", + "E-commerce Systems", + "E-learning Systems", + "Human-Robot Interaction", + "Biologically Inspired Computing", + "Social Robotics", + "Humanoid Robotics", + "Computational Neuroscience", + "Affective Computing", + "Explainable AI", + "Robot Ethics", + "Cyber-Physical Systems", + "Internet of Medical Things (IoMT)", + "Quantum Information Science", + "Quantum Cryptography", + "Quantum Algorithms", + "Quantum Machine Learning", + "Quantum Error Correction", + "Quantum Networking", + ] + Topic = apps.get_model("api", "Topic") Topic.objects.all().delete() for topic in topics: @@ -145,6 +145,7 @@ def load_data_from_json(apps, schema_editor): except Exception as e: print("Error:", str(e)) + class Migration(migrations.Migration): dependencies = [ diff --git a/backend/api/urls.py b/backend/api/urls.py index 8392492..16f3c8c 100644 --- a/backend/api/urls.py +++ b/backend/api/urls.py @@ -64,6 +64,11 @@ urlpatterns = [ views.DownvoteFile.as_view(), name="downvote-file", ), + path( + "api/files//add-peer/", + views.AddPeerToFile.as_view(), + name="add-peer", + ), path("api/files//", views.FileDetailAPIView.as_view(), name="file-detail"), path("api/report_user/", views.ReportUserView.as_view(), name="report-user"), path("api/register/", views.RegisterFile.as_view(), name="file-register"), diff --git a/backend/api/views.py b/backend/api/views.py index 89faec3..5ff15e3 100644 --- a/backend/api/views.py +++ b/backend/api/views.py @@ -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): authentication_classes = [TokenAuthentication] permission_classes = [IsAuthenticated] @@ -282,7 +307,6 @@ class FileFilterView(APIView): queryset = queryset.filter(course__id=course_id) if semester_id: queryset = queryset.filter(semester__id=semester_id) - queryset = queryset.annotate( upvote_count=Count("upvotes"), downvote_count=Count("downvotes") diff --git a/file_peer/peer_service.py b/file_peer/peer_service.py index ff7e469..d7ec64b 100644 --- a/file_peer/peer_service.py +++ b/file_peer/peer_service.py @@ -20,7 +20,7 @@ def copy_file(): print(id) if "file" not in files: return "File must be sent in request", 400 - file = request.files['file'] + file = request.files["file"] filename = file.filename.replace(" ", "_") # id = data["file_id"] # if not os.path.exists(source_file): @@ -93,6 +93,7 @@ def request_file(): print("Error:", error) return error, response.status_code + @app.route("/files", methods=["GET"]) def list_files(): files = os.listdir("./uploads") @@ -101,6 +102,7 @@ def list_files(): newFiles.append("_".join(file.split("_", 1)[1:])) return {"files": newFiles}, 200 + @app.route("/request-tests", methods=["GET"]) def request_test_(): data = request.json diff --git a/test1.py b/test1.py index a27e756..34e60ca 100644 --- a/test1.py +++ b/test1.py @@ -2,39 +2,43 @@ import requests import json import os + def request_tests(): url = "http://localhost:8080/request-tests" downloaded_files = [] print("past url") for i in range(100): filename = f"TestFileTransfer.pdf" - - payload = json.dumps({ - "id": 5, - "filename": filename, - "ip": "143.215.87.54", - "new_filename":f"TestFileTransfer_{i}.pdf" - }) + + payload = json.dumps( + { + "id": 5, + "filename": filename, + "ip": "143.215.87.54", + "new_filename": f"TestFileTransfer_{i}.pdf", + } + ) headers = { - 'Content-Type': 'application/json', + "Content-Type": "application/json", } - + response = requests.get(url, headers=headers, data=payload) print("response received") print(payload) print(response) - + if response.status_code == 200: print(f"Request {i+1} successful") downloaded_files.append(f"bcdef_{i}.txt") else: print(f"Request {i+1} failed with status code {response.status_code}") - + 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 else: return "No files downloaded", 200 -request_tests() \ No newline at end of file + +request_tests()