Skip to content

Commit

Permalink
Merge pull request #47 from Build-Squad/GEN-90-Refactor-Services-Pack…
Browse files Browse the repository at this point in the history
…ages-POST-PATCH-APIs

GEN 90 / 91 Update Create API for Services and UI Modal for Service
  • Loading branch information
muditmahajan authored Dec 21, 2023
2 parents 1555e02 + c1e0438 commit b24f324
Show file tree
Hide file tree
Showing 16 changed files with 941 additions and 616 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.7 on 2023-12-19 06:07
# Generated by Django 4.2.7 on 2023-12-19 07:41

from django.db import migrations, models

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 4.2.7 on 2023-12-19 07:41

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('packages', '0002_service_end_date_service_start_date_and_more'),
]

operations = [
migrations.RemoveField(
model_name='service',
name='end_date',
),
migrations.RemoveField(
model_name='service',
name='start_date',
),
migrations.RemoveField(
model_name='servicemaster',
name='is_duration_based',
),
migrations.AddField(
model_name='package',
name='type',
field=models.CharField(choices=[('package', 'package'), ('service', 'service')], default='package', max_length=50),
),
migrations.AddField(
model_name='service',
name='is_duration_based',
field=models.BooleanField(default=False),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 4.2.7 on 2023-12-20 05:08

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('packages', '0003_remove_service_end_date_remove_service_start_date_and_more'),
]

operations = [
migrations.AlterField(
model_name='package',
name='publish_date',
field=models.DateTimeField(blank=True, null=True),
),
migrations.AlterField(
model_name='package',
name='status',
field=models.CharField(choices=[('draft', 'draft'), ('published', 'published')], default='draft', max_length=50),
),
migrations.AlterField(
model_name='package',
name='type',
field=models.CharField(choices=[('package', 'package'), ('service', 'service')], default='service', max_length=50),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.2.7 on 2023-12-20 06:38

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('packages', '0004_alter_package_publish_date_alter_package_status_and_more'),
]

operations = [
migrations.RemoveField(
model_name='package',
name='currency',
),
migrations.RemoveField(
model_name='package',
name='price',
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2.7 on 2023-12-20 10:40

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('packages', '0005_remove_package_currency_remove_package_price'),
]

operations = [
migrations.AddField(
model_name='service',
name='platform_fees',
field=models.DecimalField(blank=True, decimal_places=2, max_digits=10, null=True),
),
migrations.AddField(
model_name='service',
name='platform_price',
field=models.DecimalField(blank=True, decimal_places=2, max_digits=10, null=True),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.2.7 on 2023-12-21 05:29

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('packages', '0006_service_platform_fees_service_platform_price'),
]

operations = [
migrations.RemoveField(
model_name='service',
name='is_duration_based',
),
migrations.RemoveField(
model_name='service',
name='platform_price',
),
migrations.AddField(
model_name='servicemaster',
name='is_duration_based',
field=models.BooleanField(default=False),
),
]
31 changes: 17 additions & 14 deletions src/api/marketplace/packages/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,27 @@
from django.core.exceptions import ValidationError

class Package(models.Model):
TYPE_CHOICES = (
('package', 'package'),
('service', 'service')
)

STATUS_CHOICES = (
('draft', 'draft'),
('published', 'published'),
)

id = models.UUIDField(primary_key=True, verbose_name='Package', default=uuid.uuid4, editable=False)
influencer = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='created_by_user', on_delete=SET_NULL, null=True)
name = models.CharField(max_length=100, blank=True, null=True)
description = models.TextField(blank=True, null=True)
price = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
currency = models.ForeignKey(Currency, related_name='package_currency_id', on_delete=SET_NULL, null=True)
status = models.CharField(max_length=100, blank=True, null=True)
publish_date = models.DateTimeField(blank=True)
status = models.CharField(choices=STATUS_CHOICES,
max_length=50, default='draft')
publish_date = models.DateTimeField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
deleted_at = models.DateTimeField(blank=True, null=True)
type = models.CharField(choices=TYPE_CHOICES,
max_length=50, default='service')

class Meta:
db_table = "package"
Expand Down Expand Up @@ -66,22 +77,14 @@ class Service(models.Model):
quantity = models.IntegerField(blank=True, null=True)
price = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
currency = models.ForeignKey(Currency, related_name='service_currency_id', on_delete=SET_NULL, null=True)
platform_fees = models.DecimalField(
max_digits=10, decimal_places=2, blank=True, null=True)
status = models.CharField(max_length=100, blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
deleted_at = models.DateTimeField(blank=True, null=True)
start_date = models.DateTimeField(blank=True, null=True)
end_date = models.DateTimeField(blank=True, null=True)
class Meta:
db_table = "service"

def delete (self, *args, **kwargs):
self.deleted_at = timezone.now()
self.save()








39 changes: 17 additions & 22 deletions src/api/marketplace/packages/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from rest_framework import serializers
from .models import ServiceMaster, Service, Package
from core.serializers import CurrencySerializer
from uuid import UUID


class ServiceMasterSerializer(serializers.ModelSerializer):
class Meta:
Expand All @@ -17,17 +19,16 @@ class Meta:
# fields = ('id', 'name', 'description', 'limit', 'type', 'created_at')

class PackageSerializer(serializers.ModelSerializer):
currency = CurrencySerializer()
class Meta:
model = Package
fields = '__all__'
# fields = ('id', 'influencer', 'name', 'description', 'price', 'currency', 'status', 'publish_date', 'created_at'


class CreatePackageSerializer(serializers.ModelSerializer):
class Meta:
model = Package
fields = '__all__'
# fields = ('id', 'influencer', 'name', 'description', 'price', 'currency', 'status', 'publish_date', 'created_at'

class ServicesSerializer(serializers.ModelSerializer):
service_master = ServiceMasterSerializer()
Expand All @@ -40,27 +41,21 @@ class Meta:
# fields = ('id', 'service_master', 'package', 'quantity', 'price', 'currency', 'status'

class CreateServicesSerializer(serializers.ModelSerializer):
package = CreatePackageSerializer()

class Meta:
model = Service
fields = '__all__'
# fields = ('id', 'service_master', 'package', 'quantity', 'price', 'currency', 'status'

def validate(self, data):
service_master = data.get('service_master')
start_date = data.get('start_date')
end_date = data.get('end_date')

# If service_master is just the ID, retrieve the ServiceMaster instance
if isinstance(service_master, int):
service_master = ServiceMaster.objects.get(id=service_master)

if service_master.is_duration_based:
if start_date is None or end_date is None:
raise serializers.ValidationError(
"Start date and end date cannot be empty")
else:
if start_date is not None or end_date is not None:
raise serializers.ValidationError(
"Service Master is not duration based. Start date and end date should be empty")

return data
def create(self, validated_data):
package_data = validated_data.pop('package')
package_data['influencer'] = self.context['request'].user_account
package = Package.objects.create(**package_data)
service = Service.objects.create(package=package, **validated_data)
return service

def update(self, instance, validated_data):
package_data = validated_data.pop('package', {})
package_data['influencer'] = self.context['request'].user_account
Package.objects.filter(id=instance.package.id).update(**package_data)
return super().update(instance, validated_data)
Loading

0 comments on commit b24f324

Please sign in to comment.