def invitations(self, request, *args, **kwargs): """Retrieve a member's invitations breakdown. will return a list containing all the memebers that have used its invitations and another list containg the invitations that haven't being used yet. """ member = self.get_object() invited_members = Membership.objects.filter(circle=self.circle, invited_by=request.user, is_active=True) unused_invitations = Invitation.objects.filter( circle=self.circle, issued_by=request.user, used=False).values_list('code') diff = member.remaining_invitations - len(unused_invitations) invitations = [x[0] for x in unused_invitations] for i in range(0, diff): invitations.append( Invitation.objects.create(issued_by=request.user, circle=self.circle).code) data = { 'used_invitations': MembershipModelSerializer(invited_members, many=True).data, 'invitations': invitations } return Response(data)
def invitations(self, request, *args, **kwargs): member = self.get_object() invited_members = Membership.objects.filter(circle=self.circle, invited_by=request.user, is_active=True) unused_invitations = Invitation.objects.filter( circle=self.circle, issued_by=request.user, used=True).values_list('code') diff = member.remaining_invitations - len(unused_invitations) invitations = [x[0] for x in unused_invitations] for i in range(0, diff): invitations.append( Invitation.objects.create(issued_by=request.user, circle=self.circle).code) data = { 'user_invitations': MembershipModelSerializer(invited_members, many=True).data, 'invitations': invitations } return Response(data)
def invitations(self, request, *args, **kwargs): """Recuperar el desglose de invitaciones de un miembro Devolverá una lista que contiene todos los miembros que han usado sus invitaciones y otra lista que contiene las invitaciones que aún no se han usado. """ member = self.get_object() # Obtenemos el objeto de detalle (el miembro) invited_members = Membership.objects.filter( circle=self.circle, invited_by=request.user, is_active=True ) # Trae a los miembro que fueron invitados por el usuario colocado en la url unsed_invitations = Invitation.objects.filter( circle=self.circle, issued_by=request.user, used=False, ).values_list('code') # Invitaciones no utilizadas.Colocamos values_list('code') para que nos lista # solo los valores de codigo. Esta lista es un poco rara. diff = member.remaining_invitations-len(unsed_invitations) # Sacamos la difencia del numero # invitaciones que le quedan por usar, contra las invitaciones que envio pero no son usadas. # Esto es para generar el codigo de invitaciones. por que por defecto seran el numero maximo. invitations = [x[0] for x in unsed_invitations] # La lista que nos devolvia el unsed_invitations tenian # de elementos tuplas. Pero no nosotros solo queremos los codigos, entonces recoremos la lista y la # llenamos en otra pero con los los elemento de la tupla. for i in range(0, diff): # recorre el for mietras diff sea mayor a cero. En otras palabras si ya # gasto todas sus invitaciones restantes y tiene las invitaciones no son usadas no entrara al for. invitations.append( Invitation.objects.create( issued_by=request.user, circle=self.circle ).code # Solo devolvemos el codigo para que se pueda agregar a la lista de strings. ) # Este for solo se activara cuando la primera vez que consulte, y cuando se le aumenten un numero # de ivitaciones. data = { 'used_invitations': MembershipModelSerializer(invited_members, many=True).data, 'invitations': invitations } return Response(data)
def invitations(self, request, *args, **kwargs): """Retrieve a member's invitation breakdown. Will return a list containing all the members that have used it's invitations and another list containing the invitations that have'nt be used yet. """ membership = self.get_object() invited_members = Membership.objects.filter( invited_by=request.user, circle=self.circle, is_active=True ) for remaining_invitation in range(membership.remaining_invitations): Invitation.objects.create( issued_by=request.user, circle=self.circle, ) membership.remaining_invitations = 0 membership.save() unused_invitations = [ x[0] for x in Invitation.objects.filter( issued_by=request.user, circle=self.circle, used=False ).values_list('code') ] data = { 'used_invitations': MembershipModelSerializer(invited_members, many=True).data, 'unused_invitations': unused_invitations } return Response(data=data, status=HTTP_200_OK)
def invitations(self, request, *args, **kwargs): """Retrieve a member's invitations breakdown. Will return a list containing all the members that have used its invitations and another list containing the invitations that haven't being used yet. """ member = self.get_object() invited_members = Membership.objects.filter(circle=self.circle, invited_by=request.user, is_active=True) # Example: # < QuerySet[('S-O2GXX7Y7',), ('O-6RTWW2ON',)] > unused_invitations = Invitation.objects.filter( circle=self.circle, issued_by=request.user, used=False).values_list('code') diff = member.remaining_invitations - len(unused_invitations) # Example # ['S-O2GXX7Y7', 'O-6RTWW2ON'] invitations = [x[0] for x in unused_invitations] # IF remaining_invitations is greater than unused_invitations # then create the ones that are needed. for i in range(0, diff): invitations.append( Invitation.objects.create(issued_by=request.user, circle=self.circle).code) data = { 'used_invitations': MembershipModelSerializer(invited_members, many=True).data, 'invitations': invitations } return Response(data)