Search code examples
pythonjsondjangohttprequesttastypie

How to choose to create or no an entity in django via tastypie depending on other Entity's data?


I have an Event class where are defined number of Participients and limit of Participients and Ticket class which represents registration to the Event.

I want that the Tickets to one event wont be created, when the associated event will be full (maxPersons==registredPersons).

Events and Tickets will be uploaded via tastypie RESTapi from Android.

What is the easier way to do that please?

So I have this class representing an event:

class Event(models.Model):
    user = models.ForeignKey(User, unique=False)
    maxPersons = models.IntegerField()
    registredPersons = models.IntegerField()

and this class which is a registration to event:

class Ticket(models.Model):
    event = models.ForeignKey(Event, unique=False)
    user = models.ForeignKey(User, unique=False)

this is tastypie resource class for Event:

class EventResource(ModelResource):
    user = fields.ForeignKey(UserResource, 'user')
    class Meta:
        queryset = Event.objects.all()
        resource_name = 'events'
        include_resource_uri = False
        authorization= Authorization()
        authentication = BasicAuthentication()



    def alter_list_data_to_serialize(self, request, data_dict):
        if isinstance(data_dict, dict):
            if 'meta' in data_dict:
                #Get rid of the meta object                                                                               
                del(data_dict['meta'])

        return data_dict

And this one is to ticket:

class TicketResource(ModelResource):
    user = fields.ForeignKey(UserResource, 'user')
    event = fields.ForeignKey(EventResource,'journey')
    class Meta:
        queryset = Ticket.objects.all()
        resource_name = 'tickets'
        include_resource_uri = False
        authorization= Authorization()
        authentication = BasicAuthentication()
        filtering = {
            'user': ALL_WITH_RELATIONS,
        }
    def get_object_list(self, request):
        return super(TicketResource, self).get_object_list(request).filter(user = request.user)


    def alter_list_data_to_serialize(self, request, data_dict):
        if isinstance(data_dict, dict):
            if 'meta' in data_dict:
                #Get rid of the meta object                                                                               
                del(data_dict['meta'])


        return data_dict

Solution

  • Simply overriding Authorization will not allow users to create tickets if event is full.

    class TicketAuthorization(Authorization):
        def create_detail(self, object_list, bundle):
            return bundle.obj.event.registredPersons <= bundle.obj.event.maxPersons:
    
    class TicketResource(ModelResource):
        user = fields.ForeignKey(UserResource, 'user')
        event = fields.ForeignKey(EventResource,'journey')
        class Meta:
            queryset = Ticket.objects.all()
            resource_name = 'tickets'
            include_resource_uri = False
            authorization= TicketAuthorization()
            authentication = BasicAuthentication()
            filtering = {
                'user': ALL_WITH_RELATIONS,
            }
    
        [...]