def locality_add(request): d = Locality() d.msnm = 0 d.date_create ="" if request.method == "POST": try: # Aquí asignar los datos d.name = request.POST.get("name") d.msnm = request.POST.get("msnm") d.date_create = None if request.POST.get("date_create"): d.date_create = request.POST.get("date_create") if request.POST.get("locality_type_id"): d.locality_type = LocalityType.objects.get(id=request.POST.get("locality_type_id")) if normalize("NFKD", u"%s" % d.name).encode("ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode("ascii", "ignore").lower() for col in Locality.objects.values("name").exclude(id=d.id) # puede .filter() ): raise Exception(_("Locality <b>%(name)s</b> name's already in use.") % {"name":d.name}) # El nombre x para localidad ya existe. # salvar registro d.save() if d.id: Message.info(request, ("Localidad <b>%(name)s</b> ha sido registrado correctamente.") % {"name":d.name}, True) # return Redirect.to(request, "/params/locality/index/")#usar to(...) cuando la acción está implementada en otra app, ver sad.views.user_index return Redirect.to_action(request, "index") # usar to_action(...) cuando la acción está implementada en este archivo except Exception, e: Message.error(request, e)
def producto_add(request): """ Agrega Producto """ d = Producto() d.descripcion = "" d.fecha_venc = "" if request.method == "POST": try: sid = transaction.savepoint() d.codigo = request.POST.get("codigox") d.descripcion = request.POST.get("descripcion") d.precio_venta = request.POST.get("precio_venta") d.headquar_id = DataAccessToken.get_headquar_id(request.session) if request.POST.get("fecha_venc"): d.fecha_venc = request.POST.get("fecha_venc") if request.POST.get("categoria_nombre"): d.categoria, is_created = Categoria.objects.get_or_create( nombre=request.POST.get("categoria_nombre"), ) if Producto.objects.filter(codigo=d.codigo).exclude( id=d.id, headquar_id=d.headquar_id).count() > 0: raise Exception("El producto <b>%s</b> ya existe " % d.codigo) d.save() if d.id: Message.info(request, ( "Producto <b>%(name)s</b> ha sido registrado correctamente." ) % {"name": d.codigo}, True) return Redirect.to_action(request, "index") except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e)
def enterprise_delete(request, key): """ Elimina empresa con todas sus sedes """ id = SecurityKey.is_valid_key(request, key, "enterprise_del") if not id: return Redirect.to_action(request, "index") try: d = get_object_or_404(Enterprise, id=id) except: Message.error(request, ("Empresa no se encuentra en la base de datos.")) return Redirect.to_action(request, "index") try: sid = transaction.savepoint() association = Association.objects.get(id=DataAccessToken.get_association_id(request.session)) if Enterprise.objects.filter(headquar__association_id=DataAccessToken.get_association_id(request.session)).count() == 1: raise Exception(("Asociación <b>%(name)s</b> no puede quedar sin ninguna sede asociada.") % {"name":association.name}) if d.userprofileenterprise_set.count() > 0: raise Exception(("Empresa <b>%(name)s</b> tiene usuarios y grupos asignados.") % {"name":d.name}) # agregue aquí sus otras relgas de negocio d.delete() if not d.id: Message.info(request, ("Empresa <b>%(name)s</b> ha sido eliminado correctamente.") % {"name":d.name}, True) return Redirect.to_action(request, "index") except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e) return Redirect.to_action(request, "index")
def producto_add(request): """ Agrega Producto """ d = Producto() d.descripcion = "" d.fecha_venc="" if request.method == "POST": try: sid = transaction.savepoint() d.codigo = request.POST.get("codigox") d.descripcion = request.POST.get("descripcion") d.precio_venta = request.POST.get("precio_venta") d.headquar_id = DataAccessToken.get_headquar_id(request.session) if request.POST.get("fecha_venc"): d.fecha_venc = request.POST.get("fecha_venc") if request.POST.get("categoria_nombre"): d.categoria, is_created = Categoria.objects.get_or_create( nombre=request.POST.get("categoria_nombre"), ) if Producto.objects.filter(codigo=d.codigo).exclude(id=d.id, headquar_id=d.headquar_id).count() > 0: raise Exception("El producto <b>%s</b> ya existe " % d.codigo) d.save() if d.id: Message.info(request, ("Producto <b>%(name)s</b> ha sido registrado correctamente.") % {"name":d.codigo}, True) return Redirect.to_action(request, "index") except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e)
def solution_state(request, state, key): """ Inactiva y reactiva el estado del la solución/plan """ id = SecurityKey.is_valid_key(request, key, "solution_%s" % state) if not id: return Redirect.to_action(request, "index") try: d = get_object_or_404(Solution, id=id) except: Message.error(request, ("Solución no se encuentra en la base de datos.")) return Redirect.to_action(request, "index") try: if state == "inactivar" and d.is_active == False: Message.error(request, ("La solución ya se encuentra inactivo.")) else: if state == "reactivar" and d.is_active == True: Message.error(request, ("La solución ya se encuentra activo.")) else: d.is_active = (True if state == "reactivar" else False) d.save() if d.id: if d.is_active: Message.info(request, ( "Solución <b>%(name)s</b> ha sido reactivado correctamente." ) % {"name": d.name}, True) else: Message.info(request, ( "Solución <b>%(name)s</b> ha sido inactivado correctamente." ) % {"name": d.name}, True) return Redirect.to_action(request, "index") except Exception, e: Message.error(request, e) return Redirect.to_action(request, "index")
def headquar_add(request): """ Agrega sede """ d = Headquar() d.phone = "" d.address = "" if request.method == "POST": try: sid = transaction.savepoint() d.name = request.POST.get("name") d.phone = request.POST.get("phone") d.address = request.POST.get("address") d.is_main = False d.locality_name = request.POST.get("locality_name") if request.POST.get("locality_name"): d.locality, is_locality_created = Locality.objects.get_or_create( name=request.POST.get("locality_name"), # name__iexact ) d.association_id = DataAccessToken.get_association_id(request.session) d.enterprise_id = DataAccessToken.get_enterprise_id(request.session) if normalize("NFKD", u"%s" % d.name).encode("ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode("ascii", "ignore").lower() for col in Headquar.objects.values("name").exclude(id=d.id).filter(enterprise_id=d.enterprise_id) ): raise Exception("La sede <b>%s</b> ya existe " % (d.name)) d.save() if d.id: Message.info(request, ("Sede <b>%(name)s</b> ha sido registrado correctamente.") % {"name":d.name}) return Redirect.to_action(request, "index") except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e)
def solution_edit(request, key): """ Actualiza solución """ id = SecurityKey.is_valid_key(request, key, "solution_upd") if not id: return Redirect.to_action(request, "index") d = None try: d = get_object_or_404(Solution, id=id) except: Message.error(request, ("Solución no se encuentra en la base de datos.")) return Redirect.to_action(request, "index") if request.method == "POST": try: d.name = request.POST.get("name") d.description = request.POST.get("description") if Solution.objects.exclude(id=d.id).filter( name=d.name).count() > 0: raise Exception( ("Solución <b>%(name)s</b> ya existe.") % {"name": d.name}) # salvar registro d.save() if d.id: Message.info(request, ( "Solución <b>%(name)s</b> ha sido actualizado correctamente." ) % {"name": d.name}) return Redirect.to_action(request, "index") except Exception, e: Message.error(request, e)
def headquar_change_association(request, key): """ Cambia de asociación a la sede """ id = SecurityKey.is_valid_key(request, key, "headquar_cha") if not id: return Redirect.to_action(request, "index") d = None try: d = get_object_or_404(Headquar, id=id) if d.association: d.association_name = d.association.name except: Message.error(request, ("Sede no se encuentra en la base de datos.")) return Redirect.to_action(request, "index") if request.method == "POST": try: # d.association_id = DataAccessToken.get_association_id(request.session) d.association_name = request.POST.get("association_name") try: d.association = Association.objects.get(name=d.association_name) except: raise Exception("La asociación <b>%s</b> no existe, vuelva a intentar " % (request.POST.get("association_name"))) # salvar registro d.save() if d.id: Message.info(request, ("Sede <b>%(name)s</b> ha sido cambiado de asociación correctamente.") % {"name":d.name}) return Redirect.to_action(request, "index") except Exception, e: Message.error(request, e)
def solution_state(request, state, key): """ Inactiva y reactiva el estado del la solución/plan """ id = SecurityKey.is_valid_key(request, key, "solution_%s" % state) if not id: return Redirect.to_action(request, "index") try: d = get_object_or_404(Solution, id=id) except: Message.error(request, ("Solución no se encuentra en la base de datos.")) return Redirect.to_action(request, "index") try: if state == "inactivar" and d.is_active == False: Message.error(request, ("La solución ya se encuentra inactivo.")) else: if state == "reactivar" and d.is_active == True: Message.error(request, ("La solución ya se encuentra activo.")) else: d.is_active = (True if state == "reactivar" else False) d.save() if d.id: if d.is_active: Message.info(request, ("Solución <b>%(name)s</b> ha sido reactivado correctamente.") % {"name":d.name}, True) else: Message.info(request, ("Solución <b>%(name)s</b> ha sido inactivado correctamente.") % {"name":d.name}, True) return Redirect.to_action(request, "index") except Exception, e: Message.error(request, e) return Redirect.to_action(request, "index")
def solution_delete(request, key): """ Elimina solución """ id = SecurityKey.is_valid_key(request, key, "solution_del") if not id: return Redirect.to_action(request, "index") try: d = get_object_or_404(Solution, id=id) except: Message.error(request, ("Solución no se encuentra en la base de datos.")) return Redirect.to_action(request, "index") try: # rastreando dependencias if d.module_set.count() > 0: raise Exception(("Solución <b>%(name)s</b> tiene módulos asignados.") % {"name":d.name}) if d.association_set.count() > 0: raise Exception(("Solución <b>%(name)s</b> está asignado en asociaciones.") % {"name":d.name}) if d.enterprise_set.count() > 0: raise Exception(("Solución <b>%(name)s</b> está asignado en empresas.") % {"name":d.name}) d.delete() if not d.id: Message.info(request, ("Solución <b>%(name)s</b> ha sido eliminado correctamente.") % {"name":d.name}, True) return Redirect.to_action(request, "index") except Exception, e: Message.error(request, e) return Redirect.to_action(request, "index") # endregion solution
def association_edit_current(request): """ Actualiza datos de la asociación a la que ingresó el usuario """ d = Association() try: d = get_object_or_404(Association, id=DataAccessToken.get_association_id(request.session)) except: Message.error(request, ("Asociación no seleccionada o no se encuentra en la base de datos.")) return Redirect.to(request, "/home/choice_headquar/") if request.method == "POST": try: sid = transaction.savepoint() d.name = request.POST.get("name") d.type_a = request.POST.get("type_a") d.solution_id = request.POST.get("solution_id") # solution=Solution.objects.get(id=d.solution_id) #no es necesario d.logo = request.POST.get("asociacion_logo") if normalize("NFKD", u"%s" % d.name).encode("ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode("ascii", "ignore").lower() for col in Association.objects.values("name").exclude(id=d.id) ): raise Exception(("Asociación <b>%(name)s</b> ya existe.") % {"name":d.name}) # salvar registro d.save() raise Exception(("Asociación <b>%(name)s</b> ya existe.") % {"name":d.name}) if d.id: Message.info(request, ("Asociación <b>%(name)s</b> ha sido actualizado correctamente.") % {"name":d.name}) except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e)
def producto_delete(request, key): """ Elimina producto """ id = SecurityKey.is_valid_key(request, key, "producto_del") if not id: return Redirect.to_action(request, "index") try: d = get_object_or_404(Producto, id=id) except: Message.error(request, ("Producto no se encuentra en la base de datos.")) return Redirect.to_action(request, "index") try: # sid = transaction.savepoint() d.delete() if not d.id: Message.info( request, ("Producto <b>%(codigo)s</b> ha sido eliminado correctamente.") % {"codigo": d.codigo}, True) return Redirect.to_action(request, "index") except Exception, e: # transaction.savepoint_rollback(sid) Message.error(request, e) return Redirect.to_action(request, "index")
def headquar_delete(request, key): """ Elimina sede """ id = SecurityKey.is_valid_key(request, key, "headquar_del") if not id: return Redirect.to_action(request, "index") try: d = get_object_or_404(Headquar, id=id) except: Message.error(request, ("Sede no se encuentra en la base de datos.")) return Redirect.to_action(request, "index") try: if d.enterprise.headquar_set.count() == 1: raise Exception(("Empresa <b>%(name)s</b> no puede quedar sin ninguna sede.") % {"name":d.enterprise.name}) if d.userprofileheadquar_set.count() > 0: raise Exception(("Sede <b>%(name)s de %(empresa)s</b> tiene usuarios y grupos asignados.") % {"name":d.name, "empresa":d.enterprise.name}) # agregue aquí sus otras relgas de negocio d.delete() if not d.id: Message.info(request, ("Sede <b>%(name)s de %(empresa)s</b> ha sido eliminado correctamente.") % {"name":d.name, "empresa":d.enterprise.name}, True) return Redirect.to_action(request, "index") except Exception, e: Message.error(request, e) return Redirect.to_action(request, "index")
def solution_edit(request, key): """ Actualiza solución """ id = SecurityKey.is_valid_key(request, key, "solution_upd") if not id: return Redirect.to_action(request, "index") d = None try: d = get_object_or_404(Solution, id=id) except: Message.error(request, ("Solución no se encuentra en la base de datos.")) return Redirect.to_action(request, "index") if request.method == "POST": try: d.name = request.POST.get("name") d.description = request.POST.get("description") if Solution.objects.exclude(id=d.id).filter(name=d.name).count() > 0: raise Exception(("Solución <b>%(name)s</b> ya existe.") % {"name":d.name}) # salvar registro d.save() if d.id: Message.info(request, ("Solución <b>%(name)s</b> ha sido actualizado correctamente.") % {"name":d.name}) return Redirect.to_action(request, "index") except Exception, e: Message.error(request, e)
def enterprise_add(request): """ Agrega empresa dentro de una asociación, para ello deberá agregarse con una sede Principal """ d = Enterprise() d.sede = "Principal" if request.method == "POST": try: sid = transaction.savepoint() d.name = request.POST.get("name") d.tax_id = request.POST.get("tax_id") d.type_e = request.POST.get("type_e") d.solution_id = request.POST.get("solution_id") # solution=Solution.objects.get(id=d.solution_id) #no es necesario if normalize("NFKD", u"%s" % d.name).encode( "ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode( "ascii", "ignore").lower() for col in Enterprise.objects.values("name").exclude( id=d.id)): raise Exception( ("Empresa <b>%(name)s</b> ya existe.") % {"name": d.name}) if Enterprise.objects.exclude(id=d.id).filter( tax_id=d.tax_id).count() > 0: raise Exception("La empresa con RUC <b>%s</b> ya existe " % (d.tax_id)) d.save() headquar = Headquar() headquar.name = request.POST.get("sede") headquar.association_id = DataAccessToken.get_association_id( request.session) headquar.enterprise = d if normalize("NFKD", u"%s" % headquar.name).encode( "ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode( "ascii", "ignore").lower() for col in Headquar.objects.values("name").exclude( id=headquar.id).filter( enterprise_id=headquar.enterprise_id)): raise Exception("La sede <b>%s</b> ya existe " % (headquar.name)) headquar.save() if d.id: Message.info(request, ( "Empresa <b>%(name)s</b> ha sido registrado correctamente." ) % {"name": d.name}) return Redirect.to_action(request, "index") except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e)
def enterprise_edit(request, key): """ Actualiza empresa """ id = SecurityKey.is_valid_key(request, key, "enterprise_upd") if not id: return Redirect.to_action(request, "index") d = None try: d = get_object_or_404(Enterprise, id=id) except: Message.error(request, ("Empresa no se encuentra en la base de datos.")) return Redirect.to_action(request, "index") if request.method == "POST": try: sid = transaction.savepoint() d.name = request.POST.get("name") d.tax_id = request.POST.get("tax_id") d.type_e = request.POST.get("type_e") d.solution_id = request.POST.get("solution_id") # solution=Solution.objects.get(id=d.solution_id) #no es necesario if normalize("NFKD", u"%s" % d.name).encode( "ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode( "ascii", "ignore").lower() for col in Enterprise.objects.values("name").exclude( id=d.id)): raise Exception( ("Empresa <b>%(name)s</b> ya existe.") % {"name": d.name}) if Enterprise.objects.exclude(id=d.id).filter( tax_id=d.tax_id).count() > 0: raise Exception("La empresa con RUC <b>%s</b> ya existe " % (d.tax_id)) # salvar registro d.save() if d.id: Message.info(request, ( "Empresa <b>%(name)s</b> ha sido actualizado correctamente." ) % {"name": d.name}) return Redirect.to_action(request, "index") except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e)
def enterprise_edit_current(request): """ Actualiza datos de la empresa a la que ingresó el usuario """ d = Enterprise() try: d = get_object_or_404(Enterprise, id=DataAccessToken.get_enterprise_id( request.session)) except: Message.error( request, ("Empresa no seleccionada o no se encuentra en la base de datos.")) return Redirect.to(request, "/home/choice_headquar/") if request.method == "POST": try: sid = transaction.savepoint() d.name = request.POST.get("name") d.tax_id = request.POST.get("tax_id") d.type_e = request.POST.get("type_e") d.solution_id = request.POST.get("solution_id") d.logo = request.POST.get("empresa_logo") # solution=Solution.objects.get(id=d.solution_id) #no es necesario if normalize("NFKD", u"%s" % d.name).encode( "ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode( "ascii", "ignore").lower() for col in Enterprise.objects.values("name").exclude( id=d.id)): raise Exception( ("Empresa <b>%(name)s</b> ya existe.") % {"name": d.name}) if Enterprise.objects.exclude(id=d.id).filter( tax_id=d.tax_id).count() > 0: raise Exception("La empresa con RUC <b>%s</b> ya existe " % (d.tax_id)) # salvar registro d.save() if d.id: Message.info(request, ( "Empresa <b>%(name)s</b> ha sido actualizado correctamente." ) % {"name": d.name}) except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e)
def headquar_edit(request, key): """ Actualiza sede """ id = SecurityKey.is_valid_key(request, key, "headquar_upd") if not id: return Redirect.to_action(request, "index") d = None try: d = get_object_or_404(Headquar, id=id) if d.locality: d.locality_name = d.locality.name except: Message.error(request, ("Sede no se encuentra en la base de datos.")) return Redirect.to_action(request, "index") if request.method == "POST": try: sid = transaction.savepoint() d.name = request.POST.get("name") d.phone = request.POST.get("phone") d.address = request.POST.get("address") d.locality_name = request.POST.get("locality_name").strip() if request.POST.get("locality_name").strip(): d.locality, is_locality_created = Locality.objects.get_or_create( name=request.POST.get( "locality_name").strip(), # name__iexact ) if normalize("NFKD", u"%s" % d.name).encode( "ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode( "ascii", "ignore").lower() for col in Headquar.objects.values("name").exclude( id=d.id).filter(enterprise_id=d.enterprise_id)): raise Exception("La sede <b>%s</b> ya existe " % (d.name)) # salvar registro d.save() if d.id: Message.info( request, ("Sede <b>%(name)s</b> ha sido actualizado correctamente.") % {"name": d.name}) return Redirect.to_action(request, "index") except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e)
def locality_edit(request, key): id = SecurityKey.is_valid_key(request, key, "locality_upd") if not id: return Redirect.to_action(request, "index") d = None try: d = get_object_or_404(Locality, id=id) # Locality.objects.get(id=id) except: # Locality.DoesNotExist Message.error(request, _("Locality not found in the database.")) return Redirect.to_action(request, "index") if request.method == "POST": try: sid = transaction.savepoint() # begin transaction, no amerita el decorador @transaction.atomic pero dejadlo para las nuevas version # Aquí asignar los datos d.name = request.POST.get("name") d.msnm = request.POST.get("msnm") if request.POST.get("date_create"): d.date_create = request.POST.get("date_create") d.is_active = True # para probar transaction # locality_type=LocalityType() # locality_type.name="Rural5" # if LocalityType.objects.filter(name = locality_type.name).count() > 0: # raise Exception(_("LocalityType <b>%(name)s</b> name's already in use.") % {"name":locality_type.name}) #trhow new Exception("msg") # locality_type.save() # d.locality_type=locality_type if normalize("NFKD", u"%s" % d.name).encode("ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode("ascii", "ignore").lower() for col in Locality.objects.values("name").exclude(id=d.id) # puede .filter() ): raise Exception(_("Locality <b>%(name)s</b> name's already in use.") % {"name":d.name}) # trhow new Exception("msg") # salvar registro d.save() # para probar transaction # raise Exception("Error para no salvar. Si funciona") if d.id: # transaction.savepoint_commit(sid) se colocaría solo al final, pero no amerita pk ya está decorado con @transaction.atomic Message.info(request, ("Localidad <b>%(name)s</b> ha sido actualizado correctamente.") % {"name":d.name}, True) return Redirect.to_action(request, "index") except Exception, e: transaction.savepoint_rollback(sid) # para reversar en caso de error en alguna de las tablas Message.error(request, e)
def solution_add(request): """ Agrega solución """ d = Solution() d.description = "" if request.method == "POST": try: d.name = request.POST.get("name") d.description = request.POST.get("description") if Solution.objects.exclude(id=d.id).filter(name=d.name).count() > 0: raise Exception(("Solución <b>%(name)s</b> ya existe.") % {"name":d.name}) d.save() if d.id: Message.info(request, ("Solución <b>%(name)s</b> ha sido registrado correctamente.") % {"name":d.name}) return Redirect.to_action(request, "index") except Exception, e: Message.error(request, e)
def association_edit_current(request): """ Actualiza datos de la asociación a la que ingresó el usuario """ d = Association() try: d = get_object_or_404(Association, id=DataAccessToken.get_association_id( request.session)) except: Message.error(request, ( "Asociación no seleccionada o no se encuentra en la base de datos." )) return Redirect.to(request, "/home/choice_headquar/") if request.method == "POST": try: sid = transaction.savepoint() d.name = request.POST.get("name") d.type_a = request.POST.get("type_a") d.solution_id = request.POST.get("solution_id") # solution=Solution.objects.get(id=d.solution_id) #no es necesario d.logo = request.POST.get("asociacion_logo") if normalize("NFKD", u"%s" % d.name).encode( "ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode( "ascii", "ignore").lower() for col in Association.objects.values("name").exclude( id=d.id)): raise Exception(("Asociación <b>%(name)s</b> ya existe.") % {"name": d.name}) # salvar registro d.save() raise Exception( ("Asociación <b>%(name)s</b> ya existe.") % {"name": d.name}) if d.id: Message.info(request, ( "Asociación <b>%(name)s</b> ha sido actualizado correctamente." ) % {"name": d.name}) except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e)
def enterprise_add(request): """ Agrega empresa dentro de una asociación, para ello deberá agregarse con una sede Principal """ d = Enterprise() d.sede = "Principal" if request.method == "POST": try: sid = transaction.savepoint() d.name = request.POST.get("name") d.tax_id = request.POST.get("tax_id") d.type_e = request.POST.get("type_e") d.solution_id = request.POST.get("solution_id") # solution=Solution.objects.get(id=d.solution_id) #no es necesario if normalize("NFKD", u"%s" % d.name).encode("ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode("ascii", "ignore").lower() for col in Enterprise.objects.values("name").exclude(id=d.id) ): raise Exception(("Empresa <b>%(name)s</b> ya existe.") % {"name":d.name}) if Enterprise.objects.exclude(id=d.id).filter(tax_id=d.tax_id).count() > 0: raise Exception("La empresa con RUC <b>%s</b> ya existe " % (d.tax_id)) d.save() headquar = Headquar() headquar.name = request.POST.get("sede") headquar.association_id = DataAccessToken.get_association_id(request.session) headquar.enterprise = d if normalize("NFKD", u"%s" % headquar.name).encode("ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode("ascii", "ignore").lower() for col in Headquar.objects.values("name").exclude(id=headquar.id).filter(enterprise_id=headquar.enterprise_id) ): raise Exception("La sede <b>%s</b> ya existe " % (headquar.name)) headquar.save() if d.id: Message.info(request, ("Empresa <b>%(name)s</b> ha sido registrado correctamente.") % {"name":d.name}) return Redirect.to_action(request, "index") except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e)
def headquar_add(request): """ Agrega sede """ d = Headquar() d.phone = "" d.address = "" if request.method == "POST": try: sid = transaction.savepoint() d.name = request.POST.get("name") d.phone = request.POST.get("phone") d.address = request.POST.get("address") d.is_main = False d.locality_name = request.POST.get("locality_name") if request.POST.get("locality_name"): d.locality, is_locality_created = Locality.objects.get_or_create( name=request.POST.get("locality_name"), # name__iexact ) d.association_id = DataAccessToken.get_association_id( request.session) d.enterprise_id = DataAccessToken.get_enterprise_id( request.session) if normalize("NFKD", u"%s" % d.name).encode( "ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode( "ascii", "ignore").lower() for col in Headquar.objects.values("name").exclude( id=d.id).filter(enterprise_id=d.enterprise_id)): raise Exception("La sede <b>%s</b> ya existe " % (d.name)) d.save() if d.id: Message.info( request, ("Sede <b>%(name)s</b> ha sido registrado correctamente.") % {"name": d.name}) return Redirect.to_action(request, "index") except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e)
def headquar_edit(request, key): """ Actualiza sede """ id = SecurityKey.is_valid_key(request, key, "headquar_upd") if not id: return Redirect.to_action(request, "index") d = None try: d = get_object_or_404(Headquar, id=id) if d.locality: d.locality_name = d.locality.name except: Message.error(request, ("Sede no se encuentra en la base de datos.")) return Redirect.to_action(request, "index") if request.method == "POST": try: sid = transaction.savepoint() d.name = request.POST.get("name") d.phone = request.POST.get("phone") d.address = request.POST.get("address") d.locality_name = request.POST.get("locality_name").strip() if request.POST.get("locality_name").strip(): d.locality, is_locality_created = Locality.objects.get_or_create( name=request.POST.get("locality_name").strip(), # name__iexact ) if normalize("NFKD", u"%s" % d.name).encode("ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode("ascii", "ignore").lower() for col in Headquar.objects.values("name").exclude(id=d.id).filter(enterprise_id=d.enterprise_id) ): raise Exception("La sede <b>%s</b> ya existe " % (d.name)) # salvar registro d.save() if d.id: Message.info(request, ("Sede <b>%(name)s</b> ha sido actualizado correctamente.") % {"name":d.name}) return Redirect.to_action(request, "index") except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e)
def enterprise_edit(request, key): """ Actualiza empresa """ id = SecurityKey.is_valid_key(request, key, "enterprise_upd") if not id: return Redirect.to_action(request, "index") d = None try: d = get_object_or_404(Enterprise, id=id) except: Message.error(request, ("Empresa no se encuentra en la base de datos.")) return Redirect.to_action(request, "index") if request.method == "POST": try: sid = transaction.savepoint() d.name = request.POST.get("name") d.tax_id = request.POST.get("tax_id") d.type_e = request.POST.get("type_e") d.solution_id = request.POST.get("solution_id") # solution=Solution.objects.get(id=d.solution_id) #no es necesario if normalize("NFKD", u"%s" % d.name).encode("ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode("ascii", "ignore").lower() for col in Enterprise.objects.values("name").exclude(id=d.id) ): raise Exception(("Empresa <b>%(name)s</b> ya existe.") % {"name":d.name}) if Enterprise.objects.exclude(id=d.id).filter(tax_id=d.tax_id).count() > 0: raise Exception("La empresa con RUC <b>%s</b> ya existe " % (d.tax_id)) # salvar registro d.save() if d.id: Message.info(request, ("Empresa <b>%(name)s</b> ha sido actualizado correctamente.") % {"name":d.name}) return Redirect.to_action(request, "index") except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e)
def locality_delete(request, key): id = SecurityKey.is_valid_key(request, key, "locality_del") if not id: return Redirect.to_action(request, "index") try: d = get_object_or_404(Locality, id=id) # Locality.objects.get(id=id) except: # Locality.DoesNotExist Message.error(request, _("Locality not found in the database.")) return Redirect.to_action(request, "index") try: # rastreando dependencias if d.headquart_set.count() > 0: raise Exception(("Localidad <b>%(name)s</b> está asignado en headquart.") % {"name":d.name}) d.delete() if not d.id: Message.info(request, ("Localidad <b>%(name)s</b> ha sido eliminado correctamente.") % {"name":d.name}, True) return Redirect.to_action(request, "index") except Exception, e: Message.error(request, e) return Redirect.to_action(request, "index")
def solution_delete(request, key): """ Elimina solución """ id = SecurityKey.is_valid_key(request, key, "solution_del") if not id: return Redirect.to_action(request, "index") try: d = get_object_or_404(Solution, id=id) except: Message.error(request, ("Solución no se encuentra en la base de datos.")) return Redirect.to_action(request, "index") try: # rastreando dependencias if d.module_set.count() > 0: raise Exception( ("Solución <b>%(name)s</b> tiene módulos asignados.") % {"name": d.name}) if d.association_set.count() > 0: raise Exception( ("Solución <b>%(name)s</b> está asignado en asociaciones.") % {"name": d.name}) if d.enterprise_set.count() > 0: raise Exception( ("Solución <b>%(name)s</b> está asignado en empresas.") % {"name": d.name}) d.delete() if not d.id: Message.info( request, ("Solución <b>%(name)s</b> ha sido eliminado correctamente.") % {"name": d.name}, True) return Redirect.to_action(request, "index") except Exception, e: Message.error(request, e) return Redirect.to_action(request, "index") # endregion solution
def solution_add(request): """ Agrega solución """ d = Solution() d.description = "" if request.method == "POST": try: d.name = request.POST.get("name") d.description = request.POST.get("description") if Solution.objects.exclude(id=d.id).filter( name=d.name).count() > 0: raise Exception( ("Solución <b>%(name)s</b> ya existe.") % {"name": d.name}) d.save() if d.id: Message.info(request, ( "Solución <b>%(name)s</b> ha sido registrado correctamente." ) % {"name": d.name}) return Redirect.to_action(request, "index") except Exception, e: Message.error(request, e)
def headquar_change_association(request, key): """ Cambia de asociación a la sede """ id = SecurityKey.is_valid_key(request, key, "headquar_cha") if not id: return Redirect.to_action(request, "index") d = None try: d = get_object_or_404(Headquar, id=id) if d.association: d.association_name = d.association.name except: Message.error(request, ("Sede no se encuentra en la base de datos.")) return Redirect.to_action(request, "index") if request.method == "POST": try: # d.association_id = DataAccessToken.get_association_id(request.session) d.association_name = request.POST.get("association_name") try: d.association = Association.objects.get( name=d.association_name) except: raise Exception( "La asociación <b>%s</b> no existe, vuelva a intentar " % (request.POST.get("association_name"))) # salvar registro d.save() if d.id: Message.info(request, ( "Sede <b>%(name)s</b> ha sido cambiado de asociación correctamente." ) % {"name": d.name}) return Redirect.to_action(request, "index") except Exception, e: Message.error(request, e)
def enterprise_delete(request, key): """ Elimina empresa con todas sus sedes """ id = SecurityKey.is_valid_key(request, key, "enterprise_del") if not id: return Redirect.to_action(request, "index") try: d = get_object_or_404(Enterprise, id=id) except: Message.error(request, ("Empresa no se encuentra en la base de datos.")) return Redirect.to_action(request, "index") try: sid = transaction.savepoint() association = Association.objects.get( id=DataAccessToken.get_association_id(request.session)) if Enterprise.objects.filter( headquar__association_id=DataAccessToken.get_association_id( request.session)).count() == 1: raise Exception(( "Asociación <b>%(name)s</b> no puede quedar sin ninguna sede asociada." ) % {"name": association.name}) if d.userprofileenterprise_set.count() > 0: raise Exception( ("Empresa <b>%(name)s</b> tiene usuarios y grupos asignados.") % {"name": d.name}) # agregue aquí sus otras relgas de negocio d.delete() if not d.id: Message.info( request, ("Empresa <b>%(name)s</b> ha sido eliminado correctamente.") % {"name": d.name}, True) return Redirect.to_action(request, "index") except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e) return Redirect.to_action(request, "index")
def producto_delete(request, key): """ Elimina producto """ id = SecurityKey.is_valid_key(request, key, "producto_del") if not id: return Redirect.to_action(request, "index") try: d = get_object_or_404(Producto, id=id) except: Message.error(request, ("Producto no se encuentra en la base de datos.")) return Redirect.to_action(request, "index") try: # sid = transaction.savepoint() d.delete() if not d.id: Message.info(request, ("Producto <b>%(codigo)s</b> ha sido eliminado correctamente.") % {"codigo":d.codigo}, True) return Redirect.to_action(request, "index") except Exception, e: # transaction.savepoint_rollback(sid) Message.error(request, e) return Redirect.to_action(request, "index")
def headquar_delete(request, key): """ Elimina sede """ id = SecurityKey.is_valid_key(request, key, "headquar_del") if not id: return Redirect.to_action(request, "index") try: d = get_object_or_404(Headquar, id=id) except: Message.error(request, ("Sede no se encuentra en la base de datos.")) return Redirect.to_action(request, "index") try: if d.enterprise.headquar_set.count() == 1: raise Exception( ("Empresa <b>%(name)s</b> no puede quedar sin ninguna sede.") % {"name": d.enterprise.name}) if d.userprofileheadquar_set.count() > 0: raise Exception(( "Sede <b>%(name)s de %(empresa)s</b> tiene usuarios y grupos asignados." ) % { "name": d.name, "empresa": d.enterprise.name }) # agregue aquí sus otras relgas de negocio d.delete() if not d.id: Message.info(request, ( "Sede <b>%(name)s de %(empresa)s</b> ha sido eliminado correctamente." ) % { "name": d.name, "empresa": d.enterprise.name }, True) return Redirect.to_action(request, "index") except Exception, e: Message.error(request, e) return Redirect.to_action(request, "index")
def enterprise_edit_current(request): """ Actualiza datos de la empresa a la que ingresó el usuario """ d = Enterprise() try: d = get_object_or_404(Enterprise, id=DataAccessToken.get_enterprise_id(request.session)) except: Message.error(request, ("Empresa no seleccionada o no se encuentra en la base de datos.")) return Redirect.to(request, "/home/choice_headquar/") if request.method == "POST": try: sid = transaction.savepoint() d.name = request.POST.get("name") d.tax_id = request.POST.get("tax_id") d.type_e = request.POST.get("type_e") d.solution_id = request.POST.get("solution_id") d.logo = request.POST.get("empresa_logo") # solution=Solution.objects.get(id=d.solution_id) #no es necesario if normalize("NFKD", u"%s" % d.name).encode("ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode("ascii", "ignore").lower() for col in Enterprise.objects.values("name").exclude(id=d.id) ): raise Exception(("Empresa <b>%(name)s</b> ya existe.") % {"name":d.name}) if Enterprise.objects.exclude(id=d.id).filter(tax_id=d.tax_id).count() > 0: raise Exception("La empresa con RUC <b>%s</b> ya existe " % (d.tax_id)) # salvar registro d.save() if d.id: Message.info(request, ("Empresa <b>%(name)s</b> ha sido actualizado correctamente.") % {"name":d.name}) except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e)
def signup_sys(request): if request.user.is_authenticated(): return HttpResponseRedirect("/accounts/choice_headquar/") d = Person() d.first_name = "" d.last_name = "" d.identity_num = "" if request.method == "POST": try: sid = transaction.savepoint() d.first_name = request.POST.get("first_name") d.last_name = request.POST.get("last_name") d.username = request.POST.get("login") d.enterprise_name = request.POST.get("enterprise_name") d.enterprise_tax_id = request.POST.get("enterprise_tax_id") d.association_name = request.POST.get("association_name") d.association_type_a = request.POST.get("association_type_a") d.solution_id = request.POST.get("solution_id") d.email = request.POST.get("email") d.photo = request.POST.get("persona_fotografia") d.identity_type = Person.DEFAULT #request.POST.get("identity_type") d.identity_num = request.POST.get("identity_num") identity_type_display = dict((x, y) for x, y in Person.IDENTITY_TYPES)[d.identity_type] solution = Solution.objects.get(id=d.solution_id) d.solution = solution if User.objects.filter(username=d.username).count() > 0: raise Exception("El usuario <b>%s</b> ya existe " % d.username) if User.objects.filter(email=d.email).count() > 0: raise Exception("El email <b>%s</b> ya existe " % d.email) user = User.objects.create_user(username=d.username, email=d.email, password=request.POST.get("password")) if normalize("NFKD", u"%s %s" % (d.first_name, d.last_name)).encode("ascii", "ignore").lower() in list( normalize("NFKD", u"%s %s" % (col["first_name"], col["last_name"])).encode("ascii", "ignore").lower() for col in Person.objects.values("first_name", "last_name").filter(identity_type=d.identity_type, identity_num=d.identity_num) ): raise Exception("La persona <b>%s %s</b> con %s:<b>%s</b> ya existe " % (d.first_name, d.last_name, identity_type_display, d.identity_num)) if Person.objects.filter(identity_type=d.identity_type, identity_num=d.identity_num).count() > 0: raise Exception("La persona con %s:<b>%s</b> ya existe " % (identity_type_display, d.identity_num)) person = Person(first_name=d.first_name, last_name=d.last_name, identity_type=d.identity_type, identity_num=d.identity_num, photo=d.photo) person.save() profile = Profile(user=user) profile.person = person profile.save() association = Association(name=d.association_name, type_a=d.association_type_a, solution=solution) if normalize("NFKD", u"%s" % d.association_name).encode("ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode("ascii", "ignore").lower() for col in Association.objects.values("name") ): raise Exception("La asociación <b>%s</b> ya existe " % (d.association_name)) association.save() enterprise = Enterprise(name=d.enterprise_name, tax_id=d.enterprise_tax_id, type_e=d.association_type_a, solution=solution) if normalize("NFKD", u"%s" % d.enterprise_name).encode("ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode("ascii", "ignore").lower() for col in Enterprise.objects.values("name") ): raise Exception("La empresa <b>%s</b> ya existe " % (d.enterprise_name)) if Enterprise.objects.filter(tax_id=d.enterprise_tax_id).count() > 0: raise Exception("La empresa con RUC <b>%s</b> ya existe " % (d.enterprise_tax_id)) enterprise.save() headquar = Headquar(name="Principal", association=association, enterprise=enterprise) headquar.save() # asigna permisos al usuario para manipular datos de cierta sede, empresa o asociación group_dist_list = [] for module in solution.module_set.all(): # .distinct() for group in module.initial_groups.all() : if len(group_dist_list) == 0 : group_dist_list.append(group.id) user.groups.add(group) user_profile_association = UserProfileAssociation() user_profile_association.user = user user_profile_association.association = association user_profile_association.group = group user_profile_association.save() user_profile_enterprise = UserProfileEnterprise() user_profile_enterprise.user = user user_profile_enterprise.enterprise = enterprise user_profile_enterprise.group = group user_profile_enterprise.save() user_profile_headquar = UserProfileHeadquar() user_profile_headquar.user = user user_profile_headquar.headquar = headquar user_profile_headquar.group = group user_profile_headquar.save() else : if group.id not in group_dist_list: group_dist_list.append(group.id) user.groups.add(group) user_profile_association = UserProfileAssociation() user_profile_association.user = user user_profile_association.association = association user_profile_association.group = group user_profile_association.save() user_profile_enterprise = UserProfileEnterprise() user_profile_enterprise.user = user user_profile_enterprise.enterprise = enterprise user_profile_enterprise.group = group user_profile_enterprise.save() user_profile_headquar = UserProfileHeadquar() user_profile_headquar.user = user user_profile_headquar.headquar = headquar user_profile_headquar.group = group user_profile_headquar.save() Message.info(request, ("Cuenta <b>%(name)s</b> ha sido registrado correctamente!.") % {"name":d.username}) if request.is_ajax(): request.path = "/accounts/login/" # /app/controller_path/action/$params return redirect("/accounts/login/") # return login_sys(request) else: return redirect("/accounts/login/") except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e)
normalize("NFKD", u"%s %s" % (col["first_name"], col["last_name"])).encode("ascii", "ignore").lower() for col in Person.objects.values("first_name", "last_name").exclude(id=person.id).filter(identity_type=d.identity_type, identity_num=d.identity_num) ): raise Exception("La persona <b>%s %s</b> y %s:<b>%s</b> ya existe " % (d.first_name, d.last_name, identity_type_display, d.identity_num)) if Person.objects.exclude(id=person.id).filter(identity_type=d.identity_type, identity_num=d.identity_num).count() > 0: raise Exception("La persona con %s:<b>%s</b> ya existe " % (identity_type_display, d.identity_num)) person.first_name = request.POST.get("first_name") person.last_name = request.POST.get("last_name") person.identity_type = request.POST.get("identity_type") person.identity_num = request.POST.get("identity_num") person.photo = request.POST.get("persona_fotografia") person.save() d.photo = person.photo if d.id: Message.info(request, ("Usuario <b>%(name)s</b> ha sido actualizado correctamente.") % {"name":d.username}, True) return Redirect.to(request, "/accounts/choice_headquar/") except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e) try: user_profile_headquar_list = UserProfileHeadquar.objects.filter(user=d).order_by("headquar") user_profile_enterprise_list = UserProfileEnterprise.objects.filter(user=d).order_by("enterprise") user_profile_association_list = UserProfileAssociation.objects.filter(user=d).order_by("association") except Exception, e: Message.error(request, e) c = {
d.precio_venta = request.POST.get("precio_venta") if request.POST.get("fecha_venc"): d.fecha_venc = request.POST.get("fecha_venc") if request.POST.get("categoria_nombre"): d.categoria, is_created = Categoria.objects.get_or_create( nombre=request.POST.get("categoria_nombre"), ) if Producto.objects.filter(codigo=d.codigo).exclude(id=d.id, headquar_id=d.headquar_id).count() > 0: raise Exception("El producto <b>%s</b> ya existe " % d.codigo) d.save() # raise Exception( "El producto <b>%s</b> ya existe " % d.codigo ) if d.id: Message.info(request, ("Producto <b>%(name)s</b> ha sido actualizado correctamente.") % {"name":d.codigo}, True) return Redirect.to_action(request, "index") except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e) categoria_nombre_list = [] try: categoria_nombre_list = json.dumps(list(col["nombre"] + "" for col in Categoria.objects.values("nombre").filter().order_by("nombre"))) print "PV=%s" % d.precio_venta except Exception, e: Message.error(request, e) c = { "page_module":("Gestión de productos"), "page_title":("Actualizar producto."), "d":d,
def add_enterprise(request): d = Enterprise() if request.method == "POST": try: sid = transaction.savepoint() d.enterprise_name = request.POST.get("enterprise_name") d.enterprise_tax_id = request.POST.get("enterprise_tax_id") d.association_name = request.POST.get("association_name") d.association_type_a = request.POST.get("association_type_a") d.solution_id = request.POST.get("solution_id") solution = Solution.objects.get(id=d.solution_id) d.logo = request.POST.get("empresa_logo") user = request.user association = Association(name=d.association_name, type_a=d.association_type_a, solution=solution, logo=d.logo) if normalize("NFKD", u"%s" % d.association_name).encode("ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode("ascii", "ignore").lower() for col in Association.objects.values("name") ): raise Exception("La asociación <b>%s</b> ya existe " % (d.association_name)) association.save() enterprise = Enterprise(name=d.enterprise_name, tax_id=d.enterprise_tax_id, type_e=d.association_type_a, solution=solution, logo=d.logo) if normalize("NFKD", u"%s" % d.enterprise_name).encode("ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode("ascii", "ignore").lower() for col in Enterprise.objects.values("name") ): raise Exception("La empresa <b>%s</b> ya existe " % (d.enterprise_name)) if Enterprise.objects.filter(tax_id=d.enterprise_tax_id).count() > 0: raise Exception("La empresa con RUC <b>%s</b> ya existe " % (d.enterprise_tax_id)) enterprise.save() headquar = Headquar(name="Principal", association=association, enterprise=enterprise) headquar.save() # asigna permisos al usuario para manipular datos de cierta sede, empresa o asociación group_dist_list = [] for module in solution.module_set.all(): # .distinct() for group in module.initial_groups.all() : if len(group_dist_list) == 0 : group_dist_list.append(group.id) user.groups.add(group) user_profile_association = UserProfileAssociation() user_profile_association.user = user user_profile_association.association = association user_profile_association.group = group user_profile_association.save() user_profile_enterprise = UserProfileEnterprise() user_profile_enterprise.user = user user_profile_enterprise.enterprise = enterprise user_profile_enterprise.group = group user_profile_enterprise.save() user_profile_headquar = UserProfileHeadquar() user_profile_headquar.user = user user_profile_headquar.headquar = headquar user_profile_headquar.group = group user_profile_headquar.save() else : if group.id not in group_dist_list: group_dist_list.append(group.id) user.groups.add(group) user_profile_association = UserProfileAssociation() user_profile_association.user = user user_profile_association.association = association user_profile_association.group = group user_profile_association.save() user_profile_enterprise = UserProfileEnterprise() user_profile_enterprise.user = user user_profile_enterprise.enterprise = enterprise user_profile_enterprise.group = group user_profile_enterprise.save() user_profile_headquar = UserProfileHeadquar() user_profile_headquar.user = user user_profile_headquar.headquar = headquar user_profile_headquar.group = group user_profile_headquar.save() Message.info(request, ("Empresa <b>%(name)s</b> ha sido registrado correctamente!.") % {"name":d.enterprise_name}) return Redirect.to(request, "/accounts/choice_headquar/") except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e)
identity_type=d.identity_type, identity_num=d.identity_num).count() > 0: raise Exception("La persona con %s:<b>%s</b> ya existe " % (identity_type_display, d.identity_num)) person.first_name = request.POST.get("first_name") person.last_name = request.POST.get("last_name") person.identity_type = request.POST.get("identity_type") person.identity_num = request.POST.get("identity_num") person.photo = request.POST.get("persona_fotografia") person.save() d.photo = person.photo if d.id: Message.info(request, ( "Usuario <b>%(name)s</b> ha sido actualizado correctamente." ) % {"name": d.username}, True) return Redirect.to(request, "/accounts/choice_headquar/") except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e) try: user_profile_headquar_list = UserProfileHeadquar.objects.filter( user=d).order_by("headquar") user_profile_enterprise_list = UserProfileEnterprise.objects.filter( user=d).order_by("enterprise") user_profile_association_list = UserProfileAssociation.objects.filter( user=d).order_by("association")
def signup_sys(request): if request.user.is_authenticated(): return HttpResponseRedirect("/accounts/choice_headquar/") d = Person() d.first_name = "" d.last_name = "" d.identity_num = "" if request.method == "POST": try: sid = transaction.savepoint() d.first_name = request.POST.get("first_name") d.last_name = request.POST.get("last_name") d.username = request.POST.get("login") d.enterprise_name = request.POST.get("enterprise_name") d.enterprise_tax_id = request.POST.get("enterprise_tax_id") d.association_name = request.POST.get("association_name") d.association_type_a = request.POST.get("association_type_a") d.solution_id = request.POST.get("solution_id") d.email = request.POST.get("email") d.photo = request.POST.get("persona_fotografia") d.identity_type = Person.DEFAULT #request.POST.get("identity_type") d.identity_num = request.POST.get("identity_num") identity_type_display = dict( (x, y) for x, y in Person.IDENTITY_TYPES)[d.identity_type] solution = Solution.objects.get(id=d.solution_id) d.solution = solution if User.objects.filter(username=d.username).count() > 0: raise Exception("El usuario <b>%s</b> ya existe " % d.username) if User.objects.filter(email=d.email).count() > 0: raise Exception("El email <b>%s</b> ya existe " % d.email) user = User.objects.create_user( username=d.username, email=d.email, password=request.POST.get("password")) if normalize( "NFKD", u"%s %s" % (d.first_name, d.last_name) ).encode("ascii", "ignore").lower() in list( normalize("NFKD", u"%s %s" % (col["first_name"], col["last_name"])).encode( "ascii", "ignore").lower() for col in Person.objects.values("first_name", "last_name") .filter(identity_type=d.identity_type, identity_num=d.identity_num)): raise Exception( "La persona <b>%s %s</b> con %s:<b>%s</b> ya existe " % (d.first_name, d.last_name, identity_type_display, d.identity_num)) if Person.objects.filter(identity_type=d.identity_type, identity_num=d.identity_num).count() > 0: raise Exception("La persona con %s:<b>%s</b> ya existe " % (identity_type_display, d.identity_num)) person = Person(first_name=d.first_name, last_name=d.last_name, identity_type=d.identity_type, identity_num=d.identity_num, photo=d.photo) person.save() profile = Profile(user=user) profile.person = person profile.save() association = Association(name=d.association_name, type_a=d.association_type_a, solution=solution) if normalize("NFKD", u"%s" % d.association_name).encode( "ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode( "ascii", "ignore").lower() for col in Association.objects.values("name")): raise Exception("La asociación <b>%s</b> ya existe " % (d.association_name)) association.save() enterprise = Enterprise(name=d.enterprise_name, tax_id=d.enterprise_tax_id, type_e=d.association_type_a, solution=solution) if normalize("NFKD", u"%s" % d.enterprise_name).encode( "ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode( "ascii", "ignore").lower() for col in Enterprise.objects.values("name")): raise Exception("La empresa <b>%s</b> ya existe " % (d.enterprise_name)) if Enterprise.objects.filter( tax_id=d.enterprise_tax_id).count() > 0: raise Exception("La empresa con RUC <b>%s</b> ya existe " % (d.enterprise_tax_id)) enterprise.save() headquar = Headquar(name="Principal", association=association, enterprise=enterprise) headquar.save() # asigna permisos al usuario para manipular datos de cierta sede, empresa o asociación group_dist_list = [] for module in solution.module_set.all(): # .distinct() for group in module.initial_groups.all(): if len(group_dist_list) == 0: group_dist_list.append(group.id) user.groups.add(group) user_profile_association = UserProfileAssociation() user_profile_association.user = user user_profile_association.association = association user_profile_association.group = group user_profile_association.save() user_profile_enterprise = UserProfileEnterprise() user_profile_enterprise.user = user user_profile_enterprise.enterprise = enterprise user_profile_enterprise.group = group user_profile_enterprise.save() user_profile_headquar = UserProfileHeadquar() user_profile_headquar.user = user user_profile_headquar.headquar = headquar user_profile_headquar.group = group user_profile_headquar.save() else: if group.id not in group_dist_list: group_dist_list.append(group.id) user.groups.add(group) user_profile_association = UserProfileAssociation() user_profile_association.user = user user_profile_association.association = association user_profile_association.group = group user_profile_association.save() user_profile_enterprise = UserProfileEnterprise() user_profile_enterprise.user = user user_profile_enterprise.enterprise = enterprise user_profile_enterprise.group = group user_profile_enterprise.save() user_profile_headquar = UserProfileHeadquar() user_profile_headquar.user = user user_profile_headquar.headquar = headquar user_profile_headquar.group = group user_profile_headquar.save() Message.info( request, ("Cuenta <b>%(name)s</b> ha sido registrado correctamente!.") % {"name": d.username}) if request.is_ajax(): request.path = "/accounts/login/" # /app/controller_path/action/$params return redirect("/accounts/login/") # return login_sys(request) else: return redirect("/accounts/login/") except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e)
def add_enterprise(request): d = Enterprise() if request.method == "POST": try: sid = transaction.savepoint() d.enterprise_name = request.POST.get("enterprise_name") d.enterprise_tax_id = request.POST.get("enterprise_tax_id") d.association_name = request.POST.get("association_name") d.association_type_a = request.POST.get("association_type_a") d.solution_id = request.POST.get("solution_id") solution = Solution.objects.get(id=d.solution_id) d.logo = request.POST.get("empresa_logo") user = request.user association = Association(name=d.association_name, type_a=d.association_type_a, solution=solution, logo=d.logo) if normalize("NFKD", u"%s" % d.association_name).encode( "ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode( "ascii", "ignore").lower() for col in Association.objects.values("name")): raise Exception("La asociación <b>%s</b> ya existe " % (d.association_name)) association.save() enterprise = Enterprise(name=d.enterprise_name, tax_id=d.enterprise_tax_id, type_e=d.association_type_a, solution=solution, logo=d.logo) if normalize("NFKD", u"%s" % d.enterprise_name).encode( "ascii", "ignore").lower() in list( normalize("NFKD", u"%s" % col["name"]).encode( "ascii", "ignore").lower() for col in Enterprise.objects.values("name")): raise Exception("La empresa <b>%s</b> ya existe " % (d.enterprise_name)) if Enterprise.objects.filter( tax_id=d.enterprise_tax_id).count() > 0: raise Exception("La empresa con RUC <b>%s</b> ya existe " % (d.enterprise_tax_id)) enterprise.save() headquar = Headquar(name="Principal", association=association, enterprise=enterprise) headquar.save() # asigna permisos al usuario para manipular datos de cierta sede, empresa o asociación group_dist_list = [] for module in solution.module_set.all(): # .distinct() for group in module.initial_groups.all(): if len(group_dist_list) == 0: group_dist_list.append(group.id) user.groups.add(group) user_profile_association = UserProfileAssociation() user_profile_association.user = user user_profile_association.association = association user_profile_association.group = group user_profile_association.save() user_profile_enterprise = UserProfileEnterprise() user_profile_enterprise.user = user user_profile_enterprise.enterprise = enterprise user_profile_enterprise.group = group user_profile_enterprise.save() user_profile_headquar = UserProfileHeadquar() user_profile_headquar.user = user user_profile_headquar.headquar = headquar user_profile_headquar.group = group user_profile_headquar.save() else: if group.id not in group_dist_list: group_dist_list.append(group.id) user.groups.add(group) user_profile_association = UserProfileAssociation() user_profile_association.user = user user_profile_association.association = association user_profile_association.group = group user_profile_association.save() user_profile_enterprise = UserProfileEnterprise() user_profile_enterprise.user = user user_profile_enterprise.enterprise = enterprise user_profile_enterprise.group = group user_profile_enterprise.save() user_profile_headquar = UserProfileHeadquar() user_profile_headquar.user = user user_profile_headquar.headquar = headquar user_profile_headquar.group = group user_profile_headquar.save() Message.info( request, ("Empresa <b>%(name)s</b> ha sido registrado correctamente!.") % {"name": d.enterprise_name}) return Redirect.to(request, "/accounts/choice_headquar/") except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e)
def login_sys(request): d = User() c = { "page_module": ("Login"), "page_title": ("Login."), "d": d, } if request.user.is_authenticated(): try: # intentar cargar la última session profile = Profile.objects.get(user_id=request.user.id) if profile.last_headquar_id and profile.last_module_id: if request.is_ajax(): request.path = "/accounts/load_access/%s/%s/" % ( profile.last_headquar_id, profile.last_module_id ) # /app/controller_path/action/$params return load_access(request, profile.last_headquar_id, profile.last_module_id) else: return redirect( "/accounts/load_access/%s/%s/" % (profile.last_headquar_id, profile.last_module_id)) except: pass return HttpResponseRedirect("/accounts/choice_headquar/") if request.method == "POST": d.username = request.POST.get("login") password = request.POST.get("password") if '@' in d.username: try: check = User.objects.get(email=d.username) d.username = check.username except: pass account = authenticate(username=d.username, password=password) if account is not None and account.is_active is True: login(request, account) Access.objects.create( #access_type=Access.INPUT, ip=request.META['REMOTE_ADDR'], session_key=request.session['_auth_user_id'], #account.pk, user=account, ) # cargando sesión para el usuario. no necesita # request.session["id"] = "Hola" if request.REQUEST.get("next"): return HttpResponseRedirect(request.REQUEST.get("next")) #redirect_to_login(request.REQUEST.get("next")) try: profile = Profile.objects.get(user_id=account.id) if profile: Message.info( request, ("Bienvenido <b>%(name)s %(ape)s</b>.") % { "name": account.profile.person.first_name, "ape": account.profile.person.last_name }) except: Message.info(request, ("Bienvenido <b>%(name)s</b>.") % {"name": account.username}) pass try: # intentar cargar la última session profile = Profile.objects.get(user_id=request.user.id) if profile.last_headquar_id and profile.last_module_id: if request.is_ajax(): request.path = "/accounts/load_access/%s/%s/" % ( profile.last_headquar_id, profile.last_module_id ) # /app/controller_path/action/$params return load_access(request, profile.last_headquar_id, profile.last_module_id) else: return redirect( "/accounts/load_access/%s/%s/" % (profile.last_headquar_id, profile.last_module_id)) except: pass return HttpResponseRedirect("/accounts/choice_headquar/") else: Message.error(request, ( "Contaseña para <b>%(name)s</b> no válido, o el usuario no existe o no está activo. " ) % {"name": d.username}) return render_to_response("accounts/login.html", c, context_instance=RequestContext(request)) else: """ user is not submitting the form, show the login form """ return render_to_response("accounts/login.html", c, context_instance=RequestContext(request))
def login_sys(request): d = User() c = { "page_module":("Login"), "page_title":("Login."), "d":d, } if request.user.is_authenticated(): try: # intentar cargar la última session profile = Profile.objects.get(user_id=request.user.id) if profile.last_headquar_id and profile.last_module_id: if request.is_ajax(): request.path = "/accounts/load_access/%s/%s/" % (profile.last_headquar_id, profile.last_module_id) # /app/controller_path/action/$params return load_access(request, profile.last_headquar_id, profile.last_module_id) else: return redirect("/accounts/load_access/%s/%s/" % (profile.last_headquar_id, profile.last_module_id)) except: pass return HttpResponseRedirect("/accounts/choice_headquar/") if request.method == "POST": d.username = request.POST.get("login") password = request.POST.get("password") if '@' in d.username: try: check = User.objects.get(email=d.username) d.username = check.username except: pass account = authenticate(username=d.username, password=password) if account is not None and account.is_active is True: login(request, account) Access.objects.create( #access_type=Access.INPUT, ip=request.META['REMOTE_ADDR'], session_key=request.session['_auth_user_id'], #account.pk, user=account, ) # cargando sesión para el usuario. no necesita # request.session["id"] = "Hola" if request.REQUEST.get("next"): return HttpResponseRedirect(request.REQUEST.get("next")) #redirect_to_login(request.REQUEST.get("next")) try: profile = Profile.objects.get(user_id=account.id) if profile: Message.info(request, ("Bienvenido <b>%(name)s %(ape)s</b>.") % {"name":account.profile.person.first_name,"ape":account.profile.person.last_name}) except: Message.info(request, ("Bienvenido <b>%(name)s</b>.") % {"name":account.username}) pass try: # intentar cargar la última session profile = Profile.objects.get(user_id=request.user.id) if profile.last_headquar_id and profile.last_module_id: if request.is_ajax(): request.path = "/accounts/load_access/%s/%s/" % (profile.last_headquar_id, profile.last_module_id) # /app/controller_path/action/$params return load_access(request, profile.last_headquar_id, profile.last_module_id) else: return redirect("/accounts/load_access/%s/%s/" % (profile.last_headquar_id, profile.last_module_id)) except: pass return HttpResponseRedirect("/accounts/choice_headquar/") else: Message.error(request, ("Contaseña para <b>%(name)s</b> no válido, o el usuario no existe o no está activo. ") % {"name":d.username}) return render_to_response("accounts/login.html", c, context_instance=RequestContext(request)) else: """ user is not submitting the form, show the login form """ return render_to_response("accounts/login.html", c, context_instance=RequestContext(request))
if request.POST.get("fecha_venc"): d.fecha_venc = request.POST.get("fecha_venc") if request.POST.get("categoria_nombre"): d.categoria, is_created = Categoria.objects.get_or_create( nombre=request.POST.get("categoria_nombre"), ) if Producto.objects.filter(codigo=d.codigo).exclude( id=d.id, headquar_id=d.headquar_id).count() > 0: raise Exception("El producto <b>%s</b> ya existe " % d.codigo) d.save() # raise Exception( "El producto <b>%s</b> ya existe " % d.codigo ) if d.id: Message.info(request, ( "Producto <b>%(name)s</b> ha sido actualizado correctamente." ) % {"name": d.codigo}, True) return Redirect.to_action(request, "index") except Exception, e: transaction.savepoint_rollback(sid) Message.error(request, e) categoria_nombre_list = [] try: categoria_nombre_list = json.dumps( list(col["nombre"] + "" for col in Categoria.objects.values( "nombre").filter().order_by("nombre"))) print "PV=%s" % d.precio_venta except Exception, e: Message.error(request, e) c = {