Пример #1
0
def calendar_add():
    """Adds a calendar to the database according to the infos in POST data.\
        Also creates the calendar in google calendar service if no google_calendar_id is present in POST data. 
    """
    calendar_name = request.form["calendar_name"]
    std_email = request.form["std_email"]
    google_calendar_id = request.form["google_calendar_id"]
    if check_google_calendar_id(google_calendar_id):
        # Add the google calendar directly to the local DB (Assume that Calendar has been already created)
        cal_obj = Calendar(summary=calendar_name,
                           std_email=std_email,
                           calendar_id_google=google_calendar_id)
        try:
            db.session.add(cal_obj)
            db.session.commit()
        except Exception:
            flash(('Could not add calendar {} to google calendar'.format(
                calendar_name)),
                  category="error")
            return redirect(url_for("get_calendars"))
        return redirect(url_for("get_calendars"))
    else:
        # Creating a google calendar and receiving the gcal ID from Google
        cal_record = Calendar.query.filter_by(summary=calendar_name).first()
        if cal_record is None:
            calendar__ = {
                'summary': calendar_name,
                'timeZone': 'Africa/Algiers'
            }
            resp = google.post("/calendar/v3/calendars", json=calendar__)
            if resp.status_code == 200:
                if "id" in resp.json().keys():
                    calendar_id = resp.json()["id"]
                    calendar_obj = Calendar(calendar_id_google=calendar_id,
                                            summary=calendar_name,
                                            std_email=std_email)
                    db.session.add(calendar_obj)
                    db.session.commit()
                    flash(('Added calendar {} to google calendar'.format(
                        calendar_name)),
                          category="success")
                    return redirect(url_for("get_calendars"))
                else:
                    flash(("Invalid response from calendar api"),
                          category="danger")
                    return redirect(url_for('get_calendars')), 302
            else:
                flash(("Calendar API returned a non 200 response"),
                      category="danger")
                return redirect(url_for('get_calendars')), 302
        else:
            flash(("Calendar {} already found in application database".format(
                calendar_name)),
                  category="info")
            return redirect(url_for('get_calendars')), 302
def update_all_calendars_from_json_resp(
        filename="fet_api_to_gcal/data/calendars_new.json"):
    f = open(filename, "r")
    data = json.load(f)
    for calendar in data["items"]:
        summary = calendar["summary"]
        if "1CPI" or "2CPI" in summary:
            if summary[-2] == "0":
                summary = summary[:-2:] + summary[-1]
        calendar_id = calendar["id"]
        cal_obj = Calendar.query.filter_by(summary=summary).first()
        if cal_obj is None:
            # getting the student email from the summary
            student_set = Std_mail.query.filter_by(std_set=summary).first()
            if student_set is None:
                print("std set {} none".format(summary))
                continue
            else:
                calendar = Calendar(summary=summary,
                                    calendar_id_google=calendar_id,
                                    std_email=student_set.std_email)
                db.session.add(calendar)
            db.session.commit()
        else:
            # updating calendar
            cal_obj.calendar_id_google = calendar_id
            db.session.commit()
Пример #3
0
def add_calendar(summary, std_email):
    """Add a calendar to the database using a student_email and a calendar name (summary) as parameters.
    
    Args:
        summary (str): a string of the calendar name
        std_email (str): a string of the calendar student email.
    
    Returns:
        JSON: Reponse from the calendar API of the calendar creation status.
    """
    cal_record = Calendar.query.filter_by(summary=summary).first()
    if cal_record is None:
        calendar__ = {'summary': summary, 'timeZone': 'Africa/Algiers'}
        resp = google.post("/calendar/v3/calendars", json=calendar__)
        if resp.status_code == 200:
            calendar_id = resp.json()["id"]
        else:
            print(resp.json())
            calendar_id = None
        if calendar_id is not None:
            cal_rec = Calendar(calendar_id_google=calendar_id,
                               summary=summary,
                               std_email=std_email)
            db.session.add(cal_rec)
    else:
        print(cal_record)
    db.session.commit()
    if calendar_id:
        return jsonify(resp.json()), 200
    else:
        return jsonify({"status": "failed"}), 200
Пример #4
0
def import_calendars():
    """Imports calendars from a json file returned by calendar api.
    """
    if request.method == "GET":
        return render_template('calendars_import.html.j2',
                               title='Import Calendars')
    elif request.method == "POST":
        if 'file' not in request.files:
            flash(("No file part"), category='danger')
            return redirect(request.url)
        file = request.files['file']
        if file.filename == "":
            flash("No file selected for uploading", category='danger')
            return redirect(request.url)
        added_calendars = 1  # number of added calendars
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            for line in file.readlines():
                line = line.decode().strip().split(",")
                cal_record = Calendar.query.filter_by(summary=line[0]).first()
                print(cal_record)
                if cal_record is None:
                    calendar__ = {
                        'summary': line[0],
                        'timeZone': 'Africa/Algiers'
                    }
                    resp = google.post("/calendar/v3/calendars",
                                       json=calendar__)
                    if resp.status_code == 200:
                        calendar_id = resp.json()["id"]
                    else:
                        print(resp.json())
                        calendar_id = None
                    # ? I dont know if this is necessary, but you may be blocked by google if you initiate too many calendar
                    # ? creation requests.
                    sleep(5)
                    if calendar_id is not None:
                        cal_rec = Calendar(calendar_id_google=calendar_id,
                                           summary=line[0],
                                           std_email=line[1].replace('"', ''))
                        db.session.add(cal_rec)
                        added_calendars += 1
            db.session.commit()
            flash(("Added {} calendars to google calendar from file {}".format(
                added_calendars, filename)),
                  category="success")
            return redirect(request.url), 302
        else:
            flash(("[ERROR] File is not allowed"), category="danger")
            return render_template('calendars_import.html.j2'), 200
def import_calendars_from_google_json_resp(
        filename="fet_api_to_gcal/data/calendars.json"):
    f = open(filename, "r")
    data = json.load(f)
    for calendar in data["items"]:
        summary = calendar["summary"]
        calendar_id = calendar["id"]
        cal_obj = Calendar.query.filter_by(summary=summary).first()
        if cal_obj is None:
            # getting the student email from the summary
            student_set = Std_mail.query.filter_by(std_set=summary).first()
            if student_set is None:
                continue
            else:
                calendar = Calendar(summary=summary,
                                    calendar_id_google=calendar_id,
                                    std_email=student_set.std_email)
                db.session.add(calendar)
            db.session.commit()
        else:
            print("Calendar already in DB")
def add_calendar(summary, std_email):
    cal_record = Calendar.query.filter_by(summary=summary).first()
    if cal_record is None:
        calendar__ = {'summary': summary, 'timeZone': 'Africa/Algiers'}
        resp = google.post("/calendar/v3/calendars", json=calendar__)
        if resp.status_code == 200:
            calendar_id = resp.json()["id"]
        else:
            print(resp.json())
            calendar_id = None
        if calendar_id is not None:
            cal_rec = Calendar(calendar_id_google=calendar_id,
                               summary=summary,
                               std_email=std_email)
            db.session.add(cal_rec)
    else:
        print(cal_record)
    db.session.commit()
    if calendar_id:
        return jsonify(resp.json()), 200
    else:
        return jsonify({"status": "failed"}), 200