def testFirst(self): first_query = self.first_query session.query(Event).delete() session.commit() session.flush() e = Event(self.et) e.title = 'First event' e.time_start = datetime(1970, 1, 1, 0, 0) e.time_end = datetime(1970, 1, 1, 3, 0) e.description = 'First description' e.source_type = EventSourceType.EMPTY session.add(e) session.commit() all_first = first_query.all() self.assertEquals(len(all_first), 1) e = all_first[0] self.assertEquals(e.title, 'First event'); self.assertEquals(e.time_start, datetime(1970, 1, 1, 0, 0)) self.assertEquals(e.time_end, datetime(1970, 1, 1, 3, 0)) self.assertEquals(e.description, 'First description') self.assertEquals(e.source_type, EventSourceType.EMPTY) self.assertEquals(e.event_type.name, 'first') all_date_empty = session.query(Event).filter(Event.time_start == date(1971, 1, 1)).all() self.assertEquals(len(all_date_empty), 0) all_date_fine = session.query(Event).filter(Event.time_start == date(1970, 1, 1)).all() self.assertEquals(len(all_date_fine), 1) session.delete(e) session.commit()
def testFirst(self): first_query = self.first_query session.query(Event).delete() session.commit() session.flush() e = Event(self.et) e.title = 'First event' e.time_start = datetime(1970, 1, 1, 0, 0) e.time_end = datetime(1970, 1, 1, 3, 0) e.description = 'First description' e.source_type = EventSourceType.EMPTY session.add(e) session.commit() all_first = first_query.all() self.assertEquals(len(all_first), 1) e = all_first[0] self.assertEquals(e.title, 'First event') self.assertEquals(e.time_start, datetime(1970, 1, 1, 0, 0)) self.assertEquals(e.time_end, datetime(1970, 1, 1, 3, 0)) self.assertEquals(e.description, 'First description') self.assertEquals(e.source_type, EventSourceType.EMPTY) self.assertEquals(e.event_type.name, 'first') all_date_empty = session.query(Event).filter( Event.time_start == date(1971, 1, 1)).all() self.assertEquals(len(all_date_empty), 0) all_date_fine = session.query(Event).filter( Event.time_start == date(1970, 1, 1)).all() self.assertEquals(len(all_date_fine), 1) session.delete(e) session.commit()
def testSecond(self): session.query(Event).delete() session.query(Person).delete() session.query(Place).delete() session.commit() place = Place('First place') place.address = 'Address' place.phone = 'Phone' place.site_url = 'http://localhost' persons_list = [] persons_list.append(Person('First', Person.MUSICIAN)) persons_list.append(Person('Second', Person.MUSICIAN)) e = Event(self.et) e.place = place for p in persons_list: e.persons.append(p) session.add(e) session.commit() session.flush() first_query = self.first_query all_first = first_query.all() self.assertEquals(len(all_first), 1) e = all_first[0] place = e.place self.assertEquals(place.address, 'Address') self.assertEquals(place.phone, 'Phone') self.assertEquals(place.site_url, 'http://localhost') person_names = [] for p in e.persons: person_names.append(p.name) self.assert_('First' in person_names) self.assert_('Second' in person_names) e = Event(EventType.findByName(session, 'live'), 'Live event') e.addEventStatus(EventStatus(EventStatus.LIVE_WANT)) e.addEventStatus(EventStatus(EventStatus.LIVE_BE_HERE)) session.add(e) session.commit() session.flush() events = self.live_query.all() self.assertEquals(len(events), 1) e = events[0] self.assertEquals(len(e.event_status_list), 2) self.assertEquals(e.last_status, EventStatus.LIVE_BE_HERE)
def testSecond(self): session.query(Event).delete() session.query(Person).delete() session.query(Place).delete() session.commit() place = Place('First place') place.address = 'Address' place.phone = 'Phone' place.site_url = 'http://localhost'; persons_list = [] persons_list.append(Person('First', Person.MUSICIAN)) persons_list.append(Person('Second', Person.MUSICIAN)) e = Event(self.et) e.place = place for p in persons_list: e.persons.append(p) session.add(e) session.commit() session.flush() first_query = self.first_query all_first = first_query.all() self.assertEquals(len(all_first), 1) e = all_first[0] place = e.place self.assertEquals(place.address, 'Address') self.assertEquals(place.phone, 'Phone') self.assertEquals(place.site_url, 'http://localhost') person_names = [] for p in e.persons: person_names.append(p.name) self.assert_('First' in person_names) self.assert_('Second' in person_names) e = Event(EventType.findByName(session, 'live'), 'Live event') e.addEventStatus(EventStatus(EventStatus.LIVE_WANT)) e.addEventStatus(EventStatus(EventStatus.LIVE_BE_HERE)) session.add(e) session.commit() session.flush() events = self.live_query.all() self.assertEquals(len(events), 1); e = events[0] self.assertEquals(len(e.event_status_list), 2) self.assertEquals(e.last_status, EventStatus.LIVE_BE_HERE)
def do_POST(self): try: output = header ctype, pdict = cgi.parse_header(self.headers.getheader("content-type")) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) restaurant = fields.get("new_rest") edit = fields.get("edit") if restaurant: self.send_response(301) self.end_headers() restaurant_new = Restaurant(name = restaurant[0]) session.add(restaurant_new) session.commit() text = """ <h2>New Restaurant was created!</h2> <p>Restaurant <strong>%s</strong> is added to restaurants</p> <a href = "/restaurants/new"><p>Back</p></a> <a href = "/restaurants"><p>To All restaurants</p></a> """ % restaurant[0] output += text output += footer self.wfile.write(output) elif edit: self.send_response(301) self.end_headers() rest_id = int(self.path.split("/")[-2]) new_name = edit[0] old_rest = session.query(Restaurant).filter(Restaurant.id == rest_id) old_name = old_rest[0].name old_rest[0].name = new_name session.add(old_rest[0]) session.commit() text = """ <h2>You have changed the name of %s to %s.</h2> <h3>Congrats!</h3> <a href = "/restaurants"><p>To All restaurants</p></a> """ % (old_name, new_name) output += text output += footer self.wfile.write(output) elif "delete" in fields.keys()[0]: del_command = fields.keys()[0] print "del del_command: ", del_command del_id = int(del_command.split("_")[1]) delete_restaurant = session.query(Restaurant).filter(Restaurant.id == del_id).one() session.delete(delete_restaurant) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() except Exception, e: print "ERROR! ", e pass
def do_GET(self): try: output = header if self.path.endswith("/restaurants"): self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() restaurants = session.query(Restaurant).all() for i in restaurants: output += "<h2>" output += i.name output += "</h2><a href='restaurants/%s/edit'>Edit</a><br><a href='/%s/delete'>Delete</a>" % (i.id, i.id) output += "<a href = '/restaurants/new'><h2>Make a new Restaurant Here</h2></a>" elif self.path.endswith("/restaurants/new"): self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() output += "<h2>Make a New Restaurant</h2>" form = """ <form method = "POST" enctype = 'multipart/form-data' action = '/restaurants/new'> <input name = 'new_rest' type = 'text' placeholder = 'New Restaurant Name'> <input type = 'submit' value = 'Create'> </form> """ output += form elif self.path.endswith("/edit"): self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() rest_id = int(self.path.split("/")[-2]) rename_restaurant = session.query(Restaurant).filter(Restaurant.id == rest_id) output += "<h2>Rename the <strong>%s</strong></h2>" % rename_restaurant[0].name form = """ <form method = "POST" enctype = 'multipart/form-data' action = '/restaurants/%s/edit'> <input name = 'edit' type = 'text'> <input type = 'submit' value = 'Rename'> </form> """ % rest_id output += form elif self.path.endswith("/delete"): self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() rest_id = int(self.path.split("/")[-2]) delete_restaurant = session.query(Restaurant).filter(Restaurant.id == rest_id) output += "<h2>You're going to delete Restaurant <strong>%s</strong>. Are you sure?</h2>" % delete_restaurant[0].name form = """ <form method = "POST" enctype = 'multipart/form-data' action = '/delete'> <input name = 'delete_%s' type = 'submit' value = 'Delete'> </form> """ % rest_id output += form # add footer and write file output += footer self.wfile.write(output) return except IOError: self.send_error(404, 'File not found: %s', self.path)
# -*- coding: utf-8 -*- from sqlalchemy import * from model import * from conn import engine, session from datetime import datetime engine.echo = True; metadata.drop_all(engine) metadata.create_all(engine, checkfirst=False) session.query(EventType).delete() session.add(EventType('live', u'Концерт')) it = ImageType(Entity.EVENT, u'Афиша события'); it.max_thumb_width = 126; it.max_thumb_height = 126; it.transform_type = ImageTransform.STD; it.base_dir = 'event'; session.add(it) for e in session.query(EventType): print e event_type = e e = Event(event_type, 'First') e.time_start = datetime.now() session.add(e) session.commit()
def do_POST(self): try: output = header ctype, pdict = cgi.parse_header( self.headers.getheader("content-type")) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) restaurant = fields.get("new_rest") edit = fields.get("edit") if restaurant: self.send_response(301) self.end_headers() restaurant_new = Restaurant(name=restaurant[0]) session.add(restaurant_new) session.commit() text = """ <h2>New Restaurant was created!</h2> <p>Restaurant <strong>%s</strong> is added to restaurants</p> <a href = "/restaurants/new"><p>Back</p></a> <a href = "/restaurants"><p>To All restaurants</p></a> """ % restaurant[0] output += text output += footer self.wfile.write(output) elif edit: self.send_response(301) self.end_headers() rest_id = int(self.path.split("/")[-2]) new_name = edit[0] old_rest = session.query(Restaurant).filter( Restaurant.id == rest_id) old_name = old_rest[0].name old_rest[0].name = new_name session.add(old_rest[0]) session.commit() text = """ <h2>You have changed the name of %s to %s.</h2> <h3>Congrats!</h3> <a href = "/restaurants"><p>To All restaurants</p></a> """ % (old_name, new_name) output += text output += footer self.wfile.write(output) elif "delete" in fields.keys()[0]: del_command = fields.keys()[0] print "del del_command: ", del_command del_id = int(del_command.split("_")[1]) delete_restaurant = session.query(Restaurant).filter( Restaurant.id == del_id).one() session.delete(delete_restaurant) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() except Exception, e: print "ERROR! ", e pass
def do_GET(self): try: output = header if self.path.endswith("/restaurants"): self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() restaurants = session.query(Restaurant).all() for i in restaurants: output += "<h2>" output += i.name output += "</h2><a href='restaurants/%s/edit'>Edit</a><br><a href='/%s/delete'>Delete</a>" % ( i.id, i.id) output += "<a href = '/restaurants/new'><h2>Make a new Restaurant Here</h2></a>" elif self.path.endswith("/restaurants/new"): self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() output += "<h2>Make a New Restaurant</h2>" form = """ <form method = "POST" enctype = 'multipart/form-data' action = '/restaurants/new'> <input name = 'new_rest' type = 'text' placeholder = 'New Restaurant Name'> <input type = 'submit' value = 'Create'> </form> """ output += form elif self.path.endswith("/edit"): self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() rest_id = int(self.path.split("/")[-2]) rename_restaurant = session.query(Restaurant).filter( Restaurant.id == rest_id) output += "<h2>Rename the <strong>%s</strong></h2>" % rename_restaurant[ 0].name form = """ <form method = "POST" enctype = 'multipart/form-data' action = '/restaurants/%s/edit'> <input name = 'edit' type = 'text'> <input type = 'submit' value = 'Rename'> </form> """ % rest_id output += form elif self.path.endswith("/delete"): self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() rest_id = int(self.path.split("/")[-2]) delete_restaurant = session.query(Restaurant).filter( Restaurant.id == rest_id) output += "<h2>You're going to delete Restaurant <strong>%s</strong>. Are you sure?</h2>" % delete_restaurant[ 0].name form = """ <form method = "POST" enctype = 'multipart/form-data' action = '/delete'> <input name = 'delete_%s' type = 'submit' value = 'Delete'> </form> """ % rest_id output += form # add footer and write file output += footer self.wfile.write(output) return except IOError: self.send_error(404, 'File not found: %s', self.path)
def __byEventTypeQuery(self, event_type_name): et_alias = aliased(EventType) return session.query(Event).\ join(et_alias, Event.event_type).\ filter(et_alias.name == event_type_name).\ group_by(Event.event_id)
def setUp(self): session.query(EventType).filter(EventType.name == 'first') session.commit() self.et = EventType('first', 'First') session.add(self.et) session.commit()