Beispiel #1
0
def serialize_asset(asset, bundled=False, reservation=False, customer=False):
    objects = defaultdict(dict)

    reference = reference_model('assets:asset', asset)
    result = serialize_model(asset)[asset.pk]

    result['condition'] = asset.get_condition_display()

    result['product'] = reference_model('assets:product', asset.product)
    serialized_product = serialize_model(asset.product)
    if asset.product.manufacturer:
        serialized_product['manufacturer'] = reference_model('assets:manufacturer', asset.product.manufacturer)
        objects['assets:manufacturer'] = serialize_model(asset.product.manufacturer)
    objects['assets:product'].update(serialized_product)

    result['available'] = asset.is_available()
    if result['available']:
        due_date = asset.get_due_date()
        if due_date:
            result['due_date'] = due_date.strftime('%m/%d/%Y %I:%M %p')

    if bundled:
        bundled = asset.bundled_assets.all().order_by('bundle_order')
        result['bundled'] = reference_models('assets:asset', bundled)
        for bundled_asset in bundled:
            bundled_result, bundled_objects = serialize_asset(bundled_asset,
                bundled=False, reservation=reservation, customer=customer)
            for type, type_objects in bundled_objects.items():
                objects[type].update(type_objects)

    if reservation:
        reservation = asset.get_current_reservation()
        if reservation:
            result['reservation'] = reference_model('reservations:reservation', reservation)
            serialized_reservation = serialize_model(reservation)
            serialized_reservation[reservation.pk]['transaction'] = reference_model(
                'reservations:transaction', reservation.transaction_out)
            serialized_transaction = serialize_model(reservation.transaction_out)
            if customer:
                customer, customer_objects = serialize_user(reservation.transaction_out.customer,
                    fines=True, reservations=False)
                serialized_transaction[reservation.transaction_out.pk]['customer'] = customer
                for type, type_objects in customer_objects.items():
                    objects[type].update(type_objects)
            objects['reservations:transaction'].update(serialized_transaction)
            objects['reservations:reservation'].update(serialized_reservation)

    objects['assets:asset'].update({asset.pk: result})
    return reference, objects
Beispiel #2
0
def serialize_user(user, fines=True, reservations=True):
    from pendle.assets.views import serialize_asset

    objects = defaultdict(dict)

    fields = ["username", "first_name", "last_name", "groups", "email"]
    reference = reference_model("auth:user", user)
    result = serialize_model(user, fields=fields)[user.pk]

    departments = user.departments.all()
    result["departments"] = reference_models("institution:department", departments)
    objects["institution:department"].update(serialize_models(departments, fields=["name"]))

    if fines:
        result["fines"] = max(0, Fine.objects.get_amount_due(user))

    if reservations:
        reservations = Reservation.objects.checked_out(transaction_out__customer=user).select_related()
        result["reservations"] = reference_models("reservations:reservation", reservations)

        serialized_reservations = serialize_models(reservations)

        transactions = []
        for reservation in reservations:
            transaction = reservation.transaction_out
            transactions.append(transaction)
            serialized_reservation = serialized_reservations[reservation.pk]
            serialized_reservation["transaction"] = reference_model("reservations:transaction", transaction)
            asset, reservation_objects = serialize_asset(reservation.asset, bundled=True)
            serialized_reservation["asset"] = asset
            for type, type_objects in reservation_objects.items():
                objects[type].update(type_objects)
            serialized_reservation["overdue"] = reservation.is_overdue()

        serialized_transactions = serialize_models(transactions)

        for transaction in transactions:
            serialized_transactions[transaction.pk]["customer"] = reference

        objects["reservations:transaction"].update(serialized_transactions)
        objects["reservations:reservation"].update(serialized_reservations)

    objects["auth:user"].update({user.pk: result})
    return reference, objects