def save_item(request, item_id, collection_id): if request.method == 'POST': collection = get_object_or_404(Collection, id=collection_id) item = get_item_for_collector(item_id, request.user.collector) item_form = CollectionItemForm(request.user.collector, request.POST, instance=item) item_form_valid = item_form.is_valid() if item.collections.filter(sell_date_used=True).exists(): sell_date_form = DateForm(request.POST, instance=item.sell_date, prefix='sell_date') sell_date_form_valid = sell_date_form.is_valid() else: sell_date_form = None sell_date_form_valid = True if item.collections.filter(acquisition_date_used=True).exists(): acquisition_date_form = DateForm(request.POST, instance=item.acquisition_date, prefix='acquisition_date') acquisition_date_form_valid = acquisition_date_form.is_valid() else: acquisition_date_form = None acquisition_date_form_valid = True if item_form_valid: if sell_date_form_valid and acquisition_date_form_valid: if sell_date_form: sell_date = sell_date_form.save() else: sell_date = None if acquisition_date_form: acquisition_date = acquisition_date_form.save() else: acquisition_date = None item = item_form.save(commit=False) item.acquisition_date = acquisition_date item.sell_date = sell_date item.save() item_form.save_m2m() messages.success(request, _('Item saved.')) else: if sell_date_form: sell_date_form.fields['date'].label = _('Sell date') if acquisition_date_form: acquisition_date_form.fields['date'].label = \ _('Acquisition date') messages.error(request, _('Date was entered in wrong format.')) return render_to_response(COLLECTION_ITEM_TEMPLATE, {'item': item, 'item_form': item_form, 'collection': collection, 'sell_date_form': sell_date_form, 'acquisition_date_form': acquisition_date_form}, context_instance=RequestContext(request)) else: messages.error(request, _('Some data was entered incorrectly.')) return render_to_response(COLLECTION_ITEM_TEMPLATE, {'item': item, 'item_form': item_form, 'collection': collection, 'sell_date_form': sell_date_form, 'acquisition_date_form': acquisition_date_form}, context_instance=RequestContext(request)) return HttpResponseRedirect( urlresolvers.reverse('view_item', kwargs={'item_id': item_id, 'collection_id': collection_id})) raise Http404
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 == 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__sort_code__gt= item.issue.sort_code)\ .exclude(issue__series__sort_name= item.issue.series.sort_name, issue__sort_code= item.issue.sort_code, id__gte=item.id).reverse() if item_before: page = 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__sort_code__lt= item.issue.sort_code)\ .exclude(issue__series__sort_name= item.issue.series.sort_name, issue__sort_code= item.issue.sort_code, id__lte=item.id) if item_after: item_after = item_after[0] return render_to_response(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}, context_instance=RequestContext(request))