def warren(request): dateform = DateForm(request.GET or None) if dateform.is_valid(): date = dateform.cleaned_data.get('date',datetime.date.today()) else: date = datetime.date.today() queryset = Warren.objects.filter(date=date).prefetch_related('cause') order = tuple(f.order for f in queryset) if order: order = max(order)+1 else: order = 1 initial= [{'date':date,'order':i} for i in range(order,WarrenFactory.extra+order+1)] factory = WarrenFactory(request.POST or None, queryset=queryset,initial=initial,prefix='tto') if request.method == 'POST' and factory.is_valid(): factory.save() for w in Warren.objects.filter(date=date).order_by('-order'): if w.tts: source = w w.source = source w.forming = Forming.objects.filter(date__lt=date).filter(tts=w.tts).order_by('-date')[0] w.save() messages.success(request,'Садка сохранена') return redirect(reverse('make:warren')+'?date=%s' % date.isoformat()) else: cause = Cause.objects.filter(type='warren') for form in factory: form.fields['cause'].queryset = cause for form in factory: print form.prefix for k,v in form.data.items(): if form.prefix in k and not form.is_valid(): print '\t',k,v return render(request,'make/warren.html',dict(factory=factory,date=date,dateform=dateform))
def forming(request): dateform = DateForm(request.GET or None) if dateform.is_valid(): date = dateform.cleaned_data.get('date', datetime.date.today()) else: date = datetime.date.today() filter = {'date': date} queryset = Forming.objects.filter(**filter).prefetch_related('bars', 'raws', 'halfs') order = max(f.order for f in queryset or (Forming(),)) + 1 initial = [{'date': date, 'order': i} for i in range(order, FormingFactory.extra + order + 1)] factory = FormingFactory(request.POST or None, initial=initial, queryset=queryset, prefix='forming') instance = queryset[0] if len(queryset) > 0 else None widthcolor = WidthColorForm(request.POST or None, instance=instance) if request.method == 'POST' and factory.is_valid() and widthcolor.is_valid(): factory.save() Forming.objects.filter(date=date).update(**widthcolor.cleaned_data) messages.success(request, 'Формовка сохранена') return redirect(reverse('make:forming') + '?date=%s&s=%d' % (date.isoformat(), id(factory))) return render(request, 'make/forming.html', {'factory': factory, 'date': date, 'dateform': dateform, 'widthcolor': widthcolor})
def warren(request): dateform = DateForm(request.GET or None) if dateform.is_valid(): date = dateform.cleaned_data.get('date', datetime.date.today()) else: date = datetime.date.today() timedelta = datetime.timedelta(20) delay = datetime.timedelta(1) queryset = Warren.objects.filter(date=date).select_related('forming', 'forming__width', 'part').prefetch_related( 'part__batch', 'part__rows', 'part__batch__width', 'cause') order = max(f.order for f in queryset or (Warren(),)) + 1 initial = [{'date': date, 'order': i} for i in range(order, WarrenFactory.extra + order + 1)] factory = WarrenFactory(request.POST or None, queryset=queryset, initial=initial, prefix='tto') if request.method == 'POST' and factory.is_valid(): factory.save() try: source = Warren.objects.filter(tto__isnull=False, date=date - datetime.timedelta(1)).latest('order') except Warren.DoesNotExist: source = None for w in Warren.objects.filter(date=date).order_by('order'): if w.tto: source = w w.source = source try: w.forming = Forming.objects.filter(date__lt=w.date - delay, date__gte=w.date - timedelta).filter( tts=w.tts).order_by('-date')[0] Warren.objects.filter(forming__id=w.forming_id).update(forming=None) except IndexError: pass w.save() messages.success(request, 'Садка сохранена') return redirect(reverse('make:warren') + '?date=%s&s=%d' % (date.isoformat(), id(factory))) json = [{'number': w.tto, 'tts': [c.tts for c in w.consumer.all() if c.date == w.date], 'rows': c.add} for w in queryset if w.tto] return render(request, 'make/warren.html', dict(factory=factory, date=date, dateform=dateform, json=simplejson.dumps(json)))
def journal(request): dateform = DateForm(request.GET or None) if dateform.is_valid(): date = dateform.cleaned_data.get('date', datetime.date.today()) else: date = datetime.date.today() date = datetime.datetime.combine(date, datetime.time(8)) filter = dict(datetime__range=(date, date + datetime.timedelta(hours=23, minutes=59))) def prepare_factory(factory, queryset, initial=({},)): queryset = queryset.extra(select={'date': 'DATE(datetime)'}).order_by('date', 'order') order = max(f.order for f in queryset or (factory.model(),)) + 1 initial = [dict(init, datetime=date, order=i) for init, i in zip(cycle(initial), range(order, factory.extra + order + 1))] name = factory.__name__ return factory(request.POST or None, queryset=queryset, prefix=name, initial=initial) bar = prepare_factory(BarFactory, Bar.objects.filter(**filter)) raw = prepare_factory(RawFactory, Raw.objects.filter(**filter)) quarry = prepare_factory(QuarryFactory, Matherial.objects.filter(**filter).filter(position=8), initial=({'position': 8},)) clay = prepare_factory(ClayFactory, Matherial.objects.filter(**filter).filter(position__lte=7)) initial = [{'position': position, 'path': path} for path, position in zip([5, 5, 7, 7], [16, 25, 16, 25])] half = prepare_factory(HalfFactory, Half.objects.filter(**filter), initial=initial) factory = (half, raw, bar, clay, quarry) if request.method == 'POST' and all(f.is_valid() for f in factory): for f in factory: f.save() messages.success(request, u'Журнал сохранен') return redirect(reverse('lab:journal') + '?date=%s&s=1' % date.date().isoformat()) if not all(f.is_valid() for f in factory): for f in factory: print f.prefix, f.errors return render(request, 'lab/journal.html', {'factory': factory, 'date': date, 'dateform': dateform, 'add': True})