def send_message(format): if not 'message' in request.form.keys(): return "Missing message", 400 else: message = request.form['message'] l = None d = None if 'location_letters' in request.form.keys(): if request.form['location_letters']: try: l = db_unique(Location, letters=request.form['location_letters'], numbers=int(request.form['location_numbers'])) except Exception, e: return str(e), 400 if 'timestamp' in request.form.keys(): if request.form['timestamp']: d = datetime.datetime.fromtimestamp(int(request.form['timestamp'])) try: m = Message(message, l, d) db_session.add(m) db_session.commit() except Exception, e: return str(e), 400
def generate_location_filter(locs): first = True for loc in locs: l = db_unique(Location, letters=loc[0], numbers=loc[1]) local_filter = Message.location == l if first: filter = local_filter first = False else: filter = or_(filter, local_filter) return filter
from euskalmap.database import db_session, db_unique from euskalmap.models import Message, Location from datetime import datetime for m in Message.query.all(): db_session.delete(m) db_session.commit() items = [ Message("This is a test message", db_unique(Location, letters="AK", numbers=91), datetime.now()), Message("Test message without location or timestamp"), Message("This one only has a timestamp.", None, datetime.now()), Message("Location, but no timestamp.", db_unique(Location, letters="AK", numbers=91)) ] for i in items: db_session.add(i) db_session.commit()
def location_messages(format, letters, numbers): l = db_unique(Location, letters=letters, numbers=numbers) return filter_and_output(l.messages, None, format)
def messages_by_location(format, letters, numbers, filter): l = db_unique(Location, letters=letters, numbers=numbers) return get_filtered_messages(format, filter, l.messages)