Example #1
#! /usr/bin/env python2

from collections import OrderedDict
from flask import Flask, request, jsonify, json
import re
import argparse

app = Flask(__name__, static_url_path="/static")

# the database :)
app.store = dict()

# TODOS API: get / upsert / delete

# load all elements
@app.route("/api/todos", methods=["GET"])
def get_todos():
    ordered = sorted(app.store.values(), key=lambda v: v["order"])
    return json.dumps(ordered)

# updates an element or insert it if it doesn't exists
@app.route("/api/todos/<string:id>", methods=["PUT"])
def upsert_todo(id):
    # leaved here for readability, for performance compile outside this function
    regex_uuid = re.compile("[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}\Z", re.I)
    if len(id) < 1 and not regex_uuid.match(id):
        return jsonify(result="fail", reason="invalid request: id")
Example #2
from flask import Flask, jsonify

import os
import time
from xml.dom.minidom import parse

import datastore

app = Flask(__name__)
app.debug = True
app.store = datastore.DataStore()

def year_data(year):
    data = app.store.get_year(year)
    return jsonify(data)

def year_month_data(year, month):
    data = app.store.get_month(year, month)
    return jsonify(data)

def year_week_data(year, week):
    data = app.store.get_week(year, week)
    return jsonify(data)