Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.0 kB
3
Indexable
Never
#!/Library/Frameworks/Python.framework/Versions/3.11/bin/python3

from http import cookies
import os

subjects = {
    'ip' : { 'name' : 'Introduction to programming' , 'year' : 1, 'ects' : 6 },
    'c1' : { 'name' : 'Calculus 1' , 'year' : 1, 'ects' : 7 },
    'cu' : { 'name' : 'Computer usage' , 'year' : 1, 'ects' : 5 },
    'dmt' : { 'name' : 'Digital and microprocessor technology', 'year' : 1, 'ects' : 6 },
    'db' : { 'name' : 'Databases' , 'year' : 2, 'ects' : 6 },
    'c2' : { 'name' : 'Calculus 2' , 'year' : 2, 'ects' : 7 },
    'dsa' : { 'name' : 'Data structures and alghoritms' , 'year' : 2, 'ects' : 5 },
    'ca' : { 'name' : 'Computer architecture', 'year' : 2, 'ects' : 6 },
    'isd' : { 'name' : 'Information systems design' , 'year' : 3, 'ects' : 5 },
    'c3' : { 'name' : 'Calculus 3' , 'year' : 3, 'ects' : 7 },
    'sa' : { 'name' : 'Server Architecture' , 'year' : 3, 'ects' : 6 },
    'cds' : { 'name' : 'Computer and data security', 'year' : 3, 'ects' : 6 }
    };
        
year_names = {
        1 : '1st Year',
        2 : '2nd Year',
        3 : '3rd Year'
    };

year_ids = {
        '1st Year' : 1,
        '2nd Year' : 2,
        '3rd Year' : 3
};

status_names = {
    'not' : 'Not selected',
    'enr' : 'Enrolled',
    'pass' : 'Passed',
};


def decide_year(year=None):
    cookies_string = os.environ.get('HTTP_COOKIE', '')
    all_cookies_object = cookies.SimpleCookie(cookies_string)
    if year is not None:
        cookie = cookies.SimpleCookie()
        cookie['year'] = year
        print(cookie.output())
    elif all_cookies_object.get('year'):
        year = all_cookies_object.get('year').value
    else:
        year = 1
    return year

def get_or_create_subject_cookies(subject_status=None):
    cookies_string = os.environ.get('HTTP_COOKIE', '')
    all_cookies_object = cookies.SimpleCookie(cookies_string)

    if subject_status is not None:
        cookie = cookies.SimpleCookie()
        for key in subjects:
            status = subject_status.get(key) if subject_status is not None and key in subject_status else ''
            cookie[key] = status
            print(cookie.output())

    else:
        subject_status = {}
        for key in subjects:
            if all_cookies_object.get('{}'.format(key)):
                status = all_cookies_object.get(key).value
                subject_status[key] = status
            else:
                subject_status[key] = subject_status.get(key) if subject_status is not None and key in subject_status else ''
    
    return subject_status


def print_radio_buttons(subject_key, selected_status):
    for status_code, status_name in status_names.items():
        checked = 'checked' if status_code == selected_status else ''
        print(f'<input type="radio" id="{subject_key}_{status_code}" name="{subject_key}_status" value="{status_code}" {checked}>{status_name}')


def display_buttons():
    for key in year_names:
        print(f'<a href="?year={year_ids[year_names[key]]}"><button>'+ year_names[key] +'</button></a>')

def display_subjects(year, subject_status):
    for key, value in subjects.items():
        if value['year'] == int(year):
            print("""<tr>
                        <td>{}</td>
                        <td>{}</td>
                        <td>""".format(value['name'], value['ects']))
            print_radio_buttons(key, subject_status)
            print("""
                        </td>
                    </tr>
            """)

def print_table(year, subject_status):
    
    print("""
        <div>
            <form method="post"> 
                <table>
                    <tr>
                        <td>"""+ year_names[int(year)]+"""</td>
                        <td>Ects</td>
                        <td>Status</td>
                    </tr>""")
    display_subjects(year, subject_status)

    print("""

                <input type="submit" value="Save">
                
            
            </table>
        </form>
        </div>
    """)