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 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_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 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 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 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 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 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 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 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 producto_edit(request, key): """ Actualiza Producto """ id = SecurityKey.is_valid_key(request, key, "producto_upd") if not id: return Redirect.to_action(request, "index") d = None try: d = get_object_or_404(Producto, id=id) try: categoria = Categoria.objects.get(id=d.categoria.id) if categoria.id: d.categoria_nombre = d.categoria.nombre except: pass except Exception, e: Message.error(request, ("Usuario no se encuentra en la base de datos. %s" % 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 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 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 locality_add_form(request): form = LocalityAddForm() if request.method == "POST": form = LocalityAddForm(request.POST) if form.is_valid(): #name = form.cleaned_data['name'] #location = form.cleaned_data['location'] #locality_type = form.cleaned_data['locality_type'].id #p = Locality() #p.name = name #p.location = location #p.locality_type_id = locality_type #p.save() form.save() return Redirect.to_action(request, "index") c = { "page_module":_("Locality"), "page_title":_("New locality."), "form":form, #"locality_type_list":locality_type_list, } return render_to_response('params/locality/add_form.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 = { "page_module":("Gestión de productos"), "page_title":("Actualizar producto."), "d":d, "categoria_nombre_list":categoria_nombre_list,