Skip to content

Commit

Permalink
Merge pull request #1833 from gtech-mulearn:dev
Browse files Browse the repository at this point in the history
[PATCH] user delete patch
  • Loading branch information
MZaFaRM authored Dec 16, 2023
2 parents 2dd9a0e + 54ce3f8 commit 5f3b9ae
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 113 deletions.
25 changes: 10 additions & 15 deletions api/dashboard/user/dash_user_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from utils.utils import CommonUtils, DateTimeUtils, DiscordWebhooks, send_template_mail
from . import dash_user_serializer

BE_DOMAIN_NAME = decouple_config('BE_DOMAIN_NAME')
BE_DOMAIN_NAME = decouple_config("BE_DOMAIN_NAME")


class UserInfoAPI(APIView):
Expand Down Expand Up @@ -52,18 +52,13 @@ def get(self, request, user_id):

@role_required([RoleType.ADMIN.value])
def delete(self, request, user_id):
user = User.objects.filter(id=user_id).first()

if user is None:
try:
user = User.objects.get(id=user_id).delete()
return CustomResponse(
general_message="User Not Available"
).get_failure_response()

user.delete()

return CustomResponse(
general_message="User deleted successfully"
).get_success_response()
general_message="User deleted successfully"
).get_success_response()
except User.DoesNotExist as e:
return CustomResponse(general_message=str(e)).get_failure_response()

@role_required([RoleType.ADMIN.value])
def patch(self, request, user_id):
Expand Down Expand Up @@ -237,9 +232,9 @@ def post(self, request):
email_muid = request.data.get("emailOrMuid")

if not (
user := User.objects.filter(
Q(muid=email_muid) | Q(email=email_muid)
).first()
user := User.objects.filter(
Q(muid=email_muid) | Q(email=email_muid)
).first()
):
return CustomResponse(
general_message="User not exist"
Expand Down
18 changes: 18 additions & 0 deletions db/apps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
from django.apps import AppConfig
from decouple import config


class SystemUserNotFoundError(Exception):
pass


class DbConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "db"

def ready(self) -> None:
_ready = super().ready()
self.check_system_user_exists()
return _ready

@classmethod
def check_system_user_exists(cls):
from db.user import User
if not User.objects.filter(id=config("SYSTEM_ADMIN_ID")).exists():
raise SystemUserNotFoundError(
f"Create a System User with pk -\"{config('SYSTEM_ADMIN_ID')}\""
)
18 changes: 9 additions & 9 deletions db/hackathon.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from db.organization import District, Organization
from db.user import User

from .helper import get_system_admin_id
from django.conf import settings

# fmt: off
# noinspection PyPep8
Expand All @@ -27,9 +27,9 @@ class Hackathon(models.Model):
event_start = models.DateTimeField(blank=True, null=True)
event_end = models.DateTimeField(blank=True, null=True)
status = models.CharField(max_length=20, blank=True, null=True)
updated_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='updated_by', related_name='hackathon_updated_by')
updated_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='updated_by', related_name='hackathon_updated_by')
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='created_by', related_name='hackathon_created_by')
created_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='created_by', related_name='hackathon_created_by')
created_at = models.DateTimeField(auto_now_add=True)

class Meta:
Expand All @@ -43,9 +43,9 @@ class HackathonForm(models.Model):
field_name = models.CharField(max_length=255)
field_type = models.CharField(max_length=50)
is_required = models.BooleanField(default=False)
updated_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='updated_by', related_name='hackathon_form_updated_by')
updated_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='updated_by', related_name='hackathon_form_updated_by')
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='created_by', related_name='hackathon_form_created_by')
created_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='created_by', related_name='hackathon_form_created_by')
created_at = models.DateTimeField(auto_now_add=True)

class Meta:
Expand All @@ -57,9 +57,9 @@ class HackathonOrganiserLink(models.Model):
id = models.CharField(primary_key=True, max_length=36)
organiser = models.ForeignKey(User, on_delete=models.CASCADE)
hackathon = models.ForeignKey(Hackathon, on_delete=models.CASCADE)
updated_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='updated_by', related_name='hackathon_organiser_link_updated_by')
updated_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='updated_by', related_name='hackathon_organiser_link_updated_by')
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='created_by', related_name='hackathon_organiser_link_created_by')
created_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='created_by', related_name='hackathon_organiser_link_created_by')
created_at = models.DateTimeField(auto_now_add=True)

class Meta:
Expand All @@ -71,9 +71,9 @@ class HackathonUserSubmission(models.Model):
id = models.CharField(primary_key=True, max_length=36)
user = models.ForeignKey(User, on_delete=models.CASCADE)
hackathon = models.ForeignKey(Hackathon, on_delete=models.CASCADE)
updated_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='updated_by', related_name='hackathon_submission_updated_by')
updated_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='updated_by', related_name='hackathon_submission_updated_by')
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='created_by', related_name='hackathon_submission_created_by')
created_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='created_by', related_name='hackathon_submission_created_by')
created_at = models.DateTimeField(auto_now_add=True)
data = models.JSONField(max_length=2000, blank=True, null=True)

Expand Down
6 changes: 0 additions & 6 deletions db/helper.py

This file was deleted.

2 changes: 1 addition & 1 deletion db/integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from db.user import User

from .helper import get_system_admin_id
from django.conf import settings

# fmt: off
# noinspection PyPep8
Expand Down
10 changes: 5 additions & 5 deletions db/learning_circle.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from db.task import InterestGroup, Organization
from db.user import User

from .helper import get_system_admin_id
from django.conf import settings

# fmt: off
# noinspection PyPep8
Expand All @@ -22,10 +22,10 @@ class LearningCircle(models.Model):
meet_time = models.CharField(max_length=10, blank=True, null=True)
day = models.CharField(max_length=20, blank=True, null=True)
note = models.CharField(max_length=500, blank=True, null=True)
updated_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column="updated_by",
updated_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column="updated_by",
related_name="learning_circle_updated_by")
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column="created_by",
created_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column="created_by",
related_name="learning_circle_created_by")
created_at = models.DateTimeField(auto_now=True)

Expand Down Expand Up @@ -59,10 +59,10 @@ class CircleMeetingLog(models.Model):
attendees = models.CharField(max_length=216)
agenda = models.CharField(max_length=2000)
images = models.ImageField(max_length=200, upload_to='lc/meet-report')
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='created_by',
created_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='created_by',
related_name='circle_meeting_log_created_by')
created_at = models.DateTimeField(auto_now=True)
updated_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='updated_by',
updated_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='updated_by',
related_name='circle_meeting_log_updated_by')
updated_at = models.DateTimeField(auto_now=True)

Expand Down
4 changes: 2 additions & 2 deletions db/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from db.user import User

from .helper import get_system_admin_id
from django.conf import settings

# fmt: off
# noinspection PyPep8
Expand All @@ -17,7 +17,7 @@ class Notification(models.Model):
button = models.CharField(max_length=10, blank=True, null=True)
url = models.CharField(max_length=100, blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column="created_by", related_name="created_notifications")
created_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column="created_by", related_name="created_notifications")

class Meta:
managed = False
Expand Down
40 changes: 20 additions & 20 deletions db/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.db import models

from .helper import get_system_admin_id
from django.conf import settings
from .user import User

# fmt: off
Expand All @@ -11,10 +11,10 @@
class Country(models.Model):
id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4)
name = models.CharField(max_length=75, null=False, unique=True)
updated_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='updated_by',
updated_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='updated_by',
related_name='country_updated_by')
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='created_by',
created_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='created_by',
related_name='country_created_by')
created_at = models.DateTimeField(auto_now_add=True)

Expand All @@ -27,10 +27,10 @@ class State(models.Model):
id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4)
name = models.CharField(max_length=75)
country = models.ForeignKey(Country, on_delete=models.CASCADE, related_name='state_country')
updated_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='updated_by',
updated_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='updated_by',
related_name='state_updated_by')
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='created_by',
created_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='created_by',
related_name='state_created_by')
created_at = models.DateTimeField(auto_now_add=True)

Expand All @@ -43,10 +43,10 @@ class Zone(models.Model):
id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4)
name = models.CharField(max_length=75)
state = models.ForeignKey(State, on_delete=models.CASCADE, related_name='zone_state')
updated_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='updated_by',
updated_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='updated_by',
related_name='zone_updated_by')
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='created_by',
created_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='created_by',
related_name='zone_created_by')
created_at = models.DateTimeField(auto_now_add=True)

Expand All @@ -59,10 +59,10 @@ class District(models.Model):
id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4)
name = models.CharField(max_length=75)
zone = models.ForeignKey(Zone, on_delete=models.CASCADE, related_name="district_zone")
updated_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='updated_by',
updated_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='updated_by',
related_name='district_updated_by')
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='created_by',
created_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='created_by',
related_name='district_created_by')
created_at = models.DateTimeField(auto_now_add=True)

Expand All @@ -75,10 +75,10 @@ class OrgAffiliation(models.Model):
id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4())
title = models.CharField(max_length=75)
updated_by = models.ForeignKey(
User, on_delete=models.SET(get_system_admin_id), db_column='updated_by', related_name='org_affiliation_updated_by')
User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='updated_by', related_name='org_affiliation_updated_by')
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(
User, on_delete=models.SET(get_system_admin_id), db_column='created_by', related_name='org_affiliation_created_by')
User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='created_by', related_name='org_affiliation_created_by')
created_at = models.DateTimeField(auto_now_add=True)

class Meta:
Expand All @@ -93,10 +93,10 @@ class Organization(models.Model):
org_type = models.CharField(max_length=25)
affiliation = models.ForeignKey(OrgAffiliation, on_delete=models.CASCADE, blank=True, null=True, related_name='organization_affiliation')
district = models.ForeignKey(District, on_delete=models.CASCADE, related_name='organization_district')
updated_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='updated_by',
updated_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='updated_by',
related_name='organization_updated_by')
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='created_by',
created_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='created_by',
related_name='organization_created_by')
created_at = models.DateTimeField(auto_now_add=True)

Expand All @@ -108,10 +108,10 @@ class Meta:
class Department(models.Model):
id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4())
title = models.CharField(max_length=100)
updated_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='updated_by',
updated_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='updated_by',
related_name='department_updated_by')
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='created_by',
created_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='created_by',
related_name='department_created_by')
created_at = models.DateTimeField(auto_now_add=True)

Expand All @@ -124,10 +124,10 @@ class College(models.Model):
id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4())
level = models.IntegerField(default=0)
org = models.OneToOneField(Organization, on_delete=models.CASCADE, related_name='college_org', unique=True)
updated_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='updated_by',
updated_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='updated_by',
related_name='college_updated_by')
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='created_by',
created_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='created_by',
related_name='college_created_by')
created_at = models.DateTimeField(auto_now_add=True)

Expand All @@ -140,10 +140,10 @@ class OrgDiscordLink(models.Model):
id = models.CharField(primary_key=True, max_length=36)
discord_id = models.CharField(unique=True, max_length=36)
org = models.OneToOneField(Organization, on_delete=models.CASCADE, related_name='org_discord_link_org_id')
updated_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='updated_by',
updated_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='updated_by',
related_name='org_discord_link_updated_by')
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='created_by',
created_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='created_by',
related_name='org_discord_link_created_by')
created_at = models.DateTimeField(auto_now_add=True)

Expand All @@ -161,7 +161,7 @@ class UserOrganizationLink(models.Model):
graduation_year = models.CharField(max_length=10, blank=True, null=True)
verified = models.BooleanField()
is_alumni = models.IntegerField(blank=True, null=True, default=False)
created_by = models.ForeignKey(User, on_delete=models.SET(get_system_admin_id), db_column='created_by',
created_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='created_by',
related_name='user_organization_link_created_by')
created_at = models.DateTimeField(auto_now_add=True)

Expand Down
Loading

0 comments on commit 5f3b9ae

Please sign in to comment.