Beispiel #1
0
    def summerize(metadata_arr, date):
        def summerize_task_item(summary, task, metadata_date, key, val):
            if val not in summary[key]:
                summary[key][val] = {
                    "day": 0,
                    "week": 0,
                    "month": 0,
                    "dayTimes": 0,
                }

            if date_helpers.is_same_week(date, metadata_date):
                summary[key][val]["week"] += task["duration"]

            if date_helpers.is_same_day(date, metadata_date):
                summary[key][val]["day"] += task["duration"]
                summary[key][val]["dayTimes"] += 1

            summary[key][val]["month"] += task["duration"]

        summary = {'tags': {}, 'people': {}, 'locations': {}}
        for metadata in metadata_arr:
            if "tasks" not in metadata:
                continue
            metadata_date = date_helpers.parse_date_str(metadata["date"])

            for task in metadata["tasks"]:
                if task["duration"] is None:
                    continue
                for tag in task["tags"]:
                    summerize_task_item(summary, task, metadata_date, 'tags', tag)
                for person in task["people"]:
                    summerize_task_item(summary, task, metadata_date, 'people', person)
                for location in task["locations"]:
                    summerize_task_item(summary, task, metadata_date, 'locations', location)
        return summary
Beispiel #2
0
def process_user(dropbox_user_id):
    # OAuth token for the user

    user = models.user.find_user_by_dropbox_id(str(dropbox_user_id))
    if not user:
        app.logger.error("User {0} was not found".format(dropbox_user_id))
        return

    cursor = None
    if "hook_cursor" in user:
        cursor = user["hook_cursor"]

    app.logger.info("Processing update for user {0}".format(user["email"]))

    dropbox = DropboxApi()
    results, cursor = dropbox.get_files_in_folder("/journal", user["dropbox_access_token"], cursor)
    for result in results:
        app.logger.info("processing changed file {0}, with name: {1}".format(result["path_lower"], result["name"]))
        response = dropbox.get_file_content(user["dropbox_access_token"], result["path_lower"])
        content = response["content"]
        date_str = result["name"][:-3]
        filename = result["name"]
        try:
            date = date_helpers.parse_date_str(date_str)
        except ValueError:
            continue
    entry_metadata = TasksParser(date_str, content).to_dict()
    models.entries.create_or_update_entry(user["_id"], filename, date, entry_metadata, dict_without(response, "content"))

    app.logger.info("Finished processing update for user {0}".format(user["email"]))
    models.user.save_hook_cursor(user["_id"], cursor)
Beispiel #3
0
def addEntry():
    line = request.json["line"]
    date_str = request.json["date"]
    is_after_midnight = request.json["is_after_midnight"] == "true"
    date = date_helpers.parse_date_str(date_str)
    perform_update(date_str, date, "add_line", {"line": line, "is_after_midnight": is_after_midnight})
    entries = models.entries.find_for_user(g.user["_id"], {"date": date})
    return jsonify(entries=entries)
Beispiel #4
0
def getMetadata():
    date_str = request.args.get('date')
    if not date_str:
        return "missing date", 400
    date = date_helpers.parse_date_str(date_str)
    entries = models.entries.find_for_user_and_month(g.user["_id"], date)
    entry_metadata_arr = [entry["metadata"] for entry in entries]
    summary = TasksParser.summerize(entry_metadata_arr, date)
    return jsonify(metadata=entry_metadata_arr, summary=summary)
Beispiel #5
0
def getMetadata():
    date_str = request.args.get('date')
    if not date_str:
        return "missing date", 400
    date = date_helpers.parse_date_str(date_str)
    entries = models.entries.find_for_user_and_month(g.user["_id"], date)
    entry_metadata_arr = [entry["metadata"] for entry in entries]
    summary = TasksParser.summerize(entry_metadata_arr, date)
    return jsonify(metadata=entry_metadata_arr, summary=summary)
Beispiel #6
0
def get_goals():
    """ get list of goals """
    date_str = request.args.get('date')
    if not date_str:
        return "missing date", 400
    date = date_helpers.parse_date_str(date_str)
    goals = models.goals.get_goals(g.user["_id"], date)
    if goals:
        goals = goals["metadata"]
    return jsonify(goals=goals)
Beispiel #7
0
def get_goals():
    """ get list of goals """
    date_str = request.args.get('date')
    if not date_str:
        return "missing date", 400
    date = date_helpers.parse_date_str(date_str)
    goals = models.goals.get_goals(g.user["_id"], date)
    if goals:
        goals = goals["metadata"]
    return jsonify(goals=goals)
Beispiel #8
0
def pollChanges():
    last_modified = request.args.get('last_modified')
    date_str = request.args.get('date')
    date = date_helpers.parse_date_str(date_str)
    not_modified = 304

    metadata = models.entries.find_for_user_and_date(g.user["_id"], date)
    if metadata["file_metadata"]["last_modified"] <= last_modified:
        return make_response("", not_modified)

    return make_response("", 200)
Beispiel #9
0
def pollChanges():
    last_modified = request.args.get('last_modified')
    date_str = request.args.get('date')
    date = date_helpers.parse_date_str(date_str)
    not_modified = 304

    metadata = models.entries.find_for_user_and_date(g.user["_id"], date)
    if metadata["file_metadata"]["last_modified"] <= last_modified:
        return make_response("", not_modified)

    return make_response("", 200)
Beispiel #10
0
def addEntry():
    line = request.json['line']
    date_str = request.json['date']
    is_after_midnight = request.json['is_after_midnight'] == 'true'
    date = date_helpers.parse_date_str(date_str)
    perform_update(date_str, date, 'add_line', {
        "line": line,
        "is_after_midnight": is_after_midnight
    })
    entries = models.entries.find_for_user(g.user["_id"], {"date": date})
    return jsonify(entries=entries)
Beispiel #11
0
def update_goals():
    """ updates the goals file """
    start_date_str = request.json['start_date']
    end_date_str = request.json['end_date']
    start_date = date_helpers.parse_date_str(start_date_str)
    end_date = date_helpers.parse_date_str(end_date_str)

    content = request.form['content']

    # update Dropbox
    dropbox = DropboxApi()
    filename = start_date_str + "-" + end_date_str + ".md"
    file_metadata = dropbox.update_file(g.user["dropbox_access_token"], "/goals/" + filename, content)

    # update metadata
    goal_metadata = []
    if len(content) > 0:
        goal_metadata = GoalsParser(content).to_array()
        models.goals.create_or_update_entry(g.user["_id"], start_date, end_date, goal_metadata, file_metadata)
        return jsonify({"success": True, **file_metadata})
Beispiel #12
0
def getEntries():
    query = {}
    tag = request.args.get("tag")
    date_str = request.args.get("date")
    if tag:
        query["tag"] = tag
    if date_str:
        date = date_helpers.parse_date_str(date_str)
        query["date"] = date
    if len(query) == 0:
        return "missing query parameters", 400
    entries = models.entries.find_for_user(g.user["_id"], query)
    return jsonify(entries=entries)
Beispiel #13
0
def getEntries():
    query = {}
    tag = request.args.get('tag')
    date_str = request.args.get('date')
    if tag:
        query["tag"] = tag
    if date_str:
        date = date_helpers.parse_date_str(date_str)
        query["date"] = date
    if len(query) == 0:
        return "missing query parameters", 400
    entries = models.entries.find_for_user(g.user["_id"], query)
    return jsonify(entries=entries)
Beispiel #14
0
def editEntry():
    prev_line = request.json['prev_line']
    new_line = request.json['new_line']
    date_str = request.json['date']
    is_after_midnight = request.json['is_after_midnight'] == 'true'
    date = date_helpers.parse_date_str(date_str)
    perform_update(
        date_str, date, 'edit_line', {
            "prev_line": prev_line,
            "new_line": new_line,
            "is_after_midnight": is_after_midnight,
        })
    entries = models.entries.find_for_user(g.user["_id"], {"date": date})
    return jsonify(entries=entries)
Beispiel #15
0
def getTags():
    date_str = request.args.get('date')
    tags = []
    if date_str is not None:
        date = date_helpers.parse_date_str(date_str)
        entry = models.entries.find_one_for_user_and_date(g.user["_id"], date)
        if entry is not None:
            summary = TasksParser.summerize([entry['metadata']], date)
            tags.extend([{"type": "tag", "tag": tag} for tag in summary["tags"]])
            tags.extend([{"type": "person", "tag": tag} for tag in summary["people"]])
            tags.extend([{"type": "location", "tag": tag} for tag in summary["locations"]])
    else:
        tags = models.tags.get_all_tags(g.user["_id"])
    return jsonify(tags=tags)
Beispiel #16
0
def editEntry():
    prev_line = request.json["prev_line"]
    new_line = request.json["new_line"]
    date_str = request.json["date"]
    is_after_midnight = request.json["is_after_midnight"] == "true"
    date = date_helpers.parse_date_str(date_str)
    perform_update(
        date_str,
        date,
        "edit_line",
        {"prev_line": prev_line, "new_line": new_line, "is_after_midnight": is_after_midnight},
    )
    entries = models.entries.find_for_user(g.user["_id"], {"date": date})
    return jsonify(entries=entries)
Beispiel #17
0
def update_goals():
    """ updates the goals file """
    start_date_str = request.json['start_date']
    end_date_str = request.json['end_date']
    start_date = date_helpers.parse_date_str(start_date_str)
    end_date = date_helpers.parse_date_str(end_date_str)

    content = request.form['content']

    # update Dropbox
    dropbox = DropboxApi()
    filename = start_date_str + "-" + end_date_str + ".md"
    file_metadata = dropbox.update_file(g.user["dropbox_access_token"],
                                        "/goals/" + filename, content)

    # update metadata
    goal_metadata = []
    if len(content) > 0:
        goal_metadata = GoalsParser(content).to_array()
        models.goals.create_or_update_entry(g.user["_id"], start_date,
                                            end_date, goal_metadata,
                                            file_metadata)
        return jsonify({"success": True, **file_metadata})
Beispiel #18
0
def updateJournal():
    date_str = request.json['date']
    content = request.json['text']

    # update dropbox
    dropbox = DropboxApi()
    filename = date_str + ".md"
    file_metadata = dropbox.update_file(g.user["dropbox_access_token"], "/journal/" + filename, content)

    # update metadata
    date = date_helpers.parse_date_str(date_str)
    entry_metadata = {}
    if len(content) > 0:
        entry_metadata = TasksParser(date_str, content).to_dict()
        models.entries.create_or_update_entry(g.user["_id"], filename, date, entry_metadata, file_metadata)
        return jsonify({"success": True, **file_metadata})
Beispiel #19
0
def updateJournal():
    date_str = request.json['date']
    content = request.json['text']

    # update dropbox
    dropbox = DropboxApi()
    filename = date_str + ".md"
    file_metadata = dropbox.update_file(g.user["dropbox_access_token"],
                                        "/journal/" + filename, content)

    # update metadata
    date = date_helpers.parse_date_str(date_str)
    entry_metadata = {}
    if len(content) > 0:
        entry_metadata = TasksParser(date_str, content).to_dict()
        models.entries.create_or_update_entry(g.user["_id"], filename, date,
                                              entry_metadata, file_metadata)
        return jsonify({"success": True, **file_metadata})
Beispiel #20
0
def process_user(dropbox_user_id):
    # OAuth token for the user

    user = models.user.find_user_by_dropbox_id(str(dropbox_user_id))
    if not user:
        app.logger.error("User {0} was not found".format(dropbox_user_id))
        return

    cursor = None
    if "hook_cursor" in user:
        cursor = user["hook_cursor"]

    app.logger.info("Processing update for user {0}".format(user["email"]))

    dropbox = DropboxApi()
    results, cursor = dropbox.get_files_in_folder("/journal",
                                                  user["dropbox_access_token"],
                                                  cursor)
    for result in results:
        app.logger.info("processing changed file {0}, with name: {1}".format(
            result["path_lower"], result["name"]))
        response = dropbox.get_file_content(user["dropbox_access_token"],
                                            result["path_lower"])
        content = response["content"]
        date_str = result["name"][:-3]
        filename = result["name"]
        try:
            date = date_helpers.parse_date_str(date_str)
        except ValueError:
            continue
    entry_metadata = TasksParser(date_str, content).to_dict()
    models.entries.create_or_update_entry(user["_id"], filename, date,
                                          entry_metadata,
                                          dict_without(response, "content"))

    app.logger.info("Finished processing update for user {0}".format(
        user["email"]))
    models.user.save_hook_cursor(user["_id"], cursor)
Beispiel #21
0
def getTags():
    date_str = request.args.get('date')
    tags = []
    if date_str is not None:
        date = date_helpers.parse_date_str(date_str)
        entry = models.entries.find_one_for_user_and_date(g.user["_id"], date)
        if entry is not None:
            summary = TasksParser.summerize([entry['metadata']], date)
            tags.extend([{
                "type": "tag",
                "tag": tag
            } for tag in summary["tags"]])
            tags.extend([{
                "type": "person",
                "tag": tag
            } for tag in summary["people"]])
            tags.extend([{
                "type": "location",
                "tag": tag
            } for tag in summary["locations"]])
    else:
        tags = models.tags.get_all_tags(g.user["_id"])
    return jsonify(tags=tags)
import models.user

def dict_without(d, key):
    new_d = d.copy()
    new_d.pop(key)
    return new_d

dropbox = DropboxApi()
for user in mongo.db.users.find():
    if user["dropbox_access_token"] == "fakeId":
        continue

    app.logger.info("Processing update for user {0}".format(user["email"]))

    mongo.db.entries.delete_many({})
    results, cursor = dropbox.get_files_in_folder("/journal/", user["dropbox_access_token"])
    for result in results:
        app.logger.info("processing file {0}, with name: {1}".format(result["path_lower"], result["name"]))
        response = dropbox.get_file_content(user["dropbox_access_token"], result["path_lower"])
        content = response["content"]
        date_str = result["name"][:-3]
        filename = result["name"]
        try:
            date = date_helpers.parse_date_str(date_str)
        except ValueError as e:
            continue
        entry_metadata = TasksParser(date_str, content).to_dict()
        models.entries.create_or_update_entry(user["_id"], filename, date, entry_metadata, dict_without(response, "content"))

    app.logger.info("Finished processing update for user {0}".format(user["email"]))