Skip to content

Commit

Permalink
added project_password as a new field into project model,added approp…
Browse files Browse the repository at this point in the history
…riate migrations and developed 2 API endpoints to set the password for the project and verify password for the project
  • Loading branch information
ishangujarathi committed Jun 25, 2024
1 parent b111c8e commit 83c9480
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
18 changes: 18 additions & 0 deletions backend/projects/migrations/0056_project_project_password.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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
71 changes: 71 additions & 0 deletions backend/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4225,3 +4225,74 @@ 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)
password = request.data.get("password")

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

project.set_project_password(password)
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)
password = request.data.get("password")

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

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 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
)
18 changes: 18 additions & 0 deletions backend/users/migrations/0036_alter_user_is_approved.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.14 on 2024-06-24 08:14

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('users', '0035_auto_20240426_1126'),
]

operations = [
migrations.AlterField(
model_name='user',
name='is_approved',
field=models.BooleanField(default=False, help_text='Indicates whether user is approved by the admin or not.', verbose_name='is_approved'),
),
]

0 comments on commit 83c9480

Please sign in to comment.