def change_summary(self, request, queryset): form = None if 'cancel' in request.POST: self.message_user(request, 'Canceled items summarization') return elif 'summarize' in request.POST: #do the summarization form = self.SummaryForm(request.POST) if form.is_valid(): summary = form.cleaned_data['summary'] count = 0 new_summ = Summary() new_summ.user = request.user new_summ.time = queryset[0].time new_summ.time_added = queryset[0].time_added new_summ.shop = queryset[0].price.shop new_summ.summary = 0 - summary new_summ.currency = queryset[0].price.currency new_summ.save() for item in queryset: item.summary = new_summ item.save() count += 1 plural = '' if count != 1: plural = 's' self.message_user(request, "Successfully summarized %d item%s in %s." % (count, plural, summary)) return HttpResponseRedirect(request.get_full_path()) if not form: form = self.SummaryForm(initial={'_selected_action': request.POST.getlist(admin.ACTION_CHECKBOX_NAME)}) return direct_to_template(request, 'fprice/admin_set_summary.html', {'items': queryset, 'form': form, 'path':request.get_full_path()})
def trade_add(request, **kwargs): if request.method == 'POST': forma = TitleForm(request.POST) formset = TradeFormSet(request.POST) if forma.is_valid() and formset.is_valid(): # check existing shop or add new shop = None if forma.cleaned_data['shop']: shop = Shop.objects.get(id=int(forma.cleaned_data['shop'])) if shop.title != forma.cleaned_data['shop_visual']: shop = None if shop == None: shop = Shop() shop.title = forma.cleaned_data['shop_visual'] shop.save() if not forma.cleaned_data['spytrade']: new_summ = Summary() new_summ.time = forma.cleaned_data['time'] new_summ.user = request.user new_summ.shop = shop new_summ.currency = forma.cleaned_data['currency'] new_summ.summary = 0 - forma.cleaned_data['summary'] new_summ.save() for form in formset.forms: if form.has_changed(): # check existing product or add new product = None if form.cleaned_data['product']: product = Product.objects.get(id=int(form.cleaned_data['product'])) if product.title != form.cleaned_data['product_visual']: product = None if product == None: product = Product() product.title = form.cleaned_data['product_visual'] product.save() # count cost cost = "%.2f" % ( float(form.cleaned_data['price_visual']) * float(form.cleaned_data['amount']) ) # check existing shop_product or add new try: new_shopprod = ShopProduct.objects.get(shop=shop,product=product) except ShopProduct.DoesNotExist: new_shopprod = None if new_shopprod == None: new_shopprod = ShopProduct() new_shopprod.shop = shop new_shopprod.product = product new_shopprod.save() # check existing price or add new newer_price = None new_price = None try: older_price = Price.objects.filter(shop_product=new_shopprod, time__lt=forma.cleaned_data['time'], currency=forma.cleaned_data['currency'])[0] except IndexError: older_price = None try: newer_price = Price.objects.order_by('last_time_update').filter(shop_product=new_shopprod, last_time_update__gt=forma.cleaned_data['time'], currency=forma.cleaned_data['currency'])[0] except IndexError: newer_price = None if older_price: if older_price.price == form.cleaned_data['price_visual']: older_price.last_user_update = request.user if forma.cleaned_data['time'] > older_price.last_time_update: older_price.last_time_update = forma.cleaned_data['time'] older_price.update_counter += 1 older_price.save() new_price = older_price elif forma.cleaned_data['time'] < older_price.last_time_update: # split price new_newer_price = Price() new_newer_price.user = older_price.last_user_update new_newer_price.last_user_update = older_price.last_user_update new_newer_price.time = older_price.last_time_update new_newer_price.last_time_update = older_price.last_time_update new_newer_price.shop_product = new_shopprod new_newer_price.price = older_price.price new_newer_price.currency = older_price.currency new_newer_price.save() older_price.last_user_update = older_price.user older_price.last_time_update = older_price.time older_price.update_counter = 0 older_price.save() elif newer_price and newer_price.price == form.cleaned_data['price_visual']: newer_price.last_user_update = request.user newer_price.time = forma.cleaned_data['time'] newer_price.update_counter += 1 newer_price.save() new_price = newer_price if not new_price: new_price = Price() new_price.user = request.user new_price.last_user_update = request.user new_price.time = forma.cleaned_data['time'] new_price.last_time_update = forma.cleaned_data['time'] new_price.shop_product = new_shopprod new_price.price = form.cleaned_data['price_visual'] new_price.currency = forma.cleaned_data['currency'] new_price.save() # update shop_product last_price if new_shopprod.last_price: if new_shopprod.last_price.last_time_update < new_price.last_time_update: new_shopprod.last_price = new_price new_shopprod.save() else: new_shopprod.last_price = new_price new_shopprod.save() # save trade if it is not spy if not forma.cleaned_data['spytrade']: new_trade = form.save(commit=False) #new_trade.customer = request.user #new_trade.time = forma.cleaned_data['time'] new_trade.price = new_price new_trade.amount = form.cleaned_data['amount'] new_trade.cost = cost new_trade.summary = new_summ new_trade.save() form.save_m2m() return HttpResponseRedirect(reverse('price_summary_list')) else: forma = TitleForm() formset = TradeFormSet() return direct_to_template(request, 'fprice/trade_add.html',{'formset':formset, 'forma':forma})