def director_departamental_modificar(request,mod_id): cargos = Group.objects.filter(pk__in = [13,14]) if request.method == 'GET': ret_data = {} dept = Departamento.objects.all() ddf = DirectorDepartametalForm(instance = get_object_or_404(DirectorDepartamental, pk=mod_id)) ddf.fields['cargo'].queryset = cargos pf = PersonaForm(instance = get_object_or_404(Persona, pk=ddf.instance.persona.pk)) pf.fields["domicilio"].queryset = Municipio.objects.filter(departamento = ddf.instance.persona.domicilio.departamento.pk) ret_data['departamento'] = ddf.instance.persona.domicilio.departamento.pk return render_to_response('admin_director_departamental.html',{'dir_dep_form':ddf,'per_form':pf,'dept':dept,'ret_data':ret_data,'t_mod':True,'admin_dir_departamental':'Modificar Usuario Departamental'},context_instance=RequestContext(request)) elif request.method == 'POST': ret_data = {} query_persona = {} query = {} dept = Departamento.objects.all() ddf = DirectorDepartametalForm(request.POST, instance = get_object_or_404(DirectorDepartamental, pk=mod_id)) pf = PersonaForm(request.POST, instance = get_object_or_404(Persona, pk=ddf.instance.persona.pk)) #Almacenamos la instancia de Persona actual tmp = Persona.objects.filter(pk = ddf.instance.persona.pk) query['persona'] = tmp[0] query.update(tmp.values()[0]) query.pop('usuario_creador_id') query.pop('usuario_modifico_id') query.pop('fecha_creacion') query.pop('fecha_modificacion') query.pop('id') query.update({'usuario_modifico':request.user,'fecha_modificacion':datetime.datetime.now()}) if ddf.is_valid() and pf.is_valid(): try: #Debo Guardar como esta actualmente PersonaLog.objects.create(**query).save() ant_username = ddf.instance.persona.identidad pf_tmp = pf.save() ddf_tmp = ddf.save(commit=False) ddf_tmp.persona = pf_tmp ddf_tmp.save() try: user = User.objects.get(username__contains = ant_username, groups__name = cargos.get(pk = ddf.cleaned_data['cargo'].pk).name) except Exception,e: pass query_persona['primer_nombre'] = pf_tmp.primer_nombre query_persona['primer_apellido'] = pf_tmp.primer_apellido query_persona['segundo_nombre'] = pf_tmp.segundo_nombre query_persona['segundo_apellido'] = pf_tmp.segundo_apellido query_persona['identidad'] = pf_tmp.identidad query_persona['correo'] = pf_tmp.correo modificar_usuario(True,user,query_persona,'') except Exception, e: transaction.rollback() ddf.fields['cargo'].queryset = cargos return render_to_response('admin_director_departamental.html',{'dir_dep_form':ddf,'per_form':pf,'dept':dept,'ret_data':ret_data,'t_mod':True,'admin_dir_departamental':'Modificar Usuario Departamental'},context_instance=RequestContext(request)) else: transaction.commit() list_departamentales = DirectorDepartamental.objects.all() return render_to_response('listar_departamentales.html',{'lst_dep':list_departamentales},context_instance=RequestContext(request))
def director_distrital_modificar(request, mod_id): cargos = Group.objects.all() if request.method == 'GET': ret_data = {} dept = Departamento.objects.all() ddf = DirectorDistritalForm(instance = get_object_or_404(DirectorDistrital, pk = mod_id)) ddf.fields['cargo'].queryset = cargos.filter(pk__in = [8,19]) if cargos.get(pk=14) in request.user.groups.all() or cargos.get(pk=13) in request.user.groups.all(): per = DirectorDepartamental.objects.get(persona__identidad = request.user.username[:13]) ddf.fields['direccion_distrital'].queryset = DireccionDistrital.objects.filter(municipio__departamento = per.direccion_departamental.departamento) per_form = PersonaForm(instance = get_object_or_404(Persona, pk=ddf.instance.persona.pk)) per_form.fields["domicilio"].queryset = Municipio.objects.filter(departamento = ddf.instance.persona.domicilio.departamento.pk) ret_data['departamento'] = ddf.instance.persona.domicilio.departamento.pk return render_to_response('admin_director_distrital.html',{'ddf':ddf,'per_form':per_form,'ret_data':ret_data,'dept':dept,'admin_dir_distrital':'Modificar Director Distrital','mod':True},context_instance = RequestContext(request)) elif request.method == 'POST': ret_data = {} query_persona = {} query = {} dept = Departamento.objects.all() ddf = DirectorDistritalForm(request.POST, instance = get_object_or_404(DirectorDistrital, pk = mod_id)) if cargos.get(pk=14) in request.user.groups.all() or cargos.get(pk=13) in request.user.groups.all(): per = DirectorDepartamental.objects.get(persona__identidad = request.user.username[:13]) ddf.fields['direccion_distrital'].queryset = DireccionDistrital.objects.filter(municipio__departamento = per.direccion_departamental.departamento) departamento = DirectorDepartamental.objects.get(persona__identidad = request.user.username[:13]).direccion_departamental.departamento per_form = PersonaForm(request.POST, instance = get_object_or_404(Persona, pk = ddf.instance.persona.pk)) tmp = Persona.objects.filter(pk = ddf.instance.persona.pk) query['persona'] = tmp[0] query.update(tmp.values()[0]) del(query['id']) del(query['fecha_creacion']) del(query['usuario_creador_id']) if ddf.is_valid() and per_form.is_valid(): try: #Debo Guardar como esta actualmente PersonaLog.objects.create(**query).save() ant_username = ddf.instance.persona.identidad per_form_tmp = per_form.save() ddf_tmp = ddf.save(commit=False) ddf_tmp.persona = per_form_tmp ddf_tmp.save() try: user = User.objects.filter(username__contains = ant_username) except Exception,e: pass query_persona['primer_nombre'] = per_form_tmp.primer_nombre query_persona['primer_apellido'] = per_form_tmp.primer_apellido query_persona['segundo_nombre'] = per_form_tmp.segundo_nombre query_persona['segundo_apellido'] = per_form_tmp.segundo_apellido query_persona['identidad'] = per_form_tmp.identidad query_persona['correo'] = per_form_tmp.correo modificar_usuario(True,user[0],query_persona,user[0].username) except Exception, e: transaction.rollback() return render_to_response('admin_director_distrital.html',{'ddf':ddf,'per_form':per_form,'ret_data':ret_data,'dept':dept,'admin_dir_distrital':'Modificar Director Distrital','mod':True},context_instance = RequestContext(request)) else: transaction.commit() if departamento: list_distritales = DirectorDistrital.objects.filter(direccion_distrital__municipio__departamento = departamento) else: list_distritales = DirectorDistrital.objects.all() return render_to_response('listar_directores_distritales.html',{'exito':True,'lst_dist':list_distritales},context_instance=RequestContext(request))
def director_distrital_crear(request): ret_data, errores = {}, {} cargos = Group.objects.all() tipoID_q = TipoDocumentoID.objects.filter(pk__in=[1,3]) dept_q = Departamento.objects.all() if request.method == 'GET': per_form = PersonaForm() ddf = DirectorDistritalForm() ddf.fields['cargo'].queryset = cargos.filter(pk__in = [8,19]) if cargos.get(pk=14) in request.user.groups.all() or cargos.get(pk=13) in request.user.groups.all(): per = DirectorDepartamental.objects.get(persona__identidad = request.user.username[:13]) ddf.fields['direccion_distrital'].queryset = DireccionDistrital.objects.filter(municipio__departamento = per.direccion_departamental.departamento) per_form.fields["domicilio"].queryset = Municipio.objects.none() pin = ''.join(random.choice(string.digits) for x in range(4)) ret_data['password'] = ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(8)) return render_to_response('admin_director_distrital.html',{'es_alumno':'0','pin':pin,'ret_data':ret_data,'per_form':per_form,'ddf':ddf, 'admin_dir_distrital':'Registrar Director Distrital', 'tipoid':per_form.fields['tipoID'].queryset,'dept':dept_q}, context_instance=RequestContext(request)) elif request.method == 'POST': per_form = PersonaForm(request.POST) ddf = DirectorDistritalForm(request.POST) ret_data['password'] = request.POST.get('password','') if request.POST.get('existe_persona','') == '0': ret_data['identidad'] = per_form['identidad'].value() if per_form.is_valid() and ddf.is_valid(): n_completo = per_form['primer_nombre'].value() + ' ' + per_form['segundo_nombre'].value() a_completo = per_form['primer_apellido'].value() + ' ' + per_form['segundo_apellido'].value() try: pf = per_form.save() ddf_tmp = ddf.save(commit=False) ddf_tmp.persona = pf ddf_tmp.save() ret_data['correo'] = per_form['correo'].value() ret_data['clave1'] = request.POST.get('password','') crear_usuario(n_completo,a_completo,ret_data,cargos.get(pk = request.POST.get('cargo','')),request.POST.get('pin','')) except Exception, e: transaction.rollback() ret_data['departamento'] = int(request.POST.get('departamento','')) per_form.fields["domicilio"].queryset = Municipio.objects.filter(departamento = ret_data['departamento']) ddf.fields['cargo'].queryset = cargos.filter(pk__in = [8,19]) if cargos.get(pk=14) in request.user.groups.all() or cargos.get(pk=13) in request.user.groups.all(): per = DirectorDepartamental.objects.get(persona__identidad = request.user.username[:13]) ddf.fields['direccion_distrital'].queryset = DireccionDistrital.objects.filter(municipio__departamento = per.direccion_departamental.departamento) return render_to_response('admin_director_distrital.html',{'pin':request.POST.get('pin',''), 'tipoid':per_form.fields['tipoID'].queryset,'per_form':per_form,'ddf':ddf,'ret_data':ret_data,'dept':dept_q, 'admin_dir_distrital':'Registrar Director Distrital'},context_instance=RequestContext(request)) else: transaction.commit() per_form = PersonaForm() ddf = DirectorDistritalForm() ddf.fields['cargo'].queryset = cargos.filter(pk__in = [8,19]) if cargos.get(pk=14) in request.user.groups.all() or cargos.get(pk=13) in request.user.groups.all(): per = DirectorDepartamental.objects.get(persona__identidad = request.user.username[:13]) ddf.fields['direccion_distrital'].queryset = DireccionDistrital.objects.filter(municipio__departamento = per.direccion_departamental.departamento) per_form.fields["domicilio"].queryset = Municipio.objects.none() pin = ''.join(random.choice(string.digits) for x in range(4)) del(ret_data) ret_data = {} ret_data['password'] = ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(8)) return render_to_response('admin_director_distrital.html',{'exito':True,'pin':pin,'ret_data':ret_data,'per_form':per_form,'ddf':ddf, 'tipoid':per_form.fields['tipoID'].queryset,'dept':dept_q, 'admin_dir_distrital':'Registrar Director Distrital'}, context_instance=RequestContext(request)) else: ret_data['departamento'] = int(request.POST.get('departamento','')) per_form.fields["domicilio"].queryset = Municipio.objects.filter(departamento = ret_data['departamento']) ddf.fields['cargo'].queryset = cargos.filter(pk__in = [8,19]) if cargos.get(pk=14) in request.user.groups.all() or cargos.get(pk=13) in request.user.groups.all(): per = DirectorDepartamental.objects.get(persona__identidad = request.user.username[:13]) ddf.fields['direccion_distrital'].queryset = DireccionDistrital.objects.filter(municipio__departamento = per.direccion_departamental.departamento) return render_to_response('admin_director_distrital.html',{'pin':request.POST.get('pin',''),'per_form':per_form,'ddf':ddf,'ret_data':ret_data, 'tipoid':per_form.fields['tipoID'].queryset,'dept':dept_q,'admin_dir_distrital':'Registrar Director Distrital'}, context_instance=RequestContext(request)) else: per_form = PersonaForm() recibo = validar_persona(request,'del administrador',tipoID_q,per_form.fields['tipoID'].queryset,dept_q,True) ret_data.update(recibo['ret_data']) errores.update(recibo['errores']) if not errores: # Ya existe la persona es por eso que los datos los recuperamos del ya existente nombre_completo = ret_data['persona'].primer_nombre if ret_data['persona'].segundo_nombre: nombre_completo += ' ' + ret_data['persona'].segundo_nombre apellido_completo = ret_data['persona'].primer_apellido if ret_data['persona'].segundo_apellido: apellido_completo += ' ' + ret_data['persona'].segundo_apellido try: if ddf.is_valid(): ddf_tmp = ddf.save(commit=False) ddf_tmp.persona = ret_data['persona'] ddf_tmp.save() else: ddf.fields['cargo'].queryset = cargos.filter(pk__in = [8,19]) if cargos.get(pk=14) in request.user.groups.all() or cargos.get(pk=13) in request.user.groups.all(): per = DirectorDepartamental.objects.get(persona__identidad = request.user.username[:13]) ddf.fields['direccion_distrital'].queryset = DireccionDistrital.objects.filter(municipio__departamento = per.direccion_departamental.departamento) return render_to_response('admin_director_distrital.html',{'pin':request.POST.get('pin',''),'ddf':ddf,'per_form':per_form, 'ret_data':ret_data,'dept':dept_q,'admin_dir_distrital':'Registrar Director Distrital', 'r_existente':'1','r_tipo_id':ret_data['persona'].tipoID.pk, 'r_identidad':ret_data['persona'].identidad,'tipoid':per_form.fields['tipoID'].queryset}, context_instance=RequestContext(request)) crear_usuario(nombre_completo,apellido_completo,ret_data,cargos.get(pk = request.POST.get('cargo','')),request.POST.get('pin','')) except Exception, e: transaction.rollback() ddf.fields['cargo'].queryset = cargos.filter(pk__in = [8,19]) if cargos.get(pk=14) in request.user.groups.all() or cargos.get(pk=13) in request.user.groups.all(): per = DirectorDepartamental.objects.get(persona__identidad = request.user.username[:13]) ddf.fields['direccion_distrital'].queryset = DireccionDistrital.objects.filter(municipio__departamento = per.direccion_departamental.departamento) return render_to_response('admin_director_distrital.html',{'pin':request.POST.get('pin',''),'ddf':ddf,'per_form':per_form,'ret_data':ret_data, 'dept':dept_q,'admin_dir_distrital':'Registrar Director Distrital', 'r_existente':'1','r_tipo_id':ret_data['persona'].tipoID.pk, 'r_identidad':ret_data['persona'].identidad,'tipoid':per_form.fields['tipoID'].queryset}, context_instance=RequestContext(request)) else: transaction.commit() per_form = PersonaForm() ddf = DirectorDistritalForm() ddf.fields['cargo'].queryset = cargos.filter(pk__in = [8,19]) if cargos.get(pk=14) in request.user.groups.all() or cargos.get(pk=13) in request.user.groups.all(): per = DirectorDepartamental.objects.get(persona__identidad = request.user.username[:13]) ddf.fields['direccion_distrital'].queryset = DireccionDistrital.objects.filter(municipio__departamento = per.direccion_departamental.departamento) per_form.fields["domicilio"].queryset = Municipio.objects.none() pin = ''.join(random.choice(string.digits) for x in range(4)) del(ret_data) ret_data = {} ret_data['password'] = ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(8)) return render_to_response('admin_director_distrital.html',{'es_alumno':'0','exito':True,'pin':pin,'ret_data':ret_data,'per_form':per_form, 'ddf':ddf,'dept':dept_q,'admin_dir_distrital':'Registrar Director Distrital', 'tipoid':per_form.fields['tipoID'].queryset}, context_instance=RequestContext(request)) else: