Exemplo n.º 1
0
def clerk_login(request, code, counter):
    try:
        counter_obj = Counter.objects.get(identifier=counter)
    except Counter.DoesNotExist:
        raise AjaxError(RET_AUTH_FAILED, _i(u"Counter has gone missing."))

    try:
        clerk = Clerk.by_code(code)
    except ValueError as ve:
        raise AjaxError(RET_AUTH_FAILED, repr(ve))

    if clerk is None:
        raise AjaxError(RET_AUTH_FAILED, _i(u"No such clerk."))

    clerk_data = clerk.as_dict()
    clerk_data['overseer_enabled'] = clerk.user.has_perm('kirppu.oversee')

    active_receipts = Receipt.objects.filter(clerk=clerk,
                                             status=Receipt.PENDING)
    if active_receipts:
        if len(active_receipts) > 1:
            clerk_data["receipts"] = [
                receipt.as_dict() for receipt in active_receipts
            ]
            clerk_data["receipt"] = "MULTIPLE"
        else:
            receipt = active_receipts[0]
            request.session["receipt"] = receipt.pk
            clerk_data["receipt"] = receipt.as_dict()

    request.session["clerk"] = clerk.pk
    request.session["clerk_token"] = clerk.access_key
    request.session["counter"] = counter_obj.pk
    return clerk_data
Exemplo n.º 2
0
Arquivo: api.py Projeto: aryla/kirppu
def clerk_login(request, code, counter):
    try:
        counter_obj = Counter.objects.get(identifier=counter)
    except Counter.DoesNotExist:
        raise AjaxError(RET_AUTH_FAILED, _i(u"Counter has gone missing."))

    try:
        clerk = Clerk.by_code(code)
    except ValueError:
        clerk = None

    if clerk is None:
        raise AjaxError(RET_AUTH_FAILED, _i(u"Unauthorized."))

    clerk_data = clerk.as_dict()

    active_receipts = Receipt.objects.filter(clerk=clerk, status=Receipt.PENDING)
    if active_receipts:
        if len(active_receipts) > 1:
            clerk_data["receipts"] = [receipt.as_dict() for receipt in active_receipts]
            clerk_data["receipt"] = "MULTIPLE"
        else:
            receipt = active_receipts[0]
            request.session["receipt"] = receipt.pk
            clerk_data["receipt"] = receipt.as_dict()

    request.session["clerk"] = clerk.pk
    request.session["clerk_token"] = clerk.access_key
    request.session["counter"] = counter_obj.pk
    return clerk_data
Exemplo n.º 3
0
Arquivo: api.py Projeto: aryla/kirppu
def item_release(request, code):
    item = _get_item_or_404(code)
    receipt_id = request.session["receipt"]
    receipt = get_object_or_404(Receipt, pk=receipt_id)

    last_added_item = ReceiptItem.objects\
        .filter(receipt=receipt, item=item, action=ReceiptItem.ADD)\
        .order_by("-add_time")

    if len(last_added_item) == 0:
        raise AjaxError(RET_CONFLICT, _i(u"Item is not added to receipt."))
    assert len(last_added_item) == 1

    last_added_item = last_added_item[0]
    last_added_item.action = ReceiptItem.REMOVED_LATER
    last_added_item.save()

    removal_entry = ReceiptItem(item=item, receipt=receipt, action=ReceiptItem.REMOVE)
    removal_entry.save()

    receipt.calculate_total()
    receipt.save()

    item.state = Item.BROUGHT
    item.save()

    return removal_entry.as_dict()
Exemplo n.º 4
0
Arquivo: api.py Projeto: aryla/kirppu
def vendor_get(request, id):
    try:
        vendor = Vendor.objects.get(pk=int(id))
    except (ValueError, Vendor.DoesNotExist):
        raise AjaxError(RET_BAD_REQUEST, _i(u"Invalid vendor id"))
    else:
        return vendor.as_dict()
Exemplo n.º 5
0
def vendor_get(request, id):
    try:
        vendor = Vendor.objects.get(pk=int(id))
    except (ValueError, Vendor.DoesNotExist):
        raise AjaxError(RET_BAD_REQUEST, _i(u"Invalid vendor id"))
    else:
        return vendor.as_dict()
Exemplo n.º 6
0
def item_release(request, code):
    item = _get_item_or_404(code)
    receipt_id = request.session["receipt"]
    receipt = get_object_or_404(Receipt, pk=receipt_id)

    last_added_item = ReceiptItem.objects\
        .filter(receipt=receipt, item=item, action=ReceiptItem.ADD)\
        .order_by("-add_time")

    if len(last_added_item) == 0:
        raise AjaxError(RET_CONFLICT, _i(u"Item is not added to receipt."))
    assert len(last_added_item) == 1

    last_added_item = last_added_item[0]
    last_added_item.action = ReceiptItem.REMOVED_LATER
    last_added_item.save()

    removal_entry = ReceiptItem(item=item,
                                receipt=receipt,
                                action=ReceiptItem.REMOVE)
    removal_entry.save()

    receipt.calculate_total()
    receipt.save()

    if item.state != Item.BROUGHT:
        ItemStateLog.objects.log_state(item=item, new_state=Item.BROUGHT)
        item.state = Item.BROUGHT
        item.save()

    return removal_entry.as_dict()
Exemplo n.º 7
0
def item_mode_change(code, from_, to, message_if_not_first=None):
    item = _get_item_or_404(code)
    if not isinstance(from_, tuple):
        from_ = (from_, )
    if item.state in from_:
        if item.hidden:
            # If an item is brought to the event, even though the user deleted it, it should begin showing again in
            # users list. The same probably applies to any interaction with the item.
            item.hidden = False

        ItemStateLog.objects.log_state(item=item, new_state=to)
        old_state = item.state
        item.state = to
        item.save()
        ret = item.as_dict()
        if message_if_not_first is not None and len(
                from_) > 1 and old_state != from_[0]:
            ret.update(_message=message_if_not_first)
        return ret

    else:
        # Item not in expected state.
        raise AjaxError(
            RET_CONFLICT,
            _i(u"Unexpected item state: {state_name} ({state})").format(
                state=item.state, state_name=item.get_state_display()),
        )
Exemplo n.º 8
0
Arquivo: api.py Projeto: aryla/kirppu
def _get_item_or_404(code):
    try:
        item = Item.get_item_by_barcode(code)
    except Item.DoesNotExist:
        item = None

    if item is None:
        raise Http404(_i(u"No item found matching '{0}'").format(code))
    return item
Exemplo n.º 9
0
def _get_item_or_404(code):
    try:
        item = Item.get_item_by_barcode(code)
    except Item.DoesNotExist:
        item = None

    if item is None:
        raise Http404(_i(u"No item found matching '{0}'").format(code))
    return item
Exemplo n.º 10
0
Arquivo: api.py Projeto: aryla/kirppu
def get_counter(request):
    """
    Get the Counter object associated with a request.

    Raise AjaxError if session is invalid or counter is not found.
    """
    if "counter" not in request.session:
        raise AjaxError(RET_UNAUTHORIZED, _i(u"Not logged in."))

    counter_id = request.session["counter"]
    try:
        counter_object = Counter.objects.get(pk=counter_id)
    except Counter.DoesNotExist:
        raise AjaxError(
            RET_UNAUTHORIZED,
            _i(u"Counter has gone missing."),
        )

    return counter_object
Exemplo n.º 11
0
def get_counter(request):
    """
    Get the Counter object associated with a request.

    Raise AjaxError if session is invalid or counter is not found.
    """
    if "counter" not in request.session:
        raise AjaxError(RET_UNAUTHORIZED, _i(u"Not logged in."))

    counter_id = request.session["counter"]
    try:
        counter_object = Counter.objects.get(pk=counter_id)
    except Counter.DoesNotExist:
        raise AjaxError(
            RET_UNAUTHORIZED,
            _i(u"Counter has gone missing."),
        )

    return counter_object
Exemplo n.º 12
0
Arquivo: api.py Projeto: aryla/kirppu
def item_mode_change(code, from_, to):
    item = _get_item_or_404(code)
    if item.state == from_:
        item.state = to
        item.save()
        return item.as_dict()

    else:
        # Item not in expected state.
        raise AjaxError(
            RET_CONFLICT,
            _i(u"Unexpected item state: {state}").format(state=item.state),
        )
Exemplo n.º 13
0
Arquivo: api.py Projeto: aryla/kirppu
def get_clerk(request):
    """
    Get the Clerk object associated with a request.

    Raise AjaxError if session is invalid or clerk is not found.
    """
    for key in ["clerk", "clerk_token", "counter"]:
        if key not in request.session:
            raise AjaxError(RET_UNAUTHORIZED, _i(u"Not logged in."))

    clerk_id = request.session["clerk"]
    clerk_token = request.session["clerk_token"]

    try:
        clerk_object = Clerk.objects.get(pk=clerk_id)
    except Clerk.DoesNotExist:
        raise AjaxError(RET_UNAUTHORIZED, _i(u"Clerk not found."))

    if clerk_object.access_key != clerk_token:
        return AjaxError(RET_UNAUTHORIZED, _i(u"Bye."))

    return clerk_object
Exemplo n.º 14
0
def get_clerk(request):
    """
    Get the Clerk object associated with a request.

    Raise AjaxError if session is invalid or clerk is not found.
    """
    for key in ["clerk", "clerk_token", "counter"]:
        if key not in request.session:
            raise AjaxError(RET_UNAUTHORIZED, _i(u"Not logged in."))

    clerk_id = request.session["clerk"]
    clerk_token = request.session["clerk_token"]

    try:
        clerk_object = Clerk.objects.get(pk=clerk_id)
    except Clerk.DoesNotExist:
        raise AjaxError(RET_UNAUTHORIZED, _i(u"Clerk not found."))

    if clerk_object.access_key != clerk_token:
        return AjaxError(RET_UNAUTHORIZED, _i(u"Bye."))

    return clerk_object
Exemplo n.º 15
0
def get_clerk_codes(request):
    if not request.user.is_staff:
        return HttpResponseForbidden(_i(u"Forbidden"))
    bar_type = request.GET.get("format", "svg").lower()

    if bar_type not in ('svg', 'png'):
        return HttpResponseBadRequest(u"Image extension not supported")

    items = []
    code_item = namedtuple("CodeItem", "name code")

    for c in Clerk.objects.filter(access_key__isnull=False):
        code = c.get_code()
        if c.user is not None:
            name = c.user.get_short_name()
            if len(name) == 0:
                name = c.user.get_username()
        else:
            name = ""

        items.append(code_item(name=name, code=code))

    return render(request, "app_clerks.html", {'items': items, 'bar_type': bar_type})
Exemplo n.º 16
0
# drop table bc_competitorstats cascade; drop table bc_competitor cascade; drop table bc_competition cascade; drop table bc_competition_competitors cascade; drop table tastypie_apiaccess; drop table tastypie_apikey; delete from django_migrations where app in ('bc', 'tastypie');
# insert into bc_competitor (id, name) values (1,'First')(2, 'Second'); insert into bc_competition (id, type, status) values (1, 1, 0);
#

from datetime import datetime
from django.db import models
from django.utils.translation import ugettext_lazy as _i

# Create your models here.

COMPETITION_STATUS_PENDING = 0
COMPETITION_STATUS_STARTED = 1
COMPETITION_STATUS_FINISHED = 2
COMPETITION_STATUSES = (
    (COMPETITION_STATUS_PENDING, _i(u'Pending')),
    (COMPETITION_STATUS_STARTED, _i(u'Started')),
    (COMPETITION_STATUS_FINISHED, _i(u'Finished')),
)

COMPETITION_TYPE_TIME = 0
COMPETITION_TYPE_DISTANCE = 1
COMPETITION_TYPES = (
    (COMPETITION_TYPE_TIME, _i(u'Time Competition')),
    (COMPETITION_TYPE_DISTANCE, _i(u'Distance Competition')),
)

DEFAULT_LIMIT_DISTANCE = 50.0
DEFAULT_LIMIT_TIME = 30.0
DEFAULT_LIMITS = (
    (DEFAULT_LIMIT_DISTANCE, _i(u'Default Distance Limit')),
    (DEFAULT_LIMIT_TIME, _i(u'Default Time Limit')),
Exemplo n.º 17
0
 def __str__(self):
     label = ''
     if hasattr(self, 'photo'):
         label = self.photo and self.photo.title
     label = label or self.pk
     return _i("{} exif".format(label))
Exemplo n.º 18
0
 def inner(request, *args, **kwargs):
     current_value = getattr(settings, setting, None)
     if not ((not callback and current_value == value) or (callback and value(current_value))):
         return HttpResponseForbidden(_i(u"Forbidden"))
     return fn(request, *args, **kwargs)
Exemplo n.º 19
0
 def wrapper(request, *args, **kwargs):
     clerk = get_clerk(request)
     if not clerk.user.has_perm('kirppu.oversee'):
         raise AjaxError(RET_FORBIDDEN, _i(u"Access denied."))
     return func(request, *args, **kwargs)
Exemplo n.º 20
0
def item_checkout(request, code):
    return item_mode_change(code,
                            (Item.BROUGHT, Item.ADVERTISED), Item.RETURNED,
                            _i(u"Item was not brought to event."))