def initialize(): """Initializes database file""" # Deletes database file if exists. if os.path.isfile("books.db"): os.unlink("books.db") # Initializes Macaron macaron.macaronage("books.db") # Creates tables macaron.execute(SQL_T_TAG) macaron.execute(SQL_T_BOOK) # Initial data tag1 = Tag.create(name="Python") tag1.books.append(title="Learning Python", description="Powerful Object-Oriented Programming", rating=5) tag1.books.append(title="Expert Python Programming", description="Python best practice for experts.", rating=4) tag2 = Tag.create(name="Japanese") tag2.books.append(title="K-ON!", description="Highschool band cartoon.", rating=5) # Commits macaron.bake()
def do_login(): un = request.forms.get('username') pw = request.forms.get('password') h_pw = computeMD5hash(pw) User.create(username=un, password=h_pw) macaron.bake() redirect("/login")
def activarEstacion(self, idEstacionP): try: macaron.macaronage("siprem.db") estacion = models.Estacion.get(idEstacionP) estacion.estado = 1 estacion.save() macaron.bake() return True except Exception, e: print e return False
def almacenarRegistros(self, kernelRegistrosP, estacionId): try: macaron.macaronage("siprem.db") estacionDaoP = models.Estacion.get(estacionId) for registroP in kernelRegistrosP: if registroP.entregarPrecipitacion() != 0 and registroP.entregarPrecipitacion() != "-": estacionDaoP.estacionreg.append( fecha = str(registroP.entregarFecha()), hora = str(registroP.entregarHora()), vprec = registroP.entregarPrecipitacion() ) macaron.bake() except Exception, e: print e
def crearEstacion(self, nombreEstacionP, ubicacionP, fechaOperacionP): response = False try: macaron.macaronage("siprem.db") models.Estacion.create( nombre = nombreEstacionP, ubicacion = ubicacionP, fechaoperacion = fechaOperacionP, estado = 1) macaron.bake() response = True return response except Exception, e: print e return response
def modificarEstacion(self, estacionDaoP, nombreEstacionP, ubicacionP, fechaOperacionP): response = False try: macaron.macaronage("siprem.db") if estacionDaoP.nombre != nombreEstacionP: estacionDaoP.nombre = nombreEstacionP if estacionDaoP.ubicacion != ubicacionP: estacionDaoP.ubicacion = ubicacionP if estacionDaoP.fechaoperacion != fechaOperacionP: estacionDaoP.fechaoperacion = fechaOperacionP estacionDaoP.save() macaron.bake() response = True return response except Exception, e: print e return response
def updatesubserver(): logging.info(request.body.buf) parentip = request.forms.get('parentip') curip = request.forms.get('ip') p_server = getServerByIp(parentip) sub_server = getServerByIp(curip) if p_server: if sub_server == None: sub_server = Server.create(i_level=p_server.i_level + 1, ind_prent=p_server.ind) #sub_server.ind_prent = __formatstr(request.forms.get('mac')) #sub_server.e_time = __formatstr(request.forms.get('e_time')) #sub_server.s_time = __formatstr(request.forms.get('s_time')) #sub_server.m_r = __formatstr(request.forms.get('m_r')) #sub_server.cabinet = __formatstr(request.forms.get('cabinet')) #sub_server.location = __formatstr(request.forms.get('location')) #sub_server.idrac = __formatstr(request.forms.get('idrac')) #sub_server.cable_label = __formatstr(request.forms.get('cable_label')) sub_server.mac = __formatstr(request.forms.get('mac')) sub_server.ip = __formatstr(request.forms.get('ip')) sub_server.cpu = __formatstr(request.forms.get('cpu')) sub_server.hd = __formatstr(request.forms.get('hd')) sub_server.memory = __formatstr(request.forms.get('memory')) sub_server.op_sys = __formatstr(request.forms.get('op_sys')) sub_server.bz_name = __formatstr(request.forms.get('bz_name')) sub_server.in_using = '1' sub_server.config = __formatstr(request.forms.get('config')) #sub_server.pc_type = __formatstr(request.forms.get('pc_type')) #sub_server.pc_code = __formatstr(request.forms.get('pc_code')) sub_server.remark = __formatstr(request.forms.get('remark')) sub_server.user_pw = __formatstr(request.forms.get('user_pw')) sub_server.monitor_url = __formatstr(request.forms.get('monitor_url')) sub_server.save() macaron.bake() logging.info("ok:" + request.body.buf) else: logging.error("error:" + request.body.buf) return '{"status":0}'
def newt(): new_team = TeamCla.create(name="Houkago Tea Time") member1 = new_team.members.append(first_name="Ritsu", last_name="Tainaka", part="Dr", age=15) member2 = new_team.members.append(first_name="Mio", last_name="Akiyama", part="Ba", age=17) macaron.bake() html = "<html>\n<head><title>Teams</title></head>\n" html += "<body>\n<h1>Teams</h1>\n<ul>\n" for team in TeamCla.all(): html += '<li><a href="/member/%s">%s</a></li>\n' % (team.id, team.name) html += "</ul>\n</body>\n</html>\n" return html
def update_server(): s = bottle.request.environ.get('beaker.session') if not s or not s.has_key('user_id') or s['user_id'] == 0: redirect('/login', code=302) ind = request.forms.get('ind') ind_prent = request.forms.get('ind_prent') i_level = request.forms.get('i_level') if ind == '0': # new record new_server = Server.create(i_level=i_level, ind_prent=ind_prent) else: new_server = Server.get(ind) new_server.e_time = __formatstr(request.forms.get('e_time')) new_server.s_time = __formatstr(request.forms.get('s_time')) new_server.m_r = __formatstr(request.forms.get('m_r')) new_server.cabinet = __formatstr(request.forms.get('cabinet')) new_server.location = __formatstr(request.forms.get('location')) new_server.idrac = __formatstr(request.forms.get('idrac')) new_server.cable_label = __formatstr(request.forms.get('cable_label')) new_server.mac = __formatstr(request.forms.get('mac')) new_server.ip = __formatstr(request.forms.get('ip')) new_server.cpu = __formatstr(request.forms.get('cpu')) new_server.hd = __formatstr(request.forms.get('hd')) new_server.memory = __formatstr(request.forms.get('memory')) new_server.op_sys = __formatstr(request.forms.get('op_sys')) new_server.bz_name = __formatstr(request.forms.get('bz_name')) new_server.in_using = __formatstr(request.forms.get('in_using')) new_server.config = __formatstr(request.forms.get('config')) new_server.pc_type = __formatstr(request.forms.get('pc_type')) new_server.pc_code = __formatstr(request.forms.get('pc_code')) new_server.remark = __formatstr(request.forms.get('remark')) new_server.user_pw = __formatstr(request.forms.get('user_pw')) new_server.monitor_url = __formatstr(request.forms.get('monitor_url')) new_server.save() macaron.bake() redirect('/serverok/%s' % new_server.ind, code=302)
def almacenarEventos(self, coleccionP, estacionId): try: macaron.macaronage("siprem.db") estacionDaoP = models.Estacion.get(estacionId) for eventoP in coleccionP: vectorMagnitudes = eventoP.entregarVectorMagnitudes() vectorSerializado = self.serializarObjeto(vectorMagnitudes) vectorBinario = self.conversorBinario(vectorSerializado) estacionDaoP.eventos.append( fecha= str(eventoP.entregarFecha()), horainicio= str(eventoP.entregarHoraInicio()), horafin= str(eventoP.entregarHoraFin()), magnitud= eventoP.entregarMagnitud(), duracion= eventoP.entregarDuracion(), intmedia= eventoP.entregarIntensidadMedia(), intmaxima= eventoP.entregarIntensidadMaxima(), tipoprec= eventoP.entregarTipoLluvia(), observ= eventoP.entregarObservaciones(), jorprec= eventoP.entregarJornadaEvento(), vmagn= vectorBinario ) macaron.bake() except Exception, e: print e
def tearDown(self): macaron.bake() macaron.cleanup()
def tearDown(self): macaron.bake()
def testCRUD(self): # create team name = "Houkago Tea Time" team = Team.create(name=name) self.assertEqual(str(team), "<Team '%s'>" % name) # create members for idx, n in enumerate(self.names): member = Member.create(team_id=team.pk, first_name=n[0], last_name=n[1], part=n[2]) self.assertEqual(str(member), "<Member '%s'>" % n[3]) self.assertEqual(member.id, idx + 1) # get member with id ritsu = Member.get(1) self.assertEqual(str(ritsu), "<Member 'Ritsu Tainaka : Dr'>") # get team the member Ritsu belongs to is Houkago Tea Time team = member.team self.assertEqual(str(team), "<Team 'Houkago Tea Time'>") # get members with iterator for idx, m in enumerate(team.members): self.assertEqual(str(m), "<Member '%s'>" % self.names[idx][3]) macaron.bake() # Yui changes instrument to castanets yui = Member.get("first_name=? AND last_name=?", ["Yui", "Hirasawa"]) yui.part = "Castanets" yui.save() # re-fetch Yui member = Member.get(3) self.assertEqual(member.part, "Castanets") # Delete all members self.assertEqual(team.members.count(), 4) team.members.select("first_name=?", ["Ritsu"]).delete() self.assertEqual(team.members.count(), 3) team.members.delete() self.assertEqual(team.members.count(), 0) # cancel the changes macaron.rollback() # Add another member 'Sawako' as Gt1 team = Team.get(1) Member.create(team_id=team.pk, first_name="Sawako", last_name="Yamanaka", part="Gt1") # re-fetch Sawako with index sawako = team.members[4] self.assertEqual(str(sawako), "<Member 'Sawako Yamanaka : Gt1'>") # but Sawako is not a member of the team sawako.delete() # Add another member Azusa through reverse relation of ManyToOne team.members.append(first_name="Azusa", last_name="Nakano", part="Gt2") azu = Member.get("first_name=? AND last_name=?", ["Azusa", "Nakano"]) self.assertEqual(str(azu), "<Member 'Azusa Nakano : Gt2'>") # Okay, Yui changes part to Gt1 yui = Member.get("first_name=? AND last_name=?", ["Yui", "Hirasawa"]) yui.part = "Gt1" yui.save() # At last, there are five menbers nm = self.names[:] nm[2] = ("Yui", "Hirasawa", "Gt1", "Yui Hirasawa : Gt1") nm.append(("Azusa", "Nakano", "Gt2", "Azusa Nakano : Gt2")) for idx, m in enumerate(team.members): self.assertEqual(str(m), "<Member '%s'>" % nm[idx][3])
def tearDown(self): macaron.SQL_TRACE_OUT = None macaron.bake() macaron.cleanup()
def testCRUDObject(self): # Test for creating, reading, updating, deleteing # Create team name = "Houkago Tea Time" team = Team.create(name=name) self.assertEqual(str(team), "<Team '%s'>" % name) self.assertEqual(team.id, 1) # Create members for idx, n in enumerate(self.names): member = Member.create(band=team, first_name=n[0], last_name=n[1], part=n[2]) self.assertEqual(str(member), "<Member '%s'>" % n[3]) self.assertEqual(member.id, idx + 1) # Get member with primary key ritsu = Member.get(1) self.assertEqual(str(ritsu), "<Member 'Ritsu Tainaka : Dr'>") # Get team the member Ritsu belongs to is Houkago Tea Time team = ritsu.band self.assertEqual(str(team), "<Team 'Houkago Tea Time'>") # Get members with iterator for idx, m in enumerate(team.members): self.assertEqual(str(m), "<Member '%s'>" % self.names[idx][3]) # Yui changes instrument to castanets macaron.bake() # Commit before changes yui = Member.get(first_name="Yui", last_name="Hirasawa") self.assert_(yui) yui.part = "Castanets" self.assertEqual(yui.part, "Castanets") yui.save() # Re-fetch Yui member = Member.get(3) self.assertEqual(member.part, "Castanets") # Delete all members self.assertEqual(team.members.count(), 4) team.members.select(first_name="Ritsu").delete() self.assertEqual(team.members.count(), 3) team.members.delete() self.assertEqual(team.members.count(), 0) # Test for rollback # Cancel the changes macaron.rollback() team = Team.get(1) self.assertEqual(team.members.count(), 4) # Add another member 'Sawako' as Gt1 member = Member.create(band=team, first_name="Sawako", last_name="Yamanaka", part="Gt1") self.assertEqual(str(member), "<Member 'Sawako Yamanaka : Gt1'>") # Re-fetch Sawako with index sawako = team.members[4] self.assertEqual(str(sawako), "<Member 'Sawako Yamanaka : Gt1'>") # But, Sawako is not a member of the team sawako.delete() # Add another member Azusa through reverse relation of ManyToOne team.members.append(first_name="Azusa", last_name="Nakano", part="Gt2") azu = Member.get(first_name="Azusa") self.assertEqual(str(azu), "<Member 'Azusa Nakano : Gt2'>") # Okay, Yui changes part to Gt1 yui = Member.get(first_name="Yui") yui.part = "Gt1" yui.save() self.assertEqual(yui.part, "Gt1") # At last, there are five menbers nm = self.names[:] nm[2] = ("Yui", "Hirasawa", "Gt1", "Yui Hirasawa : Gt1") nm.append(("Azusa", "Nakano", "Gt2", "Azusa Nakano : Gt2")) for idx, m in enumerate(team.members): self.assertEqual(str(m), "<Member '%s'>" % nm[idx][3]) # Foreign key constraint works on sqlite3 >= 3.6.19 ver = macaron.sqlite_version_info if ver >= (3, 6, 19): # Test for ON CASCADE team = Team.get(1) team.id = 2 team.save() cnt = 0 for member in Member.all(): cnt += 1 self.assertEqual(member.band_id, 2) self.assertEqual(cnt, Member.all().count()) # Test for ON DELETE team.delete() cnt = 0 for member in Member.all(): self.assertEqual(member.band, None) cnt += 1 self.assertEqual(cnt, Member.all().count()) else: msg = "Foreign key constraint works on SQLite(3.6.19) > Current(%s). Skip." warnings.warn(msg % ".".join([str(x) for x in ver]))