def load_billing_order_data(self): data = self.data data['order1'] = Order(event=data['event1'], authorization=data['authorization1'], placed_at=data['datetime2'], added_by=data['user1']) data['order1'].save() Purchase(order=data['order1'], product=data['permantentproduct1'], amount=1, price=0.50).save() Purchase(order=data['order1'], product=data['temporaryproduct1'], amount=2, price=4.66).save() data['order1'].save() data['order2'] = Order(event=data['event1'], authorization=data['authorization1'], placed_at=data['datetime2'], added_by=data['user1']) data['order2'].save() Purchase(order=data['order2'], product=data['permantentproduct1'], amount=3, price=1.50).save() Purchase(order=data['order2'], product=data['temporaryproduct1'], amount=4, price=9.32).save() data['order2'].save()
def handle(self, *args, **options): if not settings.DEBUG: raise CommandError('This command only runs if DEBUG is True.') if len(args) < 2: raise CommandError( 'This command requires an event id and a number of orders.') try: event_pk = int(args[0]) except ValueError: raise CommandError('First argument is not an integer.') try: order_count = int(args[1]) except ValueError: raise CommandError('Second argument is not an integer.') try: event = Event.objects.get(pk=event_pk) except Event.DoesNotExist: raise CommandError('Event not found.') organizer = event.organizer now = timezone.now() authorizations = organizer.authorizations.filter( Q(end_date__gte=now) | Q(end_date__isnull=True), start_date__lte=now) user = User.objects.get(id=1) if not authorizations: raise CommandError('No active authorizations for organizer.') products = PermanentProduct.objects.filter( productgroup__pricegroups__events=event) for i in range(order_count): print('-----') product_count = random.randint(1, 10) print('%s products' % product_count) order = Order(event=event, authorization=random.choice(authorizations), added_by=user) order.save() for j in range(product_count): product = random.choice(products) amount = random.randint(1, 10) price = amount * product.get_price(event) purchase = Purchase(order=order, product=product, amount=amount, price=price) purchase.save()
def juliana_order_save(request, event_id, user_id, purchases, rfid_data): """Saves a new order in the database""" event = _get_validate_event(request, event_id) rfid_identifier = rfid_to_identifier(rfid=rfid_data) try: user = User.objects.get(pk=user_id) rfidcard = RfidCard.objects.get(identifier=rfid_identifier, is_active=True) except User.DoesNotExist: raise InvalidParamsError('User does not exist') except RfidCard.DoesNotExist: raise InvalidParamsError('RFID card not found') cur_user = request.user authorization = Authorization.get_for_user_event(user, event) if not authorization: raise InvalidParamsError('No authorization available') order = Order(event=event, authorization=authorization, added_by=cur_user, rfidcard=rfidcard) order.save() for p in purchases: try: product = Product.objects.get(pk=p['product']) except Product.DoesNotExist: raise InvalidParamsError('Product %s not found' % p['product']) if product.is_permanent: product = product.permanentproduct if product.organization != event.organizer \ or product.productgroup not in event.pricegroup.productgroups.all(): raise InvalidParamsError('Product %s is not available for this event' % p['product']) elif product.is_temporary: product = product.temporaryproduct if event != product.event: raise InvalidParamsError('Product %s is not available for this event' % p['product']) else: raise OtherError('Product %s is broken' % p['product']) amount = p['amount'] if p['amount'] <= 0: raise InvalidParamsError('Zero or negative amount not allowed') price = amount * product.get_price(event) if price != p['price'] / Decimal(100): raise InvalidParamsError('Price for product %s is incorrect' % p['product']) purchase = Purchase(order=order, product=product, amount=amount, price=price) purchase.save() order.save(force_update=True) # ensure order.amount is correct return True
def handle(self, *args, **options): if not settings.DEBUG: raise CommandError('This command only runs if DEBUG is True.') if len(args) < 2: raise CommandError('This command requires an event id and a number of orders.') try: event_pk = int(args[0]) except ValueError: raise CommandError('First argument is not an integer.') try: order_count = int(args[1]) except ValueError: raise CommandError('Second argument is not an integer.') try: event = Event.objects.get(pk=event_pk) except Event.DoesNotExist: raise CommandError('Event not found.') organizer = event.organizer now = timezone.now() authorizations = organizer.authorizations.filter(Q(end_date__gte=now) | Q(end_date__isnull=True), start_date__lte=now) user = User.objects.get(id=1) if not authorizations: raise CommandError('No active authorizations for organizer.') products = PermanentProduct.objects.filter(productgroup__pricegroups__events=event) for i in range(order_count): print('-----') product_count = random.randint(1, 10) print('%s products' % product_count) order = Order(event=event, authorization=random.choice(authorizations), added_by=user) order.save() for j in range(product_count): product = random.choice(products) amount = random.randint(1, 10) price = amount * product.get_price(event) purchase = Purchase(order=order, product=product, amount=amount, price=price) purchase.save()
def juliana_order_save(request, event_id, user_id, purchases, rfid_data): """Saves a new order in the database""" event = _get_validate_event(request, event_id) rfid_identifier = rfid_to_identifier(rfid=rfid_data) try: user = User.objects.get(pk=user_id) rfidcard = RfidCard.objects.get(identifier=rfid_identifier, is_active=True) except User.DoesNotExist: raise InvalidParamsError('User does not exist') except RfidCard.DoesNotExist: raise InvalidParamsError('RFID card not found') cur_user = request.user authorization = Authorization.get_for_user_event(user, event) if not authorization: raise InvalidParamsError('No authorization available') order = Order(event=event, authorization=authorization, added_by=cur_user, rfidcard=rfidcard) order.save() for p in purchases: if p['product'][0:7] != 'product': raise InvalidParamsError('Product %s invalid' % p['product']) product_pk = int(p['product'][7:]) try: product = Product.objects.get(pk=product_pk) except Product.DoesNotExist: raise InvalidParamsError('Product %s not found' % p['product']) if product.is_permanent: product = product.permanentproduct if product.organization != event.organizer \ or product.productgroup not in event.pricegroup.productgroups.all(): raise InvalidParamsError( 'Product %s is not available for this event' % p['product']) elif product.is_temporary: product = product.temporaryproduct if event != product.event: raise InvalidParamsError( 'Product %s is not available for this event' % p['product']) else: raise OtherError('Product %s is broken' % p['product']) amount = p['amount'] if p['amount'] <= 0: raise InvalidParamsError('Zero or negative amount not allowed') price = amount * product.get_price(event) if price != p['price'] / Decimal(100): raise InvalidParamsError('Price for product %s is incorrect' % p['product']) purchase = Purchase(order=order, product=product, amount=amount, price=price) purchase.save() order.save(force_update=True) # ensure order.amount is correct return True
def test_order_unsynchronized(self): starts_at = timezone.make_aware( datetime.datetime(2014, 9, 21, 14, 16, 6), timezone.utc) ends_at = starts_at + datetime.timedelta(hours=1) placed_at = starts_at + datetime.timedelta(minutes=30) placed_at_string = '2014-09-21T14:46:06+00:00' pricegroup = PriceGroup(organization=self.data['organization1'], name='Price group') pricegroup.save() event = Event(organizer=self.data['organization1'], name='Test event', starts_at=starts_at, ends_at=ends_at, pricegroup=pricegroup, kegs=1) event.save() productgroup = ProductGroup(organization=self.data['organization1'], name='Product group') productgroup.save() product1 = PermanentProduct(productgroup=productgroup, organization=self.data['organization1'], position=0) product1.save() product2 = TemporaryProduct(event=event, price=2.33) product2.save() authorization = Authorization(user=self.data['user1'], organization=self.data['organization1'], start_date=starts_at) authorization.save() order = Order(event=event, authorization=authorization, placed_at=placed_at, added_by=self.data['user1']) order.save() Purchase(order=order, product=product1, amount=1, price=0.50).save() Purchase(order=order, product=product2, amount=2, price=4.66).save() order.save() order_json = { 'id': order.id, 'rfid': None, 'event': { 'id': event.id, 'name': 'Test event', }, 'authorization': format_authorization(authorization), 'synchronized': False, 'placed_at': placed_at_string, 'purchases': [{ 'product': { 'id': product1.id, 'name': '', }, 'amount': 1, 'price': '0.50', }, { 'product': { 'id': product2.id, 'name': '', }, 'amount': 2, 'price': '4.66', }], } self.convertAndAssertJSONEqual(format_order(order), order_json)