def pre_run(): Base.metadata.drop_all(bind=db.engine) Base.metadata.create_all(bind=db.engine) responsable1 = Responsable(nombre='Arturo', telefono='7777777', tipo='Persona') responsable2 = Responsable(nombre='Julio', telefono='2255555', tipo='Persona') responsable3 = Responsable(nombre='Ventas', telefono='3334444', tipo='Area') responsable4 = Responsable(nombre='Maria', telefono='6666661', tipo='Persona') recurso1 = Recurso(serial='ubnTY78', tipo='tecnológico', marca='Samsung', proveedor='electronicos SA', valor_comercial=300000, fecha_compra=datetime(2018, 3, 22), estado='Bueno') recurso2 = Recurso(serial='lldp38998', tipo='cafeteria', marca='xx', proveedor='cafe SA', valor_comercial=10000, fecha_compra=datetime(2019, 1, 2), estado='Malo') recurso3 = Recurso(serial='s89dg7', tipo='oficina', marca='yy', proveedor='OFF SA', valor_comercial=23000, fecha_compra=datetime(2019, 3, 10), estado='Nuevo') responsable1.add_recurso(recurso1) responsable2.add_recurso(recurso1) responsable4.add_recurso(recurso2) responsable3.add_recurso(recurso3) responsable1.add_recurso(recurso3) try: db.session.add_all( [responsable1, responsable2, responsable3, responsable4]) db.session.add_all([recurso1, recurso2, recurso3]) db.session.commit() except Exception as e: db.session.rollback() print('Error:', e)
def rsponsable_actualizar(): try: form = json.loads(request.data) except json.JSONDecodeError: return json.dumps({ 'success': False, 'error': 'Formato de JSON incorrecto.' }), 500 try: responsable = Responsable.get_by_id(db.session, form['id']) except KeyError: responsable = None if responsable is None: return json.dumps({ 'success': False, 'error': 'Responsable no encontrado.' }), 500 try: responsable.nombre = form['nombre'] except: json.dumps({'success': False, 'error': 'Error de nombre.'}), 500 try: responsable.telefono = form['telefono'] except: pass try: responsable.tipo = form['tipo'] except: pass try: responsable.remove_recurso() for r_id in form['recursos']: rec = Recurso.get_by_id(db.session, r_id) if rec is not None: responsable.add_recurso(rec) except: pass try: db.session.commit() except: db.session.rollback() return jsonify({'success': False, 'error': 'Error guardando.'}), 500 return json.dumps({ 'success': True, 'message': 'Responsable actualizado.' }), 200
def recurso_eliminar(recurso_id): try: recurso = Recurso.get_by_id(db.session, recurso_id) if recurso is None: return json.dumps ( {'success': False, 'error': 'Recurso no encontrado.'} ), 500 , \ {'ContentType': 'application/json'} db.session.delete(recurso) db.session.commit() except: db.session.rollback() return json.dumps ( {'success': False, 'error': 'Error eliminando recurso.'} ), 500 , \ {'ContentType': 'application/json'} return json.dumps ( {'success': True} ), 200 , \ {'ContentType': 'application/json'}
def responsable_agregar_recurso(): try: form = json.loads(request.data) except json.JSONDecodeError: return json.dumps({ 'success': False, 'error': 'Formato de JSON incorrecto.' }), 500 try: responsable = Responsable.get_by_id(db.session, form['id_responsable']) except: responsable = None if responsable is None: return json.dumps({ 'success': False, 'error': 'Responsable no encontrado.' }), 500 try: recurso = Recurso.get_by_serial(db.session, form['recurso_serial']) except: recurso = None if recurso is None: return json.dumps({ 'success': False, 'error': 'Recurso no encontrado.' }), 500 try: responsable.add_recurso(recurso) except: pass try: db.session.commit() except: db.session.rollback() return jsonify({'success': False, 'error': 'Error guardando.'}), 500 return json.dumps({'success': True, 'message': 'Recurso agregado.'}), 200
def responsable_crear(): try: form = json.loads(request.data) except json.JSONDecodeError: return json.dumps({ 'success': False, 'error': 'Formato de JSON incorrecto.' }), 500 responsable = Responsable() try: responsable.tipo = form['tipo'] except: pass try: responsable.nombre = form['nombre'] except: pass try: responsable.telefono = form['telefono'] except: pass try: for rec_id in form['recursos']: recurso = Recurso.get_by_id(db.session, rec_id) if recurso is not None: responsable.add_recurso(recurso) except: pass try: db.session.add(responsable) db.session.commit() except: db.session.rollback() return json.dumps({'success': False, 'error': 'Error guardando.'}), 500 return json.dumps({ 'success': True, 'message': 'Responsable agregado.' }), 200
def recurso_crear(): try: form = json.loads(request.data) except json.JSONDecodeError: return json.dumps({ 'success': False, 'error': 'Formato de JSON incorrecto.' }), 500 recurso = Recurso() try: recurso.tipo = form['tipo'] except: pass try: recurso.marca = form['marca'] except: pass try: recurso.serial = form['serial'] except: pass try: recurso.proveedor = form['proveedor'] except: pass try: recurso.valor_comercial = int(form['valor_comercial']) except: pass try: recurso.fecha_compra = form['fecha_compra'] except: pass try: recurso.estado = form['estado'] except: pass try: for resp_id in form['responsables']: responsable = Responsable.get_by_id(db.session, resp_id) if responsable is not None: recurso.add_responsable(responsable) except: pass try: db.session.add(recurso) db.session.commit() except: db.session.rollback() return json.dumps({'success': False, 'error': 'Error guardando.'}), 500 return json.dumps({'success': True, 'message': 'Recurso agregado.'}), 200
def recurso_actualizar(): try: form = json.loads(request.data) except json.JSONDecodeError: return json.dumps({ 'success': False, 'error': 'Formato de JSON incorrecto.' }), 500 try: recurso = Recurso.get_by_id(db.session, form['id']) except KeyError: recurso = None if recurso is None: return json.dumps({ 'success': False, 'error': 'Recurso no encontrado.' }), 500 try: recurso.serial = form['serial'] except: json.dumps({'success': False, 'error': 'Error de serial.'}), 500 try: recurso.marca = form['marca'] except: pass try: recurso.proveedor = form['proveedor'] except: pass try: recurso.valor_comercial = form['valor_comercial'] except: pass try: recurso.fecha_compra = form['fecha_compra'] except: pass try: recurso.estado = form['estado'] except: pass try: recurso.remove_responsables() for r_id in form['responsables']: resp = Responsable.get_by_id(db.session, r_id) if resp is not None: recurso.add_responsable(resp) except: pass try: db.session.commit() except: db.session.rollback() return jsonify({'success': False, 'error': 'Error guardando.'}), 500 return json.dumps({ 'success': True, 'message': 'Recurso actualizado.' }), 200