Skip to content

Commit

Permalink
Feat[Bckend-db-fields]: Added a JSONField to each profile and organiz…
Browse files Browse the repository at this point in the history
…ation setting for the theme and brand fields, and adjusted the views accordingly. (#110)
  • Loading branch information
shikharpa authored Apr 12, 2024
1 parent 9e791d1 commit 24fb777
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 5.0.2 on 2024-04-09 10:02

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('db', '0002_organizationsettings_alter_user_options_and_more'),
]

operations = [
migrations.RemoveField(
model_name='organizationsettings',
name='theme',
),
migrations.AddField(
model_name='organizationsettings',
name='brand',
field=models.JSONField(default=dict),
),
migrations.AddField(
model_name='profile',
name='theme',
field=models.JSONField(default=dict),
),
]
3 changes: 2 additions & 1 deletion services/api/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
bio = models.TextField(blank=True, max_length=500)
date_of_birth= models.DateField(blank=True, null=True)
theme = models.JSONField(default=dict)
profile_image_url = models.URLField(blank=True, null=True, max_length=800, validators=[URLValidator]) # Accepts valid URLs

class Meta:
Expand All @@ -85,7 +86,7 @@ class OrganizationSettings(models.Model):
name = models.CharField(max_length=255, unique=True)
logo = models.URLField(blank=True, null=True, max_length=800, validators=[URLValidator])
icon = models.URLField(blank=True, null=True, max_length=800, validators=[URLValidator])
theme = models.CharField(max_length=20) # Assuming you have predefined themes
brand = models.JSONField(default=dict)
email = models.EmailField(max_length=50)
url = models.URLField(blank=True, null=True, max_length=800, validators=[URLValidator])
TIMEZONE_CHOICES = tuple(zip(pytz.all_timezones, pytz.all_timezones))
Expand Down
2 changes: 1 addition & 1 deletion services/api/db/userSerializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = ['bio', "date_of_birth", "profile_image_url"]
fields = ['bio', "date_of_birth", "theme", "profile_image_url"]

class OrganizationSettingsSerializer(serializers.ModelSerializer):
class Meta:
Expand Down
38 changes: 14 additions & 24 deletions services/api/db/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def put(self, request):
if serializer.is_valid(raise_exception=True):
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
except ValidationError as e:
return Response({"error" : e.detail}, status=status.HTTP_400_BAD_REQUEST)
except Exception:
return Response({'error': 'Please try again later!'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

Expand All @@ -35,32 +36,21 @@ def get(self, request):
return Response(serializer.data, status=status.HTTP_200_OK)
except OrganizationSettings.DoesNotExist:
return Response({'error': "Sorry! we couldn't find organization"}, status=status.HTTP_404_NOT_FOUND)
def post(self, request):

def put(self, request):
try:
organization_settings = OrganizationSettings.objects.first() #this returns none automatically if there is no object instance
if organization_settings:
return Response({'error': "organization already exists"}, status=status.HTTP_400_BAD_REQUEST)
serializer = OrganizationSettingsSerializer(data=request.data)
if serializer.is_valid(raise_exception=True):
serializer.save()
organization_settings, created = OrganizationSettings.objects.get_or_create(
defaults=request.data
)
if not created:
serializer = OrganizationSettingsSerializer(organization_settings, data=request.data)
if serializer.is_valid(raise_exception=True):
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
else:
serializer = OrganizationSettingsSerializer(organization_settings)
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
except ValidationError as e:
return Response({'error': e.detail}, status=status.HTTP_400_BAD_REQUEST)
except Exception:
return Response({'error': 'Please try again later!'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

def put(self, request):
try:
organization_settings = OrganizationSettings.objects.get()
serializer = OrganizationSettingsSerializer(organization_settings, data=request.data)
if serializer.is_valid(raise_exception=True):
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
except OrganizationSettings.DoesNotExist:
return Response({'error': "Sorry! we couldn't find organization"}, status=status.HTTP_404_NOT_FOUND)
except Exception:
return Response({'error': 'Please try again later!'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

0 comments on commit 24fb777

Please sign in to comment.