Exemple #1
0
def authorization_add(request, radius_username, account):
    """
    Add a new authorization to the specified user.

    Required user level: Manager

    Returns the authorization on success.

    radius_username    -- RADIUS username to search for.
    account            -- Bank account number.

    Example return value:
    {
        "account": "NL13TEST0123456789",
        "id": 1,
        "end_date": null,
        "start_date": "2014-09-21T14:16:06+00:00",
        "user": "******"
    }

    Raises error -32602 (Invalid params) if the radius_username does not exist.
    """

    try:
        user = User.objects.get(authenticationdata__backend=RADIUS_BACKEND_NAME,
                                authenticationdata__username=radius_username)
    except User.DoesNotExist:
        raise InvalidParametersError('User with provided radius_username does not exits')

    authorization = Authorization(user=user, organization=request.organization, account=account)
    authorization.save()

    return format_authorization(authorization)
Exemple #2
0
    def test_authorization_ended(self):
        """
        Test the authorization.list call.
        """

        # '2014-09-21T14:16:06+00:00'
        start_date = timezone.make_aware(
            datetime.datetime(2014, 9, 21, 14, 16, 6), timezone.utc)
        # '2015-04-16T02:56:33+00:00'
        end_date = timezone.make_aware(
            datetime.datetime(2015, 4, 16, 2, 56, 33), timezone.utc)

        # Create authorization
        auth = Authorization(user=self.data['user1'],
                             organization=self.data['organization1'],
                             account='NL13TEST0123456789',
                             start_date=start_date,
                             end_date=end_date)

        auth_json = {
            'id': auth.id,
            'user': self.data['user1'].username,
            'user_id': self.data['user1'].id,
            'start_date': '2014-09-21T14:16:06+00:00',
            'end_date': '2015-04-16T02:56:33+00:00',
            'account': 'NL13TEST0123456789',
        }

        self.convertAndAssertJSONEqual(format_authorization(auth), auth_json)
Exemple #3
0
def juliana_rfid_get(request, event_id, rfid):
    event = _get_validate_event(request, event_id)

    identifier = rfid_to_identifier(rfid=rfid)

    try:
        card = RfidCard.objects.get(identifier=identifier, is_active=True)
    except RfidCard.DoesNotExist:
        raise InvalidParamsError('RFID card not found')

    user = card.user
    authorization = Authorization.get_for_user_event(user, event)

    if not authorization:
        raise InvalidParamsError('No authorization found for user')

    res = {
        'user': {
            'id': user.pk,
            'first_name': user.first_name,
            'last_name': user.last_name,
            'username': user.username,
        },
        'authorization': format_authorization(authorization),
    }

    return res
Exemple #4
0
def juliana_rfid_get(request, event_id, rfid):
    event = _get_validate_event(request, event_id)

    identifier = rfid_to_identifier(rfid=rfid)

    try:
        card = RfidCard.objects.get(identifier=identifier, is_active=True)
    except RfidCard.DoesNotExist:
        raise InvalidParamsError('RFID card not found')

    user = card.user
    authorization = Authorization.get_for_user_event(user, event)

    if not authorization:
        raise InvalidParamsError('No authorization found for user')

    res = {
        'user': {
            'id': user.pk,
            'first_name': user.first_name,
            'last_name': user.last_name,
            'username': user.username,
        },
        'authorization': format_authorization(authorization),
    }

    return res
Exemple #5
0
    def test_authorization_without_account(self):
        """
        Test the authorization.list call.
        """

        # '2014-09-21T14:16:06+00:00'
        start_date = timezone.make_aware(
            datetime.datetime(2014, 9, 21, 14, 16, 6), timezone.utc)

        # Create authorization
        auth = Authorization(user=self.data['user1'],
                             organization=self.data['organization1'],
                             account=None,
                             start_date=start_date)

        auth_json = {
            'id': auth.id,
            'user': self.data['user1'].username,
            'user_id': self.data['user1'].id,
            'start_date': '2014-09-21T14:16:06+00:00',
            'end_date': None,
            'account': None,
        }

        self.convertAndAssertJSONEqual(format_authorization(auth), auth_json)
Exemple #6
0
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
Exemple #7
0
def authorization_add(request, radius_username, account):
    """
    Add a new authorization to the specified user.

    Required user level: Manager

    Returns the authorization on success.

    radius_username    -- RADIUS username to search for.
    account            -- Bank account number.

    Example return value:
    {
        "account": "NL13TEST0123456789",
        "id": 1,
        "end_date": null,
        "start_date": "2014-09-21T14:16:06+00:00",
        "user": "******"
    }

    Raises error -32602 (Invalid params) if the radius_username does not exist.
    """

    try:
        user = User.objects.get(
            authenticationdata__backend=RADIUS_BACKEND_NAME,
            authenticationdata__username=radius_username)
    except User.DoesNotExist:
        raise InvalidParametersError(
            'User with provided radius_username does not exits')

    authorization = Authorization(user=user,
                                  organization=request.organization,
                                  account=account)
    authorization.save()

    return format_authorization(authorization)
Exemple #8
0
def juliana_rfid_get(request, event_id, rfid):
    event = _get_validate_event(request, event_id)
    card = _retrieve_rfidcard(rfid)
    user = card.user
    authorization = Authorization.get_for_user_event(user, event)

    if not authorization:
        raise InvalidParamsError('No authorization found for user')

    res = {
        'user': {
            'id': user.pk,
            'first_name': user.first_name,
            'last_name': user.last_name,
            'username': user.username,
        },
        'authorization': format_authorization(authorization),
    }

    return res
Exemple #9
0
    def load_billing_data(self):
        data = self.data

        data['pricegroup1'] = PriceGroup(organization=data['organization1'],
                                         name='Price group 1')
        data['pricegroup1'].save()

        data['productgroup1'] = ProductGroup(
            organization=data['organization1'], name='Product group 1')
        data['productgroup1'].save()

        data['permantentproduct1'] = PermanentProduct(
            productgroup=data['productgroup1'],
            organization=data['organization1'],
            position=0)
        data['permantentproduct1'].save()

        data['authorization1'] = Authorization(
            user=data['user1'],
            organization=data['organization1'],
            start_date=data['datetime1'])
        data['authorization1'].save()
Exemple #10
0
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
Exemple #11
0
    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)
Exemple #12
0
    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)