예제 #1
0
def add_cigarette():
    created_date = request.json.get('createdDate', None)
    if created_date is None:
        created_date = datetime.datetime.utcnow()
    else:
        # timezone is the result of getTimezoneOffset() from javascript so it's UTC - Local
        # For me right now that is 300 minutes so we want to add it back on to get UTC time
        tz_offset = request.json.get('timezone', 0)
        created_date = datetime.datetime.strptime(created_date, "%Y-%m-%d %H:%M:%S")
        created_date = created_date + datetime.timedelta(minutes=tz_offset)

    latitude = float(request.json.get('latitude', 0.0))
    longitude = float(request.json.get('longitude', 0.0))
    if latitude == 0.0 or longitude == 0.0:
        return Response('Latitude and longitude are required', 400)

    habitdb = get_habitdb()
    habitdb.cigarettes.insert_one({
        "CreatedDate": created_date,
        "Latitude": latitude,
        "Longitude": longitude,
        "Username": session['username']
    })

    return jsonify({
        'createdDate': created_date,
        'latitude': latitude,
        'longitude': longitude
    })
예제 #2
0
def get_cigarettes_by_date(date):
    habitdb = get_habitdb()
    # TODO take timezone as parameter
    central = timezone('US/Central')
    date_start = datetime.datetime.strptime(date, "%Y-%m-%d")
    date_start_local = central.localize(datetime.datetime(date_start.year, date_start.month, date_start.day))
    date_end_local = date_start_local + datetime.timedelta(days=1)
    cigarettes = habitdb["cigarettes"].find({
        "Username": session['username'],
        "CreatedDate": {"$gt": date_start_local, "$lt": date_end_local}
    }).sort([("CreatedDate", pymongo.DESCENDING)])
    return json_util.dumps(cigarettes)
예제 #3
0
def get_cigarette_totals():
    habitdb = get_habitdb()
    limit = int(request.args.get('limit', 10))
    skip = int(request.args.get('skip', 0))
    tzoffset = int(request.args.get('timezone', 0))
    cigarettes = habitdb["cigarettes"].aggregate([
        {"$match": {"Username": session['username']}},
        {"$project": {"CreatedDate": {"$add": ["$CreatedDate", {"$multiply": [tzoffset * -1, 60, 1000]}]}}},
        {"$project": {
            "CreatedDate": 1,
            "h": {"$hour": "$CreatedDate"},
            "m": {"$minute": "$CreatedDate"},
            "s": {"$second": "$CreatedDate"},
            "ml": {"$millisecond": "$CreatedDate"}
        }},
        {"$project": {
            "CreatedDate": {
                "$subtract": [
                    "$CreatedDate",
                    {
                        "$add": [
                            "$ml",
                            {"$multiply": ["$s", 1000]},
                            {"$multiply": ["$m", 60, 1000]},
                            {"$multiply": ["$h", 60, 60, 1000]}
                        ]
                    }
                ]
            }
        }},
        {"$group": {"_id": "$CreatedDate", "count": {"$sum": 1}}},
        {"$sort": SON([("_id", -1)])},
        {"$skip": skip},
        {"$limit": limit},
        {"$project": {
            "CreatedDate": {
                "$add": ["$_id", {"$multiply": [tzoffset, 60, 1000]}]
            },
            "Count": "$count"}
        }
    ])
    return json_util.dumps(cigarettes)
예제 #4
0
def get_cigarettes_summary():
    central = timezone('US/Central')
    today = datetime.date.today()
    current_date = central.localize(datetime.datetime(today.year, today.month, today.day))
    yesterday = current_date - datetime.timedelta(days=1)
    habitdb = get_habitdb()
    last_cigarette = datetime.datetime(2016, 1, 1)
    cigarettes = habitdb["cigarettes"].find({"Username": session['username']}).sort([("CreatedDate", pymongo.DESCENDING)])
    if cigarettes.count() > 0 and cigarettes[0] is not None:
        last_cigarette = cigarettes[0]["CreatedDate"]
    cigarettes_today = habitdb["cigarettes"].find({"Username": session['username'], "CreatedDate": {"$gt": current_date}}).count()
    cigarettes_yesterday = habitdb["cigarettes"].find({"Username": session['username'], "CreatedDate": {"$gt": yesterday, "$lt": current_date}}).count()
    # TODO get delta from some kind of settings or at least configuration
    target_time = last_cigarette + datetime.timedelta(minutes=105)
    return jsonify({
        'summary': {
            'currentDate': current_date,
            'lastCigarette': last_cigarette,
            'cigarettesToday': cigarettes_today,
            'cigarettesYesterday': cigarettes_yesterday,
            'targetTime': target_time
        }
    })
예제 #5
0
from data.habitdb import get_habitdb

habitdb = get_habitdb()
cigarettes = habitdb["cigarettes"].update_many({"Username": None}, {"$set": {"Username": "******"}})
예제 #6
0
def get_cigarettes():
    habitdb = get_habitdb()
    cigarettes = habitdb["cigarettes"].find({"Username": session['username']}).sort([("CreatedDate", pymongo.DESCENDING)])
    return json_util.dumps(cigarettes)