def edit_device(request, id=None): context = { 'page_title': u'Dispositivos', 'edit_name': 'device', 'has_back': False, 'features': get_user_features(request) } t = get_template('edit.html') device = None form = DeviceForm() try: if request.method == 'POST': form = DeviceForm(request.POST) if form.is_valid(): cd = form.cleaned_data device = _save_device(cd) messages.success(request, 'Dispositivo salvo com sucesso.') form = DeviceForm(initial={}) elif id: device = Device.objects.get(id=id) initial = device.__dict__ initial['category'] = device.category.id form = DeviceForm(initial=initial) except Exception as e: log.error(e) messages.error( request, u'Ocorreu um erro ao processar a requisição, por favor tente novamente.' ) context = _set_device_form_context(device, form, context) return render_to_response('edit.html', context, context_instance=RequestContext(request))
def edit_profile(request, id=None): context = { 'page_title': u'Perfis', 'edit_name': 'profile', 'has_back': False, 'features': get_user_features(request) } t = get_template('edit.html') profile = None form = ProfileForm() try: if request.method == 'POST': form = ProfileForm(request.POST) if form.is_valid(): cd = form.cleaned_data profile = _save_profile(cd) messages.success(request, 'Perfil salvo com sucesso.') form = ProfileForm(initial={}) elif id: profile = Profile.objects.get(id=id) initial = profile.__dict__ initial['features'] = profile.features.all() form = ProfileForm(initial=initial) except Exception as e: log.error(e) messages.error( request, u'Ocorreu um erro ao processar a requisição, por favor tente novamente.' ) context = _set_profile_form_context(profile, form, context) return render_to_response('edit.html', context, context_instance=RequestContext(request))
def edit_period(request, id=None): context = { 'page_title': u'Períodos', 'edit_name': 'period', 'has_back': True, 'back_page_name': u'trainee', 'features': get_user_features(request) } id_trainee = request.GET.get('parent_object_id', None) t = get_template('edit.html') trainee = None new_form_initial = {} period = StallTraineePeriod() form = StallTraineePeriodForm() # try: if id_trainee: trainee = StallTrainee.objects.get(id=id_trainee) period.stall_trainee = trainee form = StallTraineePeriodForm(initial={'stalltrainee': trainee.id}) if request.method == 'POST': form = StallTraineePeriodForm(request.POST) if form.is_valid(): cd = form.cleaned_data period, is_valid = _save_stall_trainee_period(cd) initial = period.__dict__ initial['stalltrainee'] = period.stall_trainee.id form = StallTraineePeriodForm(initial=initial) if is_valid: messages.success(request, 'Período salvo com sucesso.') else: messages.error( request, 'Erro ao salvar Período, já existe outro período nesta baia.' ) elif id: period = StallTraineePeriod.objects.get(id=id) initial = period.__dict__ initial['stalltrainee'] = period.stall_trainee.id form = StallTraineePeriodForm(initial=initial) context['parent_object_id'] = period.stall_trainee.id if period: if period.id: new_form_initial['periods'] = period.periods.all() new_form = forms.Form(initial=new_form_initial) new_form.fields['periods'] = form.fields.pop('periods') new_form.fields['stalltrainee'] = form.fields['stalltrainee'] context = _set_period_form_context(period, form, context) context['fields'] = new_form.as_ul() context['aux_fields'] = form.as_ul() # except Exception as e: # log.error(e) # messages.error(request, u'Ocorreu um erro ao processar a requisição, por favor tente novamente.') context['has_auxiliar_form'] = True return render_to_response('edit.html', context, context_instance=RequestContext(request))
def list_profile(request): profile_list = Profile.objects.all().order_by('id') values_dict = {} for profile in profile_list: profile.list_values = [profile.name, profile.description] context = { 'page_title': u'Perfis', 'header_name_list': profile_list_header, 'object_list': profile_list, 'edit_name': 'profile', 'can_remove': True, 'features': get_user_features(request) } return render_to_response('list.html', context, context_instance=RequestContext(request))
def list_role(request): t = get_template('list.html') role_list = Role.objects.all().order_by('name') values_dict = {} for role in role_list: role.list_values = [role.name] context = { 'page_title': u'Papéis', 'header_name_list': role_list_header, 'object_list': role_list, 'edit_name': 'role', 'can_remove': True, 'features': get_user_features(request) } return render_to_response('list.html', context, context_instance=RequestContext(request))
def list_people(request): people_list = Person.objects.all().order_by('name') values_dict = {} for person in people_list: person.list_values = [ person.name, person.level, person.role.name, person.institution.name ] context = { 'page_title': u'Pessoas', 'header_name_list': people_list_header, 'object_list': people_list, 'edit_name': 'people', 'can_remove': True, 'features': get_user_features(request) } return render_to_response('list.html', context, context_instance=RequestContext(request))
def list_device(request): t = get_template('list.html') device_list = Device.objects.all().order_by('category') values_dict = {} for device in device_list: device.list_values = [ device.description, device.patrimony_number, device.category.name ] context = { 'page_title': u'Dispositivos', 'header_name_list': device_list_header, 'object_list': device_list, 'edit_name': 'device', 'can_remove': True, 'features': get_user_features(request) } return render_to_response('list.html', context, context_instance=RequestContext(request))
def edit_people(request, id=None): context = { 'page_title': u'Pessoas', 'edit_name': 'people', 'has_back': False, 'features': get_user_features(request) } t = get_template('edit.html') person = None form = PersonForm() try: if request.method == 'POST': form = PersonForm(request.POST) if form.is_valid(): cd = form.cleaned_data person = _save_person(cd) messages.success(request, 'Pessoa salva com sucesso.') # initial = person.__dict__ # initial['institution'] = person.institution.id # initial['role'] = person.role.id form = PersonForm(initial={}) elif id: person = Person.objects.get(id=id) initial = person.__dict__ initial['institution'] = person.institution.id initial['role'] = person.role.id form = PersonForm(initial=initial) except Exception as e: log.error(e) messages.error( request, u'Ocorreu um erro ao processar a requisição, por favor tente novamente.' ) context = _set_person_form_context(person, form, context) return render_to_response('edit.html', context, context_instance=RequestContext(request))
def show_home(request): t = get_template('home.html') expiration_messages = [] try: today = datetime.now() expiration_date = today + timedelta(days=10) username = request.COOKIES.get("logged_user") user = User.objects.select_related().get(username=username) message_feature = Feature.objects.get(name="home_message") has_message = False if message_feature and user.profile.features.get( id=message_feature.id): has_message = True stall_list = Stall.objects.filter(leader=user.person) stall_trainees = StallTrainee.objects.select_related().filter( stall__in=stall_list, finish_period__gte=today, finish_period__lte=expiration_date) for trainee in stall_trainees: expiration_messages.append( u'O período de utilização da baia {0} na sala {1} pelo bolsista {2} irá expirar em {3}.' .format(trainee.stall.name, trainee.stall.room.number, trainee.trainee.name, trainee.finish_period.strftime('%d/%m/%Y'))) except: messages.error( request, u'Ocorreu um erro ao processar a requisição, por favor tente novamente.' ) html = t.render( Context({ 'welcome_message': u'Bem-vindo ao sistema de controle de dispositivos', 'message': expiration_messages, 'has_message': has_message, 'features': get_user_features(request) })) return HttpResponse(html)
def list_device_category(request): category_list = DeviceCategory.objects.all().order_by('name') values_dict = {} for category in category_list: category.list_values = [category.name] context = {'page_title': u'Categorias de Dispositivos', 'header_name_list': category_list_header, 'object_list': category_list, 'edit_name': 'categorydevice', 'can_remove': True, 'features':get_user_features(request)} return render_to_response('list.html', context, context_instance=RequestContext(request))
def edit_rooms(request, id=None): context = {'page_title': u'Salas', 'edit_name': 'room', 'list_title': u'Baias', 'list_edit_name': 'stall', 'header_name_list': stall_list_header, 'has_back': False, 'features':get_user_features(request)} t = get_template('edit.html') room = None form = RoomForm() try: if request.method == 'POST': form = RoomForm(request.POST) if form.is_valid(): cd = form.cleaned_data room = _save_room(cd) messages.success(request, 'Sala salva com sucesso.') initial=room.__dict__ initial['syndic'] = room.syndic form = RoomForm(initial=initial) elif id: room = Room.objects.get(id=id) initial=room.__dict__ initial['syndic'] = room.syndic form = RoomForm(initial=initial) except Exception as e: log.error(e) messages.error(request, u'Ocorreu um erro ao processar a requisição, por favor tente novamente.') context = _set_room_form_context(room, form, context, request) return render_to_response('edit.html', context, context_instance=RequestContext(request))
def period_report(request, id=None): obj_list = trainee_report_query.all() try: obj_list = filter_elements(obj_list, trainee_filter_list, id) #preparando dados para tela for obj in obj_list: weekdays = [] if obj.monday: weekdays.append(u"Segunda") if obj.tuesday: weekdays.append(u"Terça") if obj.wednesday: weekdays.append(u"Quarta") if obj.thursday: weekdays.append(u"Quinta") if obj.friday: weekdays.append(u"Sexta") day_periods = [p.name for p in obj.periods.all()] obj.list_values = [obj.stall_trainee.trainee.name, obj.stall_trainee.trainee.role.name, obj.stall_trainee.trainee.level, obj.stall_trainee.stall.room.number, obj.stall_trainee.stall.name, obj.stall_trainee.stall.leader.name, obj.stall_trainee.start_period.strftime('%d/%m/%Y'), obj.stall_trainee.finish_period.strftime('%d/%m/%Y'), u", ".join(weekdays), u", ".join(day_periods)] except: messages.error(request, u'Ocorreu um erro ao processar a requisição, por favor tente novamente.') context = {'page_title': u'Relatório por Períodos', 'header_name_list': trainee_report_list_header, 'object_list': obj_list, 'filters': trainee_screen_filter_list, 'acumulated_value': id and id or "", 'features':get_user_features(request), 'report_url': 'export_period'} return render_to_response('report.html', context, context_instance=RequestContext(request))
def edit_trainees(request, id=None): context = {'page_title': u'Bolsistas', 'edit_name': 'trainee', 'list_title': u'Períodos', 'list_edit_name': 'period', 'header_name_list': period_list_header, 'has_back': True, 'back_page_name': u'stall', 'features':get_user_features(request)} id_stall = request.GET.get('parent_object_id', None) stall = None trainee = StallTrainee() form = None try: if id_stall: stall = Stall.objects.get(id = id_stall) trainee.stall = stall form = TraineeForm(initial={'stall': stall.id}) t = get_template('edit.html') if request.method == 'POST': form = TraineeForm(request.POST) if form.is_valid(): cd = form.cleaned_data if not cd['id']: form_period = StallTraineePeriodForm(request.POST) if form_period and form_period.is_valid(): cd = dict(cd.items() + form_period.cleaned_data.items()) trainee, is_valid = _save_trainee(cd) if is_valid: messages.success(request, 'Bolsista salvo com sucesso.') else: messages.error(request, 'Erro ao salvar bolsista.') initial = _get_trainee_form_initial_value(trainee) form = TraineeForm(initial=initial) elif id: trainee = StallTrainee.objects.get(id=id) initial = _get_trainee_form_initial_value(trainee) form = TraineeForm(initial=initial) else: form_period = StallTraineePeriodForm() form.fields['periods'] = form_period.fields.pop('periods') context['aux_fields'] = form_period.as_ul() context['has_auxiliar_form'] = True context['fields'] = form.as_ul() form.fields['trainee'].queryset = Person.objects.exclude(role=Role.objects.get(name='Orientador')) context = _set_period_form_context(trainee, form, context, request) except Exception as e: log.error(e) messages.error(request, u'Ocorreu um erro ao processar a requisição, por favor tente novamente.') return render_to_response('edit.html', context, context_instance=RequestContext(request))
def edit_stalls(request, id=None): context = {'page_title': u'Baias', 'edit_name': 'stall', 'list_title': u'Bolsistas', 'list_edit_name': 'trainee', 'header_name_list': trainee_list_header, 'can_remove': True, 'has_back': True, 'back_page_name': u'room', 'features':get_user_features(request)} id_room = request.GET.get('parent_object_id', None) room = None stall = Stall() form = StallForm() try: if id_room: room = Room.objects.get(id = id_room) stall.room = room form = StallForm(initial={'room': room.id}) t = get_template('edit.html') if request.method == 'POST': form = StallForm(request.POST) if form.is_valid(): cd = form.cleaned_data stall = _save_stall(cd) messages.success(request, 'Baia salva com sucesso.') initial = _get_stall_form_initial_value(stall) form = StallForm(initial=initial) elif id: stall = Stall.objects.get(id=id) initial = _get_stall_form_initial_value(stall) form = StallForm(initial=initial) all_stall = Stall.objects.all() form.fields['device'].queryset = Device.objects.filter((Q(stall=None) | (~Q(stall__in = all_stall))) | Q(stall=stall)) except Exception as e: log.error(e) messages.error(request, u'Ocorreu um erro ao processar a requisição, por favor tente novamente.') context = _set_stall_form_context(stall, form, context, request) return render_to_response('edit.html', context, context_instance=RequestContext(request))
def list_rooms(request): t = get_template('list.html') username=request.COOKIES.get("logged_user"); user = User.objects.select_related().get(username=username) if user.profile.features.filter(name="room"): room_list = Room.objects.all().order_by('number') elif user.profile.features.filter(name="syndic_room"): room_list = user.person.room_set.all().order_by('number') values_dict = {} for room in room_list: room.list_values = [room.number, room.description, len(room.stall_set.all())] context = {'page_title': u'Salas', 'header_name_list': room_list_header, 'object_list': room_list, 'can_remove': True, 'edit_name': 'room', 'features':get_user_features(request)} return render_to_response('list.html', context, context_instance=RequestContext(request))
def list_institution(request): institution_list = Institution.objects.all().order_by('id') values_dict = {} for institution in institution_list: institution.list_values = [institution.name, institution.country, institution.observation] context = {'page_title': u'Instituições', 'header_name_list': institution_list_header, 'object_list': institution_list, 'edit_name': 'institution', 'can_remove': True, 'features':get_user_features(request)} return render_to_response('list.html', context, context_instance=RequestContext(request))
def occupacy_report(request, id=None): obj_list = occupacy_report_query.all() try: obj_list = filter_elements(obj_list, occupacy_filter_list, id) #preparando dados para tela used_data = [] for obj in obj_list: if not obj.room.number+obj.leader.name in used_data: obj.list_values = [obj.room.number, obj.leader.name, len(obj.room.stall_set.filter(leader = obj.leader)), len(obj.room.stall_set.all())] used_data.append(obj.room.number + obj.leader.name) except: messages.error(request, u'Ocorreu um erro ao processar a requisição, por favor tente novamente.') context = {'page_title': u'Relatório por ocupação', 'header_name_list': occupacy_report_list_header, 'object_list': obj_list, 'filters': screen_filter_list, 'acumulated_value': id and id or "", 'features':get_user_features(request), 'report_url': 'export_occupacy'} return render_to_response('report.html', context, context_instance=RequestContext(request))