Skip to content

Commit

Permalink
Fix: transfer data using session
Browse files Browse the repository at this point in the history
  • Loading branch information
ASlugin authored and KirillSmirnov committed Sep 26, 2023
1 parent be36db2 commit 2a73bc1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
39 changes: 22 additions & 17 deletions src/flask_se_practice_admin.py
Expand Up @@ -68,6 +68,21 @@ class PracticeAdminPage(Enum):
THESIS = "thesis"


request_column_names = {
"name": "user_name_column",
"how_to_contact": "how_to_contact_column",
"supervisor": "supervisor_column",
"consultant": "consultant_column",
"theme": "theme_column",
"text": "text_column",
"supervisor_review": "supervisor_review_column",
"reviewer_review": "reviewer_review_column",
"code": "code_column",
"committer": "committer_column",
"presentation": "presentation_column",
}


def user_is_staff(func):
@wraps(func)
def check_user_is_staff_decorator(*args, **kwargs):
Expand Down Expand Up @@ -135,36 +150,26 @@ def index_admin():
url_for("index_admin", area_id=area.id, worktype_id=worktype.id)
)

column_names = {
"name": request.form.get("user_name_column", ""),
"how_to_contact": request.form.get("how_to_contact_column", ""),
"supervisor": request.form.get("supervisor_column", ""),
"consultant": request.form.get("consultant_column", ""),
"theme": request.form.get("theme_column", ""),
"text": request.form.get("text_column", ""),
"supervisor_review": request.form.get("supervisor_review_column", ""),
"reviewer_review": request.form.get("reviewer_review_column", ""),
"code": request.form.get("code_column", ""),
"committer": request.form.get("committer_column", ""),
"presentation": request.form.get("presentation_column", ""),
}
for value in column_names.values():
if not value or value == "":
column_names = []
for column in TABLE_COLUMNS:
column_value = request.form.get(request_column_names[column], "")
if not column_value or column_value == "":
flash(
"Название столбца таблицы не может быть пустым",
category="error",
)
return redirect(
url_for("index_admin", area_id=area.id, worktype_id=worktype.id)
)
column_names.append((column, column_value))

if "yandex_button" in request.form:
return handle_yandex_table(
table_name=table_name,
sheet_name=sheet_name,
area_id=area.id,
worktype_id=worktype.id,
column_names=column_names,
column_names_list=column_names,
)
else:
table_filename = table_name.split("/")[-1]
Expand All @@ -175,7 +180,7 @@ def index_admin():
sheet_name=sheet_name,
area_id=area.id,
worktype_id=worktype.id,
column_names=column_names,
column_names_list=column_names,
)
return send_file(
full_filename, download_name=table_filename, as_attachment=True
Expand Down
6 changes: 4 additions & 2 deletions src/flask_se_practice_table.py
Expand Up @@ -23,13 +23,14 @@
from se_models import Users, CurrentThesis


def edit_table(path_to_table, sheet_name, area_id, worktype_id, column_names):
def edit_table(path_to_table, sheet_name, area_id, worktype_id, column_names_list):
if os.path.exists(path_to_table):
table_df = read_table(path_to_table, sheet_name)
if table_df is None:
return
else:
table_df = pd.DataFrame(columns=list(column_names.values()))
columns = [pair[1] for pair in column_names_list]
table_df = pd.DataFrame(columns=columns)

table = openpyxl.Workbook()
if sheet_name == "":
Expand All @@ -38,6 +39,7 @@ def edit_table(path_to_table, sheet_name, area_id, worktype_id, column_names):
table.active.title = sheet_name
table.save(path_to_table)

column_names = dict(column_names_list)
checked_thesis_ids = set()
for index, row in table_df.iterrows():
try:
Expand Down
11 changes: 5 additions & 6 deletions src/flask_se_practice_yandex_disk.py
Expand Up @@ -34,16 +34,15 @@
YANDEX_GET_TOKEN_URL,
)

COLUMN_NAMES = None


def handle_yandex_table(table_name, sheet_name, area_id, worktype_id, column_names):
def handle_yandex_table(
table_name, sheet_name, area_id, worktype_id, column_names_list
):
session["table_path"] = table_name
session["sheet_name"] = sheet_name
session["area_id"] = area_id
session["worktype_id"] = worktype_id
global COLUMN_NAMES
COLUMN_NAMES = column_names
session["column_names_list"] = column_names_list
return get_code()


Expand Down Expand Up @@ -106,7 +105,7 @@ def yandex_code():
sheet_name=session.get("sheet_name"),
area_id=area_id,
worktype_id=worktype_id,
column_names=COLUMN_NAMES,
column_names_list=session.get("column_names_list"),
)

try:
Expand Down

0 comments on commit 2a73bc1

Please sign in to comment.