示例#1
0
def shop_edit_featured(request, shop_id, page=0, template_name='fprice/shop_edit_featured.html', **kwargs):
    shop = Shop.objects.get(id=shop_id)
    products = Product.objects.filter(is_featured=True)
    PriceFormSetA = formset_factory(PriceForm, extra=0)

    if request.method == 'POST':
        formset = PriceFormSetA(request.POST)

        if formset.is_valid():
            for form in formset.forms:
                if form.has_changed() and form.cleaned_data['price_visual']:
                    # check shop_product
                    product = Product.objects.get(id=form.cleaned_data['product'])

                    # 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
                    try:
                        new_price = Price.objects.get(shop_product=new_shopprod, price=form.cleaned_data['price_visual'])
                    except Price.DoesNotExist:
                        new_price = None
                    if (new_price == None):
                        # TODO form has changed not working
                        new_price = Price()
                        new_price.user = request.user
                        new_price.last_user_update = request.user
                        new_price.time = datetime.datetime.now()#forma.cleaned_data['time']
                        new_price.last_time_update = datetime.datetime.now()#forma.cleaned_data['time']
                        new_price.shop_product = new_shopprod
                        new_price.price = form.cleaned_data['price_visual']
                        new_price.currency = 'rur'#forma.cleaned_data['currency']
                        new_price.save()
                    else:
                        if datetime.datetime.now() > new_price.last_time_update:
                            new_price.last_user_update = request.user
                            new_price.last_time_update = datetime.datetime.now()#forma.cleaned_data['time']
                        if datetime.datetime.now() < new_price.time:
                            new_price.time = datetime.datetime.now()#forma.cleaned_data['time']
                        new_price.update_counter += 1
                        new_price.save()

                    # update shop_product last_price
                    # TODO check if not 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()

            return HttpResponseRedirect(reverse('price_shop_detail', args=[shop_id]))
    else:
        initial_data_list = []
        for item in products:
            initial_data_list.append({'product':item.id, 'product_visual':item.title})
        formset = PriceFormSetA(initial=initial_data_list)

    return direct_to_template(request, 'fprice/shop_edit_featured.html',{'shop':shop, 'formset':formset})
示例#2
0
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})