Commit fd968e2a authored by Anthony Jacob's avatar Anthony Jacob
Browse files

adjust diploma to new fields

parent eb2019c6
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -81,7 +81,7 @@ def create_diploma():
            except ValueError:
                return jsonify({"error": "end_date must be in YYYY-MM-DD format"}), 400

        if "start_date" in data:
        if "start_date" in data and data.get("start_date") is not None:
            try:
                start_date: date = datetime.strptime(
                    data.get("start_date"), "%Y-%m-%d"
@@ -174,7 +174,7 @@ def update_diploma(id):
                            400,
                        )

                if "start_date" in data:
                if "start_date" in data  and data.get("start_date") is not None:
                    try:
                        start_date: date = datetime.strptime(
                            data.get("start_date"), "%Y-%m-%d"
+14 −5
Original line number Diff line number Diff line
@@ -72,6 +72,9 @@ def create_keyword():
        language_code = data.get("language_code")
        position = None

        print("*******************************keyword create *******************************")


        if "keyword" not in data:
            return jsonify({"error": "Missing keyword"}), 400

@@ -102,6 +105,8 @@ def create_keyword():
                400,
            )

        print(isKeywordExists(keyword, language_code))

        if position is None:
            position = getLastPosition(language_code) + 1

@@ -109,6 +114,8 @@ def create_keyword():

        keyword_id = insertKeyword(keyword, languageID, position)

        print(languageID, keyword_id)

        if keyword_id > 0:
            return (
                jsonify({"message": "Keyword inserted", "keyword_id": keyword_id}),
@@ -136,9 +143,11 @@ def update_keyword(id):
                return jsonify({"message": "keyword not found", "id": id}), 404
            elif keyword:
                data = request.get_json()
                keyword = data.get("keyword")
                keywordstr = data.get("keyword")
                language_code = data.get("language_code")
                position = data.get("position")
                print(data)
                print(keywordstr, language_code, position)

                if "keyword" not in data:
                    return jsonify({"error": "Missing keyword"}), 400
@@ -179,7 +188,7 @@ def update_keyword(id):
                        400,
                    )

                existingKeyword = getKeywordByCode(keyword, language_code)
                existingKeyword = getKeywordByCode(keywordstr, language_code)
                print(existingKeyword)
                if (
                    existingKeyword
@@ -189,14 +198,14 @@ def update_keyword(id):
                    return (
                        jsonify(
                            {
                                "error": f"the keyword {keyword} is already used by the keyword {existingKeyword['id']}"
                                "error": f"the keyword {keywordstr} is already used by the keyword {existingKeyword['id']}"
                            }
                        ),
                        400,
                    )

                languageID = getLanguageByCode(language_code)["id"]
                updatedKeyword = updateKeyword(id, keyword, languageID, position)
                updatedKeyword = updateKeyword(id, keywordstr, languageID, position)
                # updatedKeyword = -2

                if updatedKeyword == -2:
@@ -216,7 +225,7 @@ def update_keyword(id):
                        200,
                    )
                else:
                    return jsonify({"error": "Failed to insert keyword"}), 500
                    return jsonify({"error": "Failed to update keyword"}), 500

    except Exception as e:
        error_message = str(e)
+46 −7
Original line number Diff line number Diff line
@@ -30,7 +30,9 @@ def getDiplomasFull():
                json_build_object(
                    'language_code', L.language_code,
                    'title', DT.title,
                    'description', DT.description
                    'description', DT.description,
                    'link', DT.link,
                    'link_text', DT.link_text
                )
            ) AS translations
        FROM diploma D
@@ -67,7 +69,9 @@ def getDiploma(DiplomaId: int) -> Any | Literal[-1] | Literal[False]:
                json_build_object(
                    'language_code', L.language_code,
                    'title', DT.title,
                    'description', DT.description
                    'description', DT.description,
                    'link', DT.link,
                    'link_text', DT.link_text
                )
            ) AS translations
        FROM diploma D
@@ -110,11 +114,14 @@ def insertDiploma(
                diploma_id = cur.fetchone()[0]
                conn.commit()
                for DiplomaTranslation in translations:
                    print(DiplomaTranslation)
                    if not insertDiplomaTranslation(
                        diploma_id,
                        DiplomaTranslation["language_code"],
                        DiplomaTranslation["title"],
                        DiplomaTranslation["description"],
                        DiplomaTranslation["link"],
                        DiplomaTranslation["link_text"],
                    ):
                        raise Exception(
                            f"Database error while inserting diploma translation for {DiplomaTranslation['language_code']}"
@@ -131,19 +138,21 @@ def insertDiploma(


def insertDiplomaTranslation(
    diplomaID: int, language_code: str, title: str, description: str
    diplomaID: int, language_code: str, title: str | None = None, description: str | None = None, link: str | None = None, link_text: str | None = None
) -> bool | int:

    InsertDiplomaQuery = """INSERT INTO diploma_translation (diploma_id, language_id, title, description)
               VALUES (%s, (SELECT id FROM language WHERE language_code= %s ), %s, %s);"""
    InsertDiplomaQuery = """INSERT INTO diploma_translation (diploma_id, language_id, title, description, link, link_text)
               VALUES (%s, (SELECT id FROM language WHERE language_code= %s ), %s, %s, %s, %s);"""

    title = None if title == "NULL" else title
    description = None if description == "NULL" else description
    link = None if link == "NULL" else link
    link_text = None if link_text == "NULL" else link_text

    try:
        with db_cursor() as (conn, cur):
            cur.execute(
                InsertDiplomaQuery, (diplomaID, language_code, title, description)
                InsertDiplomaQuery, (diplomaID, language_code, title, description, link, link_text)
            )
            conn.commit()

@@ -155,7 +164,7 @@ def insertDiplomaTranslation(


def updateDiplomaTranslation(
    diplomaID: int, language_code: str, title: str, description: str
    diplomaID: int, language_code: str, title: str | None = None, description: str | None = None, link: str | None = None, link_text: str | None = None
):
    try:
        with db_cursor() as (conn, cur):
@@ -177,6 +186,20 @@ def updateDiplomaTranslation(
                    update_fields.append("description = %s")
                    update_values.append(description)

            if link is not None:
                if link == "NULL":
                    update_fields.append("link = NULL")
                else:
                    update_fields.append("link = %s")
                    update_values.append(link)

            if link_text is not None:
                if link_text == "NULL":
                    update_fields.append("link_text = NULL")
                else:
                    update_fields.append("link_text = %s")
                    update_values.append(link_text)

            if update_fields:
                update_query = f"""UPDATE diploma_translation
                                                SET {', '.join(update_fields)}
@@ -266,6 +289,18 @@ def updateDiploma(
                        else:
                            diplomaTranslation["title"] = None

                        if "link" in diplomaTranslation:
                            if diplomaTranslation["link"] is None:
                                diplomaTranslation["link"] = "NULL"
                        else:
                            diplomaTranslation["link"] = None

                        if "link_text" in diplomaTranslation:
                            if diplomaTranslation["link_text"] is None:
                                diplomaTranslation["link_text"] = "NULL"
                        else:
                            diplomaTranslation["link_text"] = None

                        if "description" in diplomaTranslation:
                            if diplomaTranslation["description"] is None:
                                diplomaTranslation["description"] = "NULL"
@@ -279,6 +314,8 @@ def updateDiploma(
                                id,
                                diplomaTranslation["language_code"],
                                diplomaTranslation["title"],
                                diplomaTranslation["link"],
                                diplomaTranslation["link_text"],
                                diplomaTranslation["description"],
                            ):
                                raise Exception(
@@ -288,6 +325,8 @@ def updateDiploma(
                            id,
                            diplomaTranslation["language_code"],
                            diplomaTranslation["title"],
                            diplomaTranslation["link"],
                            diplomaTranslation["link_text"],
                            diplomaTranslation["description"],
                        ):
                            raise Exception(
+1 −3
Original line number Diff line number Diff line
@@ -111,7 +111,7 @@ def insertKeyword(keyword: str, languageID: str, position: int = None) -> bool |
                    position,
                ),
            )

            conn.commit()
            keyword_id = cur.fetchone()[0]
            return keyword_id

@@ -140,8 +140,6 @@ def updateKeyword(id: int, keyword: str, language_id: int, position: int):
        """SELECT id FROM keyword WHERE UPPER(keyword_code) = UPPER(%s)"""
    )

    deleteKeywordsQuery = """"""

    try:
        with db_cursor() as (conn, cur):
            cur.execute(