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