Пример #1
0
def handleUserData(user=""):
    year = request.args.get('year', None, type=int)
    dayType = request.args.get('type', None, type=str)
    days = request.args.get('days', 0, type=int)
    location = request.args.get('location', None, type=str)

    if 'year' in request.args and not year:
        return jsonify({"error": "Invalid year given"}), 400

    if dayType:
        dayType = dayType.lower()

        if dayType not in {
                "office", "remote", "vacation", "holidays", "sick", "total"
        }:
            return jsonify({"error": "Invalid day type given"}), 400

    if location:
        location = location.title()

    if request.method == "GET":
        return get_worklog_data_service.getWorklogData(user, year, dayType)

    elif request.method == "POST":
        return modify_worklog_data_service.addWorklogData(
            user, year, dayType, days, location)

    elif request.method == "PUT":
        return modify_worklog_data_service.resetWorklogData(
            user, year, dayType, days, location, request.get_json())
Пример #2
0
def setWorklogData(user, year, dayType, days, location):
    startdate, lastdate = utils.getYearDates(year)

    if days < 0:
        return jsonify({"error": "Invalid days given"}), 400

    utils.addUserWorkLogData(user, year, dayType, days, location, startdate,
                             lastdate)

    return get_worklog_data_service.getWorklogData(user, year, dayType)
def addWorklogData(user, date, dayType, location, notes):
    if not date:
        return jsonify({"error": "No date given"}), 400

    if not dayType:
        return jsonify({"error": "No day type given"}), 400

    if dayType == "remote":
        if not location:
            return jsonify({"error": "No location given"}), 400

    hasDateData = utils.getUserDateData(user, date)

    if hasDateData:
        return jsonify({"error": "Data already exists for date given"}), 400

    utils.addUserWorkLogData(user, date, dayType, location, notes)

    return get_worklog_data_service.getWorklogData(user, None, date)
Пример #4
0
def handleUserData(user=""):
    year = request.args.get('year',None,type=int)
    dayType = request.args.get('type',None,type=str)
    date = request.args.get('date',None,type=str)
    location = request.args.get('location',None,type=str)
    deleteUser = request.args.get('deleteuser',False,type=bool)
    deleteAll = request.args.get('deleteall',False,type=bool)
    deleteYear = request.args.get('deleteyear',False,type=bool)
    
    if 'year' in request.args and not year:
        return jsonify({"error": "Invalid year given"}), 400
    
    if dayType:
        dayType = dayType.lower()

        if dayType not in {"office","remote","vacation","holiday","sick"}:
            return jsonify({"error": "Invalid day type given"}), 400
        
    if date:
        try:
            if sys.version_info.major >= 3 and sys.version_info.minor >= 7:
                date = datetime.date.fromisoformat(date)
            else:
                date_split = date.split("-")
                date = datetime.date(int(date_split[0]),int(date_split[1]),int(date_split[2]))
        except:
            return jsonify({"error": "Invalid date given"}), 400
        
    if location:
        location = location.title()
    
    if request.method == "GET":
        return get_worklog_data_service.getWorklogData(user, year, date) 
        
    elif request.method == "POST":
        return modify_worklog_data_service.addWorklogData(user, date, dayType, location)
    
    elif request.method == "PUT":
        return modify_worklog_data_service.updateWorklogData(user, date, dayType, location)
    
    elif request.method == "DELETE":
        return modify_worklog_data_service.deleteWorklogData(user, date, year, deleteUser, deleteAll, deleteYear)
def updateWorklogData(user, date, dayType, location, notes):
    if not date:
        return jsonify({"error": "No date given"}), 400

    if not dayType:
        return jsonify({"error": "No day type given"}), 400

    if dayType == "remote":
        if not location:
            return jsonify({"error": "No location given"}), 400

    userDateData = utils.getUserDateData(user, date)

    if userDateData:

        utils.updateUserWorkLog(user, date, dayType, location, notes)

        return get_worklog_data_service.getWorklogData(user, None, date)

    return jsonify({"error": "No data found to update"}), 404
Пример #6
0
def updateWorklogData(user, year, dayType, days, location, userYearData):
    _, lastdate = utils.getYearDates(year)

    updatedData = {"lastdate": lastdate}

    if dayType == "remote":
        updatedData[dayType + ".total"] = userYearData[dayType]["total"] + days

        if location in userYearData[dayType]["locations"]:
            if days < 0 or userYearData[dayType]["locations"][
                    location] + days > (userYearData["lastdate"] -
                                        userYearData["startdate"]).days:
                return jsonify({"error": "Invalid days given"}), 400

            updatedData[
                dayType + ".locations." +
                location] = userYearData[dayType]["locations"][location] + days
        else:
            if days < 0:
                return jsonify({"error": "Invalid days given"}), 400

            locations = userYearData[dayType]["locations"]

            locations[location] = days

            updatedData[dayType + ".locations"] = locations
    else:
        if days < 0 or userYearData[dayType] + days > (
                userYearData["lastdate"] - userYearData["startdate"]).days:
            return jsonify({"error": "Invalid days given"}), 400

        updatedData[dayType] = userYearData[dayType] + days

    utils.updateUserWorkLog(user, year, updatedData)

    return get_worklog_data_service.getWorklogData(user, year, dayType)
Пример #7
0
def resetWorklogData(user, year, dayType, days, location, data):
    if not dayType:
        return jsonify({"error": "No day type given"}), 400

    userYearData = utils.getUserYearData(user, year)

    if userYearData:
        if dayType == "remote":
            if days > userYearData[dayType]["locations"][location] or days < 0:
                return jsonify({"error": "Invalid days given"}), 400

            updatedData = {
                dayType + ".total":
                userYearData[dayType]["total"] -
                userYearData[dayType]["locations"][location] + days,
                dayType + ".locations." + location:
                days
            }
        else:
            if days > userYearData[dayType] or days < 0:
                return jsonify({"error": "Invalid days given"}), 400

            updatedData = {dayType: days}

        if data:
            startdate = None
            lastdate = None
            if "lastdate" in data:
                try:
                    lastdate = datetime.datetime(year,
                                                 data["lastdate"]["month"],
                                                 data["lastdate"]["day"])
                except:
                    return jsonify({"error": "Invalid dates given"}), 400

            if "startdate" in data:
                try:
                    startdate = datetime.datetime(year,
                                                  data["startdate"]["month"],
                                                  data["startdate"]["day"])
                except:
                    return jsonify({"error": "Invalid dates given"}), 400

                if lastdate:
                    if (lastdate - startdate).days < 0:
                        return jsonify({"error": "Invalid dates given"}), 400
                else:
                    if (userYearData["lastdate"] - startdate).days < 0:
                        return jsonify({"error": "Invalid dates given"}), 400

            if startdate:
                updatedData["startdate"] = startdate

            if lastdate:
                updatedData["lastdate"] = lastdate

        utils.updateUserWorkLog(user, year, updatedData)

        return get_worklog_data_service.getWorklogData(user, year, dayType)

    return jsonify({"error": "No data found for year given"}), 404
Пример #8
0
def handleUserData(user=""):
    year = request.args.get('year', None, type=int)
    dayType = request.args.get('type', None, type=str)
    date = request.args.get('date', None, type=str)
    location = request.args.get('location', None, type=str)
    deleteUser = request.args.get('deleteuser', False, type=bool)
    deleteAll = request.args.get('deleteall', False, type=bool)
    deleteYear = request.args.get('deleteyear', False, type=bool)

    data = request.get_json()
    notes = ""
    if data and "notes" in data:
        notes = data["notes"]

    if 'year' in request.args and not year:
        return jsonify({"error": "Invalid year given"}), 400

    if dayType:
        dayType = dayType.lower()

        if dayType not in {"office", "remote", "vacation", "holiday", "sick"}:
            return jsonify({"error": "Invalid day type given"}), 400

    if date:
        try:
            if sys.version_info.major >= 3 and sys.version_info.minor >= 7:
                date = datetime.date.fromisoformat(date)
            else:
                date_split = date.split("-")
                date = datetime.date(int(date_split[0]), int(date_split[1]),
                                     int(date_split[2]))
        except:
            return jsonify({"error": "Invalid date given"}), 400

    if location:
        location = location.title()

    if request.method == "GET":
        return get_worklog_data_service.getWorklogData(user, year, date)

    elif request.method == "POST":
        response = modify_worklog_data_service.addWorklogData(
            user, date, dayType, location, notes)

        if type(response) != tuple and response.status_code == 200:
            settings = json.loads(
                get_worklog_data_service.getWorklogSettings(
                    user).data)["settings"]
            if settings["slack"].strip():
                try:
                    data = {
                        "payload": {
                            "text":
                            user + " added " + dayType + " for " +
                            date.isoformat()
                        }
                    }
                    data = bytes(urllib.parse.urlencode(data).encode())

                    urllib.request.urlopen(settings["slack"].strip(),
                                           data=data)
                except:
                    pass

        return response

    elif request.method == "PUT":
        response = modify_worklog_data_service.updateWorklogData(
            user, date, dayType, location, notes)

        if type(response) != tuple and response.status_code == 200:
            settings = json.loads(
                get_worklog_data_service.getWorklogSettings(
                    user).data)["settings"]
            if settings["slack"].strip():
                try:
                    data = {
                        "payload": {
                            "text":
                            user + " changed " + date.isoformat() + " to " +
                            dayType
                        }
                    }
                    data = bytes(urllib.parse.urlencode(data).encode())

                    urllib.request.urlopen(settings["slack"].strip(),
                                           data=data)
                except:
                    pass

        return response

    elif request.method == "DELETE":
        return modify_worklog_data_service.deleteWorklogData(
            user, date, year, deleteUser, deleteAll, deleteYear)