def get(self, request): """ Return the step's works for a grid """ if request.is_ajax(): # == 'POST': try: step_id = int(request.GET.get('step')) w = Work.objects.filter(current_step=step_id, status='A').order_by( 'status', 'datetime_change') if w.count() > 0: rows = [] for item in w: datetime_limit = item.datetime_change + datetime.timedelta( minutes=item.current_step.time_limit) d = { 'id': item.pk, 'name': unicode(item.name), 'desc': item.desc, 'owner': unicode(item.owner), 'datetime_add': item.datetime_add.strftime('%d/%m/%Y %H:%M'), 'datetime_change': item.datetime_change.strftime('%d/%m/%Y %H:%M'), 'datetime_limit': datetime_limit.strftime('%d/%m/%Y %H:%M'), 'reject': [u'Não', u'Sim'][item.reject], 'reason_reject': item.reason_reject, 'status': [u'Fechado', u'Aberto'][item.status == 'A'], 'can_upload': item.current_step.can_upload, } rows.append(d) r = {'total': len(w.values()), 'rows': rows} else: r = { 'total': 0, 'rows': [{ 'id': 0, 'name': 'Nenhum resultado' }] } except: r = {'total': 0, 'rows': [{'id': 0, 'name': 'nada'}]} json = simplejson.dumps(r) return R(json, mimetype='application/json') else: return R('not ajax')
def products(self, request): """ Return the product list for combobox """ if request.is_ajax(): # == 'POST': try: p = Product.objects.all() if p.count() > 0: rows = [] for item in p: try: product_with_price = item.productprice_set.select_related( ).filter(price__gt=0) if product_with_price.count() > 0: d = {'id': item.pk, 'name': item.name} rows.append(d) except: pass r = {'total': len(rows), 'rows': rows} else: r = { 'total': 0, 'rows': [{ 'id': 0, 'name': 'Nenhum resultado' }] } except Exception, e: r = {'total': 0, 'rows': [{'id': 0, 'name': e.message}]} json = simplejson.dumps(r) return R(json, mimetype='application/json')
def information(self, request): """ Return a page about work information """ try: work_id = int(request.GET.get('work')) p = ProcessFieldValue.objects.filter(work=work_id) p_list = [] if p.count() > 0: for item in p: p_list.append({ 'label': item.dynamicfield.label, 'value': item.value, }) w = WorkFieldValue.objects.filter( work=work_id).order_by('dynamicfield__step__sequence') w_list = [] if w.count() > 0: for item in w: w_list.append({ 'label': item.dynamicfield.label, 'value': item.value, 'step': u'%s' % item.dynamicfield.step, }) return direct_to_template(request, 'information.html', { 'information': w_list, 'initial_fields': p_list }) #else: # return R('<p style="color:red">Nenhuma informação disponível para esse trabalho.</p>') except Exception, e: return R(e.message)
def client(self, request): """ Return data about client """ try: w = Work.objects.get(id=request.GET.get('work')) try: client_id = w.client.id return HttpResponseRedirect( '%sclient/client/objects/%s' % (settings.DATABROWSE_URL, client_id)) except: return R('Não há cliente relacionado à esse trabalho.') except Exception, e: return R(e.message)
def download(self, request, filename): from django.core.servers.basehttp import FileWrapper a = get_object_or_404(Attachment, filename=filename) f = a.filepath.path #mime = 'application/force-download' mime = 'application/octet-stream' #import mimetypes #mime = mimetypes.guess_type(filename)[0] try: wrapper = FileWrapper(file(f)) response = R(wrapper, mimetype=mime) response[ 'Content-Disposition'] = 'inline; filename="%s"' % filename response['Content-Length'] = os.path.getsize(f) return response except: return R('Erro: Arquivo não encontrado no servidor.')
def new(request): """ Cria o form para cadastro. Retorna os campos no formato json e convertidos para campos ExtJS. """ try: f = LancamentoForm() json = f.as_ext() except: json = DEFAULT_EXTJS_RESP.copy() json = u'%s' % json.update({'msg': u'Formulário apresenta algum erro e não pode ser exibido'}) return R(json,mimetype='application/json')
def history(self, request): """ Get work history """ if request.is_ajax(): try: work_id = request.GET.get('work') wh = WorkHistory.objects.filter( work=work_id).order_by('-datetime_add') if wh.count() > 0: rows = [] for item in wh: d = { #'id': item.pk, 'datetime_add': item.datetime_add.strftime('%d/%m/%Y %H:%M'), 'user': unicode(item.user), 'desc': item.desc, } rows.append(d) r = {'total': len(wh.values()), 'rows': rows} else: r = { 'total': 0, 'rows': [{ 'id': 0, 'datetime_add': '', 'user': '', 'desc': 'Nenhum resultado' }] } except Exception, e: r = { 'total': 0, 'rows': [{ 'id': 0, 'datetime_add': '', 'user': '', 'desc': e.message }] } json = simplejson.dumps(r) return R(json, mimetype='application/json')
class ItemView(object): @login_required def add(self, request): if request.method == 'POST': try: w = Work.objects.get(id=request.POST.get('work')) if not w.owner == request.user: result = { 'success': True, 'status': 'error', 'msg': 'Permissão negada.<br>Apenas o dono do trabalho pode alterar a tabela.' } else: items = simplejson.loads(request.POST.get('items')) if len(items) > 0: for items_dict in items: try: i = Item.objects.get(id=items_dict['id']) except: i = Item() i.work = w i.user = request.user i.product = Product.objects.get( pk=items_dict['product']) i.productprice = ProductPrice.objects.get( productservice=items_dict['productservice']) i.amount = items_dict['amount'] i.price = str(items_dict['price']) try: i.save() result = { 'success': True, 'status': 'ok', 'msg': 'Tabela salva com sucesso.' } except Exception, e: result = { 'success': True, 'status': 'error', 'msg': e.message } except Exception, e: result = {'success': True, 'status': 'error', 'msg': e.message} json = simplejson.dumps(result) return R(json, mimetype='text/html')
def get_step_tasks(request): """ Return the tasks of step """ if request.method == 'POST': try: step_id = int(request.POST.get('step')) s = Step.objects.get(pk=step_id) tasks = {'tasks': s.tasks} except: tasks = {'tasks': ''} json = simplejson.dumps(tasks) return R(json, mimetype='application/json')
def get(self, request): """ Return the files's works for a grid """ if request.is_ajax(): # == 'POST': try: work_id = int(request.GET.get('work')) w = Attachment.objects.filter( work=work_id).order_by('filename') if w.count() > 0: rows = [] for item in w: if item.user == request.user: delete_class = '<center><div class="delete-abled-icon" onclick="deleteFile(%s)"></div></center>' % item.id else: delete_class = '<center><div class="delete-disabled-icon"></div></center>' d = { # 'id': item.pk, 'filename': '<a href="%s">%s</a>' % (reverse('workflow_file_download', args=[item.filename]), item.filename), 'datetime_change': item.datetime_change.strftime('%d/%m/%Y'), 'delete': delete_class } rows.append(d) r = {'total': len(w.values()), 'rows': rows} else: r = { 'total': 0, 'rows': [{ 'id': 0, 'name': 'Nenhum resultado' }] } except: r = {'total': 0, 'rows': [{'id': 0, 'name': 'nada'}]} json = simplejson.dumps(r) return R(json, mimetype='application/json')
def files(self, request): """ Show work files """ if request.method == 'GET': work_id = request.GET.get('work') a = Attachment.objects.filter( work=work_id, user=request.user).order_by('-datetime_change') return direct_to_template(request, 'files.html', extra_context={ 'work_id': work_id, 'files': a }) else: return R('Nenhum trabalho selecionado.')
def get(self, request): """ Return the items """ if request.is_ajax(): # == 'POST': try: work_id = int(request.GET.get('work')) i = Item.objects.filter(work=work_id).order_by('product') if i.count() > 0: rows = [] for item in i: #if item.user == request.user: # delete_class = '<center><div class="delete-abled-icon" onclick="deleteFile(%s)"></div></center>' % item.id #else: # delete_class = '<center><div class="delete-disabled-icon"></div></center>' d = { 'id': item.pk, 'product': item.product.id, 'productservice': item.productprice.productservice.id, 'amount': item.amount, 'price': str(item.price), 'grouping': 'Itens' } rows.append(d) r = {'total': len(i.values()), 'rows': rows} else: r = { 'total': 0, 'rows': [{ 'id': 0, 'name': 'Nenhum resultado', 'grouping': 'Itens' }] } except Exception, e: r = {'total': 0, 'rows': [{'id': 0, 'name': e.message}]} json = simplejson.dumps(r) return R(json, mimetype='application/json')
def remove(self, request): if request.is_ajax(): item_id = request.POST.get('id') try: i = Item.objects.get(id=item_id, user=request.user).delete() result = { 'success': True, 'status': 'ok', 'msg': 'Item removido' } except: result = { 'success': True, 'status': 'error', 'msg': 'Permissão negada' } else: result = {'success': False, 'status': 'error', 'msg': 'Not ajax'} json = simplejson.dumps(result) return R(json, mimetype='text/html')
def report(self, request): if request.method == 'GET': work_id = request.GET.get('work') try: w = Work.objects.get(id=work_id) fields = w.processfieldvalue_set.all().order_by('dynamicfield') field_list = {} for item in fields: field_list.update({ #'field': item.dynamicfield.label, #'value': item.value item.dynamicfield.name.replace('-', ''): [item.value, '-'][item.value == '' or item.value == None] }) item_list = [] total = 0 for item in w.item_set.all(): item_list.append({ 'product': item.product.name, 'product_desc': item.product.desc, 'amount': item.amount, 'price': item.price, 'total': item.total }) total += item.total context = { 'work': w, 'fields': field_list, 'items': item_list, 'total': total, 'pagesize': 'A4' } return write_pdf('report_pedido.html', context, 'pedido%s' % work_id) #return direct_to_template(request, 'report_pedido.html', extra_context=context) except Exception, e: return R('Nenhum trabalho selecionado<br>%s' % e.message)
def menu(request): """ Return the values for a treemenu """ # Admin tem acesso a todos os steps de todos os processos #if request.user.is_superuser: # s = Step.objects.all().order_by('process','sequence') #else: #s = Step.objects.filter(managers=request.user).order_by('process','sequence') s = Step.objects.all().order_by('process', 'sequence') process_added = [] menu = [] temp = {} #import ipdb #ipdb.set_trace() for step in s: # Verifica se o usuario pode iniciar um trabalho #try: # step.process.users_can_start.get(username=request.user) can_start_process = True #except: # can_start_process = False #try: # #step.process.users_can_participate.get(username=request.user) # step.participants.get(username=request.user) can_participate_step = True #except: # can_participate_step = False # Obtem o toal de trabalhos abertos e exibe ao lado do passo total_open = Work.objects.filter(current_step=step, status='A').count() if total_open > 0: total_open_str = '<span style="color:red;">(%s)</span>' % total_open else: total_open_str = '' step_title_menu = '<span style="color:black">%s.</span> %s %s' % ( step.sequence, step.name, total_open_str) # Se o usuario logado for o responsavel pelo passo a cor do titulo é azul try: step.managers.get(username=request.user) step_title_menu = '<span style="color:blue">%s</span>' % step_title_menu can_manager_step = True except: # Se nao for o responsavel mas participar do processo, a cor do titulo é cinza can_manager_step = False if can_participate_step: step_title_menu = '<span style="color:gray">%s</span>' % step_title_menu if can_manager_step or can_participate_step: # Se o processo nao estiver na lista... if not step.process.name in process_added: # Adiciona o processo a lista process_added.append(step.process.name) # Cria a arvore cujo nome eh o mesmo do processo e insere o primeiro passo children = [] if can_start_process == True: # Botao Novo children.append({ 'pk': 0, 'process_id': step.process.id, 'process_name': step.process.name, 'text': 'Novo', 'leaf': True, 'iconCls': 'add-icon' }) children.append({ 'pk': step.id, 'process_id': step.process.id, 'process_name': step.process.name, 'step_name': step.name, 'text': step_title_menu, 'leaf': True, 'manager': can_manager_step, 'participant': can_participate_step }) temp = { step.process.name: { 'text': step.process.name, 'id': step.process.id, 'cls': 'file', 'expanded': True, 'children': children, } } # Adiciona a lista o menu menu.append(temp) else: # Se o processo ja esta na lista... # Varre a lista menu for cont in range(len(menu)): # Verifica se o nome do processo eh a chave do menu if menu[cont].has_key(step.process.name): # Se for, adiciona um nó ao menu menu[cont][step.process.name]['children'].append({ 'pk': step.id, 'process_id': step.process.id, 'process_name': step.process.name, 'step_name': step.name, 'text': step_title_menu, 'leaf': True, 'manager': can_manager_step, 'participant': can_participate_step, }) m = [] for item in menu: m.append(item.values()[0]) json = simplejson.dumps(m) return R(json, mimetype='application/json')
""" try: process_id = request.GET.get('process') form = self.__get_dynamicprocessform(process_id) if form: f1 = WorkForm() f2 = form() json = '%s%s' % (f1.as_ext(), f2.as_ext()) json = json.replace('][', ',') else: json = '{"success": true, "status": "error", "msg": "Not form"}' except Exception, e: json = '{"success": true, "status": "error", "msg": "%s"}' % e.message return R(json, mimetype='application/json') @login_required def add(self, request): """ Adiciona um novo trabalho """ msg = '' status = 'error' if request.is_ajax(): P = request.POST try: process_id = int(request.GET.get('process')) except Exception, e: msg = e.message
class WorkView(object): def __insert_work_history(self, w, u, d): """ Create a history about work """ try: h = WorkHistory.objects.create(work=w, user=u, desc=d) h.save() return True except: return False def __get_dynamicworkform(self, step_id, work_id=None): d = DynamicFieldStep.objects.filter(step=step_id).order_by('sequence') if d.count() == 0: return False dynfields = {} for f in d: max_length = [250, f.max_length][f.max_length > 0] required = [False, True][f.required] # Try get value, if the field already exist. For exampĺe, a reject work try: initial = u'' % WorkFieldValue.objects.get(dynamicfield=f, work=work_id).value except: initial = None if f.fieldtype == 'CharField': formfield = fields.CharField(max_length=max_length, required=required, label=f.label, initial=initial) if f.fieldtype == 'TextField': formfield = TextField(required=required, label=f.label, initial=initial) if f.fieldtype == 'EmailField': formfield = fields.EmailField(max_length=max_length, required=required, label=f.label, initial=initial) if f.fieldtype == 'DateField': formfield = fields.DateField( ('%d/%m/%Y', ), widget=DateTimeInput(format='%d/%m/%Y', attrs={ 'class': 'brdatefield', 'maxlength': '10' }), required=required, label=f.label, initial=initial) if f.fieldtype == 'IntegerField': formfield = fields.IntegerField(required=required, label=f.label, min_value=0, initial=initial) if f.fieldtype == 'DecimalField': formfield = fields.DecimalField(max_digits=10, decimal_places=2, required=required, label=f.label, initial=initial) if f.fieldtype == 'BooleanField': formfield = fields.BooleanField(required=False, label=f.label, initial='true') if f.fieldtype == 'FileField': formfield = fields.FileField(required=required, label=f.label, initial=initial) dynfields[f.name] = formfield process = f.step.process sequence = f.step.sequence next_steps = Step.objects.filter( process=process, sequence__gt=sequence).order_by('sequence') if next_steps.count() > 0: for n in next_steps: if n.sequence == sequence + 1: selected = n.pk if not selected: selected = n.pk dynfields['step'] = ModelChoiceField(queryset=next_steps, label=u'Próximo Passo', empty_label=None, initial=selected) form = type('', (forms.Form, ), dynfields) def as_ext(self): return mark_safe(simplejson.dumps(self, cls=ExtJSONEncoder)) form.as_ext = as_ext return form def __get_dynamicprocessform(self, process_id): d = DynamicFieldProcess.objects.filter( process=process_id).order_by('sequence') if d.count() == 0: return False dynfields = {} for f in d: max_length = [250, f.max_length][f.max_length > 0] required = [False, True][f.required] initial = None if f.fieldtype == 'CharField': formfield = fields.CharField(max_length=max_length, required=required, label=f.label, initial=initial) if f.fieldtype == 'TextField': formfield = TextField(required=required, label=f.label, initial=initial) if f.fieldtype == 'EmailField': formfield = fields.EmailField(max_length=max_length, required=required, label=f.label, initial=initial) if f.fieldtype == 'DateField': formfield = fields.DateField( ('%d/%m/%Y', ), widget=DateTimeInput(format='%d/%m/%Y', attrs={ 'class': 'brdatefield', 'maxlength': '10' }), required=required, label=f.label, initial=initial) if f.fieldtype == 'IntegerField': formfield = fields.IntegerField(required=required, label=f.label, min_value=0, initial=initial) if f.fieldtype == 'DecimalField': formfield = fields.DecimalField(max_digits=10, decimal_places=2, required=required, label=f.label, initial=initial) if f.fieldtype == 'BooleanField': formfield = fields.BooleanField(required=False, label=f.label, initial='true') if f.fieldtype == 'FileField': formfield = fields.FileField(required=required, label=f.label, initial=initial) dynfields[f.name] = formfield form = type('', (forms.Form, ), dynfields) def as_ext(self): return mark_safe(simplejson.dumps(self, cls=ExtJSONEncoder)) form.as_ext = as_ext return form @login_required def form_accept(self, request): """ Make the form for accept work """ try: work_id = int(request.GET.get('work')) w = Work.objects.get(pk=work_id, status='A') form = self.__get_dynamicworkform(w.current_step.pk, w.pk) if form: f = form() json = f.as_ext() else: json = '{"success": true, "status": "error", "msg": "Not form"}' except Exception, e: json = '{"success": true, "status": "error", "msg": "%s"}' % e.message return R(json, mimetype='application/json')