예제 #1
0
파일: auth.py 프로젝트: mb-dev/plan-my-time
def authorize():
    session = {}
    dropbox = DropboxApi()
    redirect_url = dropbox.dropbox_auth_start(app.config, session)
    return jsonify(
        url=redirect_url,
        csrf_token=session["dropbox-auth-csrf-token"].decode("utf-8"))
예제 #2
0
파일: hook.py 프로젝트: mb-dev/plan-my-time
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)
예제 #3
0
def getJournal():
    date_str = request.args.get('date')
    if not date_str:
        return "missing date", 400
    dropbox = DropboxApi()
    filename = date_str + ".md"
    content_and_metadata = dropbox.get_file_or_create(g.user["dropbox_access_token"], "/journal/" + filename)
    return jsonify(content_and_metadata)
예제 #4
0
def getJournal():
    date_str = request.args.get('date')
    if not date_str:
        return "missing date", 400
    dropbox = DropboxApi()
    filename = date_str + ".md"
    content_and_metadata = dropbox.get_file_or_create(
        g.user["dropbox_access_token"], "/journal/" + filename)
    return jsonify(content_and_metadata)
예제 #5
0
def get_goals_file():
    start_date_str = request.json['start_date']
    end_date_str = request.json['end_date']
    if not start_date_str or not end_date_str:
        return "missing date", 400
    filename = start_date_str + "-" + end_date_str + ".md"

    dropbox = DropboxApi()
    content_and_metadata = dropbox.get_file_or_create(g.user["dropbox_access_token"], "/goals/" + filename)
    return jsonify(content_and_metadata)
예제 #6
0
def get_goals_file():
    start_date_str = request.json['start_date']
    end_date_str = request.json['end_date']
    if not start_date_str or not end_date_str:
        return "missing date", 400
    filename = start_date_str + "-" + end_date_str + ".md"

    dropbox = DropboxApi()
    content_and_metadata = dropbox.get_file_or_create(
        g.user["dropbox_access_token"], "/goals/" + filename)
    return jsonify(content_and_metadata)
예제 #7
0
파일: auth.py 프로젝트: mb-dev/plan-my-time
def finalize():
    session = {'dropbox-auth-csrf-token': request.json['csrf_token']}
    query = {'state': request.json['csrf_token'], 'code': request.json['code']}
    dropbox = DropboxApi()
    access_token, user_id, _ = dropbox.dropbox_auth_finish(app.config, session, query)
    user_info = dropbox.get_user_info(access_token)
    if user_info["verified"] is False:
        raise errors.AppException(errors.DROPBOX_EMAIL_NOT_VERIFIED)
    user_id = models.user.create_or_find_user({"dropbox_access_token": access_token, "dropbox_user_id": user_id, **user_info})
    encoded = jwt.encode({"user_id": str(user_id)}, app.config["TOKEN_SECRET"], algorithm='HS256')
    return jsonify(token=encoded.decode('utf-8'))
예제 #8
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})
예제 #9
0
def perform_update(date_str, date, action, params):
    metadata = models.entries.find_one_for_user_and_date(g.user["_id"], date)
    parser = TasksParser(date_str, "")
    if metadata:
        parser.from_dict(metadata["metadata"])
    if action == "add_line":
        parser.add_line(params["line"], params["is_after_midnight"])
    elif action == "edit_line":
        parser.edit_line(params["prev_line"], params["new_line"], params["is_after_midnight"])
    content = parser.to_tasks_file()
    entry_metadata = parser.to_dict()

    # update dropbox
    dropbox = DropboxApi()
    filename = date_str + ".md"
    file_metadata = dropbox.update_file(g.user["dropbox_access_token"], "/journal/" + filename, content)
    return models.entries.create_or_update_entry(g.user["_id"], filename, date, entry_metadata, file_metadata)
예제 #10
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})
예제 #11
0
파일: auth.py 프로젝트: mb-dev/plan-my-time
def finalize():
    session = {'dropbox-auth-csrf-token': request.json['csrf_token']}
    query = {'state': request.json['csrf_token'], 'code': request.json['code']}
    dropbox = DropboxApi()
    access_token, user_id, _ = dropbox.dropbox_auth_finish(
        app.config, session, query)
    user_info = dropbox.get_user_info(access_token)
    if user_info["verified"] is False:
        raise errors.AppException(errors.DROPBOX_EMAIL_NOT_VERIFIED)
    user_id = models.user.create_or_find_user({
        "dropbox_access_token": access_token,
        "dropbox_user_id": user_id,
        **user_info
    })
    encoded = jwt.encode({"user_id": str(user_id)},
                         app.config["TOKEN_SECRET"],
                         algorithm='HS256')
    return jsonify(token=encoded.decode('utf-8'))
예제 #12
0
def perform_update(date_str, date, action, params):
    metadata = models.entries.find_one_for_user_and_date(g.user["_id"], date)
    parser = TasksParser(date_str, "")
    if metadata:
        parser.from_dict(metadata["metadata"])
    if action == 'add_line':
        parser.add_line(params["line"], params["is_after_midnight"])
    elif action == 'edit_line':
        parser.edit_line(params["prev_line"], params["new_line"],
                         params["is_after_midnight"])
    content = parser.to_tasks_file()
    entry_metadata = parser.to_dict()

    # update dropbox
    dropbox = DropboxApi()
    filename = date_str + ".md"
    file_metadata = dropbox.update_file(g.user["dropbox_access_token"],
                                        "/journal/" + filename, content)
    return models.entries.create_or_update_entry(g.user["_id"], filename, date,
                                                 entry_metadata, file_metadata)
예제 #13
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})
예제 #14
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)
예제 #15
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})
예제 #16
0
파일: auth.py 프로젝트: mb-dev/plan-my-time
def authorize():
    session = {}
    dropbox = DropboxApi()
    redirect_url = dropbox.dropbox_auth_start(app.config, session)
    return jsonify(url=redirect_url, csrf_token=session["dropbox-auth-csrf-token"].decode("utf-8"))
예제 #17
0
sys.path.insert(0, os.path.join(os.path.dirname(script_path), "../"))

import lib.mongo as mongo
from app import app
from lib.dropbox          import DropboxApi
import lib.date_helpers   as     date_helpers
from parsers.tasks_parser import TasksParser
import models.entries
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: