def create_quote_form(): ip = request.remote_addr type = request.headers['Content-Type'] tags = [] tags_valid = True body_valid = True content = request.form['quote'] tags_raw = request.form['tags'] # a smidgen of validation if len(tags_raw) > 100: tags_valid = False else: tags = map(string.strip, tags_raw.split(',')) body_valid, tags_valid = validate_quote(content, tags) quote = None if body_valid and tags_valid: quote = Quote(content, ip) quote.tags = map(Tag, tags) quote = db.put(quote) # grabbing return val isn't strictly necessary if request.wants_json(): return create_quote_resp_json(quote, body_valid, tags_valid) else: return create_quote_resp_html(quote, body_valid, tags_valid)
def create_quote_form(): ip = request.remote_addr type = request.headers['Content-Type'] tags = [] tags_valid = True body_valid = True content = request.form['quote'] # strip trailing whitespace at each line content_lines = content.splitlines() for i, l in enumerate(content_lines): content_lines[i] = l.rstrip() content = '\n'.join(content_lines) logger.info(content) tags_raw = request.form['tags'] # a smidgen of validation if len(tags_raw) > 100: tags_valid = False else: tags = map(string.strip, tags_raw.split(',')) body_valid, tags_valid = validate_quote(content, tags) quote = None if body_valid and tags_valid: quote = Quote(content, ip) quote.tags = map(Tag, tags) quote = db.put(quote) # grabbing return val isn't strictly necessary if request.wants_json(): return create_quote_resp_json(quote, body_valid, tags_valid) else: return create_quote_resp_html(quote, body_valid, tags_valid)
def create_quote_form(): ip = request.remote_addr type = request.headers['Content-Type'] tags = [] tags_valid = True body_valid = True content = request.form['quote'] # strip trailing whitespace at each line content_lines = content.splitlines() for i,l in enumerate(content_lines): content_lines[i] = l.rstrip() content = '\n'.join(content_lines) logger.info(content) tags_raw = request.form['tags'] # a smidgen of validation if len(tags_raw) > 100: tags_valid = False else: tags = map(string.strip, tags_raw.split(',')) body_valid, tags_valid = validate_quote(content, tags) quote = None if body_valid and tags_valid: quote = Quote(content, ip) quote.tags = map(Tag, tags) quote = db.put(quote) # grabbing return val isn't strictly necessary if request.wants_json(): return create_quote_resp_json(quote, body_valid, tags_valid) else: return create_quote_resp_html(quote, body_valid, tags_valid)
def create_quote_json(): ip = request.remote_addr data = request.json body_valid, tags_valid = validate_quote(data['body'], data['tags']) if body_valid and tags_valid: quote = Quote(data['body'], ip) quote.tags = map(Tag, data['tags']) quote = db.put(quote) # grabbing return val isn't strictly necessary if request.wants_json(): return create_quote_resp_json(quote, body_valid, tags_valid) else: return create_quote_resp_html(quote, body_valid, tags_valid)
def default(self, json_obj): if json_obj['mimetype'] == Quote.json_mimetype: q = Quote() q.id = int(json_obj['id']) q.up_votes = int(json_obj['up']) q.down_votes = int(json_obj['down']) q.body = json_obj['body'] q.tags = [QuoteDecoder.default(self, t) for t in json_obj['tags']] return q elif json_obj['mimetype'] == Tag.json_mimetype: t = Tag() t.id = int(json_obj['id']) t.tag = json_obj['tag'] return tag else: return json.JSONDecoder.default(self, json_obj)
#!/usr/bin/env python # -*- coding: utf-8 -*- import db from data_models import Quote, Tag db = db.SQLQuoteStore() for i in range(100): quote = Quote("this is a quote %s" % (i)) quote.tags = [ Tag("foo"), Tag("bar") ] db.put(quote)
#!/usr/bin/env python # -*- coding: utf-8 -*- import db from data_models import Quote, Tag quote = Quote("this is a quote") quote.tags = [ Tag("foo"), Tag("bar") ] db = db.SQLQuoteStore() db.put(quote) print quote.id quote2 = db.get(quote.id) assert quote == quote2