Example #1
0
def move_item(request, item_id, collection_id):
    if request.method != 'POST':
        raise ErrorWithMessage("This page may only be accessed through the "
                               "proper form.")
    from_collection = get_object_or_404(Collection, id=collection_id)
    item = get_item_for_collector(item_id, request.user.collector)
    check_item_is_in_collection(request, item, from_collection)
    collection_form = CollectionSelectForm(request.user.collector, None,
                                           request.POST)
    if collection_form.is_valid():
        to_collection_id = collection_form.cleaned_data['collection']
        to_collection = get_object_or_404(Collection, id=to_collection_id)

        check_item_is_not_in_collection(request, item, to_collection)
        if 'move' in request.POST:
            from_collection.items.remove(item)
            item.collections.add(to_collection)
            messages.success(request, _("Item moved between collections"))
            if to_collection.own_used and to_collection.own_default is not None:
                if to_collection.own_default != item.own:
                    item.own = to_collection.own_default
                    item.save()
                    messages.warning(
                        request,
                        mark_safe(
                            _("Item status for own/want differed from"
                              " default of new collection <b>%s</b> and "
                              "was changed." % esc(to_collection.name))))
            return HttpResponseRedirect(
                urlresolvers.reverse('view_item',
                                     kwargs={
                                         'item_id': item_id,
                                         'collection_id': to_collection_id
                                     }))
        elif 'copy' in request.POST:
            item.collections.add(to_collection)
            messages.success(request, _("Item copied between collections"))
            if to_collection.own_used and \
               to_collection.own_default is not None:
                if to_collection.own_default != item.own:
                    messages.warning(
                        request,
                        _("Own/want default for new "
                          "collection differs from item status."))
        else:
            messages.error(request, _("Item unchanged"))
    else:
        messages.error(request, _("Item unchanged"))

    return HttpResponseRedirect(
        urlresolvers.reverse('view_item',
                             kwargs={
                                 'item_id': item_id,
                                 'collection_id': collection_id
                             }))
Example #2
0
def view_item(request, item_id, collection_id):
    collection = get_object_or_404(Collection, id=collection_id)
    collector = collection.collector
    if request.user.is_authenticated and \
       collector == request.user.collector:
        item = get_item_for_collector(item_id, request.user.collector)
    elif collection.public is True:
        item = get_item_for_collector(item_id, collector)
    elif not request.user.is_authenticated:
        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
    else:
        raise PermissionDenied

    check_item_is_in_collection(request, item, collection)

    sell_date_form = None
    acquisition_date_form = None
    if request.user.is_authenticated and \
       collector == request.user.collector:
        initial = {}
        if collector.default_currency:
            if not item.price_paid_currency:
                initial['price_paid_currency'] = collector.default_currency
            if not item.sell_price_currency:
                initial['sell_price_currency'] = collector.default_currency
            if not item.market_value_currency:
                initial['market_value_currency'] = collector.default_currency
        item_form = CollectionItemForm(collector,
                                       instance=item,
                                       initial=initial)
        if item.collections.filter(sell_date_used=True).exists():
            sell_date_form = DateForm(instance=item.sell_date,
                                      prefix='sell_date')
            sell_date_form.fields['date'].label = _('Sell date')
        if item.collections.filter(acquisition_date_used=True).exists():
            acquisition_date_form = DateForm(instance=item.acquisition_date,
                                             prefix='acquisition_date')
            acquisition_date_form.fields['date'].label = _('Acquisition date')
        collection_form = CollectionSelectForm(
            collector, excluded_collections=item.collections.all())
        other_collections = item.collections.exclude(id=collection.id)
    else:
        item_form = None
        collection_form = None
        other_collections = None

    # TODO with django1.6 use first/last here
    item_before = collection.items.filter(issue__series__sort_name__lte=
                                          item.issue.series.sort_name)\
                                  .exclude(issue__series__sort_name=
                                           item.issue.series.sort_name,
                                           issue__series__year_began__gt=
                                           item.issue.series.year_began)\
                                  .exclude(issue__series_id=
                                           item.issue.series.id,
                                           issue__sort_code__gt=
                                           item.issue.sort_code)\
                                  .exclude(issue__series_id=
                                           item.issue.series.id,
                                           issue__sort_code=
                                           item.issue.sort_code,
                                           id__gte=item.id).reverse()

    if item_before:
        page = int(item_before.count() / DEFAULT_PER_PAGE + 1)
        item_before = item_before[0]
    else:
        page = 1
    item_after = collection.items.filter(issue__series__sort_name__gte=
                                         item.issue.series.sort_name)\
                                 .exclude(issue__series__sort_name=
                                          item.issue.series.sort_name,
                                          issue__series__year_began__lt=
                                          item.issue.series.year_began)\
                                 .exclude(issue__series_id=
                                          item.issue.series.id,
                                          issue__sort_code__lt=
                                          item.issue.sort_code)\
                                 .exclude(issue__series_id=
                                          item.issue.series.id,
                                          issue__sort_code=
                                          item.issue.sort_code,
                                          id__lte=item.id)
    if item_after:
        item_after = item_after[0]

    return render(
        request, COLLECTION_ITEM_TEMPLATE, {
            'item': item,
            'item_form': item_form,
            'item_before': item_before,
            'item_after': item_after,
            'page': page,
            'collection': collection,
            'other_collections': other_collections,
            'sell_date_form': sell_date_form,
            'acquisition_date_form': acquisition_date_form,
            'collection_form': collection_form
        })