Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added project_password as a new field into project model,added approp… #84

Merged
merged 4 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions backend/projects/migrations/0056_project_project_password.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.2.14 on 2024-06-24 08:14

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("projects", "0055_alter_project_project_type"),
]

operations = [
migrations.AddField(
model_name="project",
name="project_password",
field=models.CharField(
blank=True,
help_text="Password for the Project",
max_length=128,
null=True,
),
),
]
15 changes: 15 additions & 0 deletions backend/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.utils.timezone import now
from datetime import datetime, timedelta
from users.models import LANG_CHOICES
from django.contrib.auth.hashers import make_password, check_password

# from dataset import LANG_CHOICES

Expand Down Expand Up @@ -308,6 +309,20 @@ def set_lock(self, annotator, context):
verbose_name="Target Language",
)

project_password = models.CharField(
max_length=128,
null=True,
blank=True,
help_text=("Password for the Project"),
)

def set_project_password(self, raw_password):
self.project_password = make_password(raw_password)
self.save()

def check_project_password(self, raw_password):
return check_password(raw_password, self.project_password)

def __str__(self):
return str(self.title)

Expand Down
100 changes: 100 additions & 0 deletions backend/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4235,3 +4235,103 @@ def change_task_language_field_to_project_target_language(self, request, pk):
{"message": "language field of task data succesfully updated!"},
status=status.HTTP_200_OK,
)

@action(
detail=True,
methods=["POST"],
url_name="set_password",
)
def set_password(self, request, pk=None):
try:
project = Project.objects.get(pk=pk)

if "password" not in request.data:
return Response(
{"error": "Password key is missing"},
status=status.HTTP_400_BAD_REQUEST,
)

password = request.data.get("password")
ishangujarathi marked this conversation as resolved.
Show resolved Hide resolved

if not password:
return Response(
{"error": "Password not provided"},
status=status.HTTP_400_BAD_REQUEST,
)

try:
project.set_project_password(password)

except Exception as e:
return Response(
{"error": f"Failed to set the password : {str(e)}"},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)

return Response(
{"message": "Password set Successfully"}, status=status.HTTP_200_OK
)

except Project.DoesNotExist:
return Response(
{"error": "Project not found"}, status=status.HTTP_404_NOT_FOUND
)

except Exception as e:
return Response(
{"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR
)

@action(
detail=True,
methods=["POST"],
url_name="verify_password",
)
def verify_password(
self,
request,
pk=None,
):
try:
project = Project.objects.get(pk=pk)

if "password" not in request.data:
return Response(
{"error": "Password key is missing"},
status=status.HTTP_400_BAD_REQUEST,
)

password = request.data.get("password")

if not password:
return Response(
{"error": "Password not provided"},
status=status.HTTP_400_BAD_REQUEST,
)
try:
if project.check_project_password(password):
return Response(
{"message": "Authentication Successful"},
status=status.HTTP_200_OK,
)

else:
return Response(
{"error": "Authentication Failed"},
status=status.HTTP_401_UNAUTHORIZED,
)
except Exception as e:
return Response(
{"error": f"Failed to authenticate project : {str(e)}"},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)

except Project.DoesNotExist:
return Response(
{"error": "Project not found"}, status=status.HTTP_404_NOT_FOUND
)

except Exception as e:
return Response(
{"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR
)
3 changes: 2 additions & 1 deletion backend/users/migrations/0036_alter_user_is_approved.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Generated by Django 3.2.14 on 2024-06-26 05:14
# Generated by Django 3.2.14 on 2024-06-24 08:14


from django.db import migrations, models

Expand Down
Loading