File size: 3,539 Bytes
5aecaa0
 
 
6bbef4c
5aecaa0
 
 
6bbef4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6d5f06
6bbef4c
 
206aa70
6bbef4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5aecaa0
 
 
 
6bbef4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import streamlit as st
from azure.cosmos import CosmosClient
from dotenv import load_dotenv
import os

load_dotenv()

def handle_submit():
    client: CosmosClient = st.session_state["db"]
    database = client.get_database_client("assessment-database")
    container = database.get_container_client("assessments")
    assessment_item_coding_tasks = st.session_state["assessment_db_object"]["coding_tasks"]
    if st.session_state["upload_type"] == "Text upload":
        for i, coding_task in enumerate(st.session_state["assessment_db_object"]["coding_tasks"]):
            assessment_item_coding_tasks[i]["candidate_solution"] = st.session_state["code_text_"+str(i+1)]
    else:
        for i, coding_task in enumerate(st.session_state["assessment_db_object"]["coding_tasks"]):
            # print(st.session_state["file_upload_"+str(i+1)].read())
            try:
                assessment_item_coding_tasks[i]["candidate_solution"] = st.session_state["file_upload_"+str(i+1)].read().decode("utf-8")
            except:
                st.error("Please upload a valid code file")
                return
    upsert_item = st.session_state["assessment_db_object"]
    upsert_item["coding_tasks"] = assessment_item_coding_tasks
    upsert_item["process_status"] = "coding_solutions_uploaded"
    container.upsert_item(upsert_item)
    st.session_state["file_uploaded"] = True

if "db" not in st.session_state:
    client = CosmosClient(os.environ["COSMOS_DB_ENDPOINT"], os.environ["COSMOS_DB_KEY"])   
    st.session_state["db"] = client
if "assessment_db_object" not in st.session_state:
    if "assessment-id" in st.query_params:
        client: CosmosClient = st.session_state["db"]
        database = client.get_database_client("assessment-database")
        container = database.get_container_client("assessments")
        assessment = container.query_items(query="SELECT * FROM c WHERE c.id = @assessment_id", parameters=[dict(name="@assessment_id", value=st.query_params["assessment-id"]) ,], enable_cross_partition_query=True, max_item_count=1)
        st.session_state["assessment_db_object"] = list(assessment)[0]
    else:
        st.error("No assessment found. Please contact the side admin.", icon="๐Ÿšจ")
        st.stop()
if "file_uploaded" not in st.session_state:
    st.session_state["file_uploaded"] = False

col1, col2 = st.columns([2, 1])

col1.title("Code Upload")
col2.image("https://www.workgenius.com/wp-content/uploads/2023/03/WorkGenius_navy-1.svg")

if not st.session_state["file_uploaded"]:
    st.write("Assessment: "+st.session_state["assessment_db_object"]["assessment_title"])
    st.radio("Select text or file upload", ["Text upload", "File upload"], key="upload_type")

    if st.session_state["upload_type"] == "Text upload":
        for i, coding_task in enumerate(st.session_state["assessment_db_object"]["coding_tasks"]):
            st.write("Coding task "+str(i+1)+". : "+coding_task["coding_task"])
            st.text_area("Enter your code here", height=200, key="code_text_"+str(i+1))
    else:
        for i, coding_task in enumerate(st.session_state["assessment_db_object"]["coding_tasks"]):
            st.write("Coding task "+str(i+1)+". : "+coding_task["coding_task"])
            st.file_uploader("Upload your code file here", type=None, key="file_upload_"+str(i+1), accept_multiple_files=False)

    st.button("Submit", on_click=handle_submit)
    
else:    
    st.success("Your code has been successfully uploaded. You will be notified once the assessment is evaluated.", icon="๐ŸŽ‰")