Beispiel #1
0
 def update_coupons(self, save=False):
     if not self.draw.result:
         return
     
     if self.coupon_issue.exists():
         return
             
     CouponIssue.assign(self, user=self.user)
Beispiel #2
0
    def get(self, request, filter="active"):
        
        import datetime
        user = request.yoo["user"]
        now = datetime.datetime.now()
        cv_id = request.GET.get('cv_id', None)
        vendor = request.GET.get("vendor", None)
        
        if filter == "active":
            issued = CouponIssue.objects.filter(user=user, redeemed__isnull=True, 
                coupon__redeemable=True, coupon__valid_to__gte=now)

            if len(issued) < 5:
                for x in xrange(5):
                    CouponIssue.assign(ticket=None, user=user)
            
                issued = CouponIssue.objects.filter(user=user, redeemed__isnull=True, 
                    coupon__redeemable=True, coupon__valid_to__gte=now)
                                        
            if vendor:
                try:
                    issued = issued.filter(coupon__vendor_id=int(vendor))
                except ValueError:
                    pass
                
            issued = list(issued)
            redeemed = CouponIssue.objects.filter(user=user, redeemed__isnull=False)

            if vendor:
                try:
                    issued = redeemed.filter(coupon__vendor_id=int(vendor))
                except ValueError:
                    pass
            if not cv_id:
                return map(lambda x: x.representation(), issued + list(redeemed))
            else:
                couponvendor = CouponVendorModel.objects.get(id=cv_id)
                try:
                    coupon = CouponModel.objects.filter(vendor=couponvendor,
                                valid_from__lte=now,
                                valid_to__gte=now,
                                redeem_limit__gte=1,
                                )
                except Exception as e:
                    print e
                    raise exceptions.WebServiceException("No coupons available for this Vendor")
                if coupon:
                    return map(lambda x: x.representation(), coupon)
                else:
                    raise exceptions.WebServiceException("No coupons available for this Vendor")
                
                
        elif filter == "redeemed":
            redeemed = CouponIssue.objects.filter(user=user, redeemed__isnull=False)
            return map(lambda x: x.representation(), redeemed)
        elif filter == "expired":
            expired = CouponIssue.objects.filter(
                Q(Q(user=user)&Q(redeemed__isnull=True))
                &Q(Q(coupon__valid_to__lte=now)|Q(coupon__redeemable=False)))
            
            return map(lambda x: x.representation(), expired)
        else:
            raise exceptions.WebServiceException("Invalid Coupon Filter")