Skip to content

Commit

Permalink
Merge pull request #12 from foreverjun/ver2_dev
Browse files Browse the repository at this point in the history
actual code of se.math.spbu.ru
  • Loading branch information
iakov committed May 13, 2023
2 parents 81ce57b + 9dc37d1 commit fdc629f
Show file tree
Hide file tree
Showing 14 changed files with 372 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linter.yml
Expand Up @@ -9,6 +9,6 @@ jobs:
- uses: actions/checkout@v2
- uses: psf/black@stable
with:
options: "--check --verbose --diff"
options: "--check --verbose --diff --exclude '/migrations'"
src: "./src"
version: "~= 23.0"
3 changes: 2 additions & 1 deletion requirements.txt
Expand Up @@ -56,4 +56,5 @@ Pillow==9.3.0
google-auth-oauthlib==0.7.1
textile==4.0.2
whoosh==2.7.4
pip==22.3.1
pip==22.3.1
zipp==3.7.0
12 changes: 10 additions & 2 deletions src/flask_se.py
Expand Up @@ -137,7 +137,12 @@
reports_staff,
finished_thesises_staff,
)
from flask_se_practice_admin import index_admin, deadline_admin, choose_worktype_admin
from flask_se_practice_admin import (
index_admin,
deadline_admin,
choose_worktype_admin,
thesis_admin,
)

app = Flask(
__name__, static_url_path="", static_folder="static", template_folder="templates"
Expand Down Expand Up @@ -341,7 +346,7 @@
)

# Practice admin
app.add_url_rule("/practice_admin", methods=["GET"], view_func=index_admin)
app.add_url_rule("/practice_admin", methods=["GET", "POST"], view_func=index_admin)
app.add_url_rule(
"/practice_admin/choose_worktype",
methods=["GET", "POST"],
Expand All @@ -350,6 +355,9 @@
app.add_url_rule(
"/practice_admin/deadline", methods=["GET", "POST"], view_func=deadline_admin
)
app.add_url_rule(
"/practice_admin/thesis", methods=["GET", "POST"], view_func=thesis_admin
)

# Init Database
db.app = app
Expand Down
11 changes: 5 additions & 6 deletions src/flask_se_practice.py
Expand Up @@ -464,7 +464,7 @@ def practice_preparation(current_thesis):
return redirect(url_for("practice_preparation", id=current_thesis.id))

if text_file is not None and text_file.filename != "":
full_filename, filename = get_filename(
full_filename, filename = __get_filename(
current_thesis, TEXT_UPLOAD_FOLDER, TypeOfFile.TEXT.value
)
text_file.save(full_filename)
Expand Down Expand Up @@ -519,7 +519,7 @@ def practice_preparation(current_thesis):
return redirect(url_for("practice_preparation", id=current_thesis.id))

if supervisor_review:
full_filename, filename = get_filename(
full_filename, filename = __get_filename(
current_thesis,
REVIEW_UPLOAD_FOLDER,
TypeOfFile.SUPERVISOR_REVIEW.value,
Expand All @@ -532,7 +532,7 @@ def practice_preparation(current_thesis):
)

if reviewer_review:
full_filename, filename = get_filename(
full_filename, filename = __get_filename(
current_thesis,
REVIEW_UPLOAD_FOLDER,
TypeOfFile.REVIEWER_REVIEW.value,
Expand All @@ -543,7 +543,6 @@ def practice_preparation(current_thesis):
flash("Отзыв рецензента успешно загружен!", category="success")

elif "submit_presentation_button" in request.form:
print(request.files)
presentation_file = (
request.files["presentation"]
if "presentation" in request.files
Expand Down Expand Up @@ -591,7 +590,7 @@ def practice_preparation(current_thesis):
return redirect(url_for("practice_preparation", id=current_thesis.id))

if presentation_file is not None and presentation_file.filename != "":
full_filename, filename = get_filename(
full_filename, filename = __get_filename(
current_thesis,
PRESENTATION_UPLOAD_FOLDER,
TypeOfFile.PRESENTATION.value,
Expand Down Expand Up @@ -636,7 +635,7 @@ def practice_preparation(current_thesis):
)


def get_filename(
def __get_filename(
current_thesis: CurrentThesis, folder: str, type_of_file: str
) -> Tuple[str, str]:
author_en = translit(current_user.get_name(), "ru", reversed=True)
Expand Down
106 changes: 98 additions & 8 deletions src/flask_se_practice_admin.py
@@ -1,12 +1,15 @@
# -*- coding: utf-8 -*-
import os
import io
from functools import wraps

import pytz
from flask import flash, redirect, request, render_template, url_for
from flask import flash, redirect, request, render_template, url_for, send_file
from datetime import datetime

from flask_login import current_user
from pytz import timezone
from zipfile import ZipFile
from transliterate import translit

from flask_se_auth import login_required
from se_forms import DeadlineTemp, CurrentWorktypeArea
Expand All @@ -20,19 +23,25 @@
add_mail_notification,
Staff,
)

from flask_se_practice import (
TEXT_UPLOAD_FOLDER,
PRESENTATION_UPLOAD_FOLDER,
REVIEW_UPLOAD_FOLDER,
)
from flask_se_config import get_thesis_type_id_string
from templates.practice.admin.templates import PracticeAdminTemplates

FORMAT_DATE_TIME = "%d.%m.%Y %H:%M"
ARCHIVE_FOLDER = "static/zip/"


def user_is_staff(func):
@wraps(func)
def check_user_is_staff_decorator():
def check_user_is_staff_decorator(*args, **kwargs):
user_staff = Staff.query.filter_by(user_id=current_user.id).first()
if not user_staff:
return redirect(url_for("practice_index"))
return func()
return func(*args, **kwargs)

return check_user_is_staff_decorator

Expand Down Expand Up @@ -82,11 +91,15 @@ def index_admin():
area = AreasOfStudy.query.filter_by(id=area_id).first()
worktype = Worktype.query.filter_by(id=worktype_id).first()

if request.method == "POST":
if "download_materials_button" in request.form:
return download_materials(area, worktype)

list_of_thesises = (
CurrentThesis.query.filter_by(status=1)
.filter_by(deleted=False)
.filter_by(area_id=area_id)
CurrentThesis.query.filter_by(area_id=area_id)
.filter_by(worktype_id=worktype_id)
.filter_by(deleted=False)
.filter_by(status=1)
.all()
)
return render_template(
Expand All @@ -97,6 +110,83 @@ def index_admin():
)


@login_required
@user_is_staff
def download_materials(area, worktype):
thesises = (
CurrentThesis.query.filter_by(area_id=area.id)
.filter_by(worktype_id=worktype.id)
.filter_by(deleted=False)
.filter_by(status=1)
.all()
)

filename = (
get_thesis_type_id_string(worktype.id)
+ "_"
+ translit(area.area, "ru", reversed=True).replace(" ", "_")
+ ".zip"
)
full_filename = ARCHIVE_FOLDER + filename

with ZipFile(full_filename, "w") as zip_file:
for thesis in thesises:
if thesis.text_uri is not None:
zip_file.write(
TEXT_UPLOAD_FOLDER + thesis.text_uri, arcname=thesis.text_uri
)
if thesis.supervisor_review_uri is not None:
zip_file.write(
REVIEW_UPLOAD_FOLDER + thesis.supervisor_review_uri,
arcname=thesis.supervisor_review_uri,
)
if thesis.reviewer_review_uri is not None:
zip_file.write(
REVIEW_UPLOAD_FOLDER + thesis.reviewer_review_uri,
arcname=thesis.reviewer_review_uri,
)
if thesis.presentation_uri is not None:
zip_file.write(
PRESENTATION_UPLOAD_FOLDER + thesis.presentation_uri,
arcname=thesis.presentation_uri,
)

return __send_file_and_remove(full_filename, filename)


def __send_file_and_remove(full_filename, filename):
return_data = io.BytesIO()
with open(full_filename, "rb") as fo:
return_data.write(fo.read())
return_data.seek(0)
os.remove(full_filename)
return send_file(return_data, mimetype=full_filename, attachment_filename=filename)


@login_required
@user_is_staff
def thesis_admin():
current_thesis_id = request.args.get("id", type=int)
if not current_thesis_id:
return redirect(url_for("index_admin"))
current_thesis = CurrentThesis.query.filter_by(id=current_thesis_id).first()
if not current_thesis:
return redirect(url_for("index_admin"))
area_id = request.args.get("area_id", type=int)
worktype_id = request.args.get("worktype_id", type=int)
if not area_id or not worktype_id:
return redirect(url_for("choose_worktype_admin", source=index_admin.__name__))
area = AreasOfStudy.query.filter_by(id=area_id).first()
worktype = Worktype.query.filter_by(id=worktype_id).first()

return render_template(
PracticeAdminTemplates.THESIS.value,
thesis=current_thesis,
area=area,
worktype=worktype,
)


@login_required
@user_is_staff
def deadline_admin():
Expand Down
2 changes: 2 additions & 0 deletions src/static/currentThesis/reviews/.gitignore
@@ -0,0 +1,2 @@
# ignore all files in this folder
*
Binary file not shown.
2 changes: 2 additions & 0 deletions src/static/currentThesis/slides/.gitignore
@@ -0,0 +1,2 @@
# ignore all files in this folder
*
Binary file not shown.
2 changes: 2 additions & 0 deletions src/static/currentThesis/texts/.gitignore
@@ -0,0 +1,2 @@
# ignore all files in this folder
*
3 changes: 3 additions & 0 deletions src/static/zip/.gitignore
@@ -0,0 +1,3 @@
# ignore all files in this folder
# for add empty folder to git
*
16 changes: 14 additions & 2 deletions src/templates/practice/admin/current_thesises_admin.html
Expand Up @@ -15,15 +15,24 @@ <h5>Работы</h5>
{% if not list_of_thesises %}
<div class="row">
<div class="col text-center">
<p>Здесь будут отображаться текущ6ие работы по направлению<br>{{ area }}, {{ worktype }}</p>
<p>Здесь будут отображаться текущие работы по направлению<br>{{ area }}, {{ worktype }}</p>
</div>
</div>
{% else %}

<form method="POST" action="{{ url_for('index_admin', area_id=area.id, worktype_id=worktype.id) }}">
<div class="row mb-3">
<div class="col">
<input type="submit" class="btn btn-sm btn-primary" name="download_materials_button" value="Загрузить материалы работ">
</div>
</div>
</form>

{% for thesis in list_of_thesises %}
<div class="card border-0 mb-3">
<div class="card-header">
<h6 class="mb-0">
<a id="{{thesis.id}}" name="thesis_link" href="{{ url_for('thesis_staff', id=thesis.id) }}" class="link text-primary">
<a href="{{ url_for('thesis_admin', id=thesis.id, area_id=area.id, worktype_id=worktype.id) }}" class="link text-primary">
<strong>{{ thesis.title }}</strong>
</a>
</h6>
Expand All @@ -34,6 +43,9 @@ <h6 class="mb-0">
</div>
</div>
{% endfor %}



{% endif %}

</div>
Expand Down
1 change: 1 addition & 0 deletions src/templates/practice/admin/templates.py
Expand Up @@ -5,3 +5,4 @@ class PracticeAdminTemplates(Enum):
CURRENT_THESISES = "practice/admin/current_thesises_admin.html"
CHOOSE_WORKTYPE = "practice/admin/choose_worktype_admin.html"
DEADLINE = "practice/admin/deadline_admin.html"
THESIS = "practice/admin/thesis_admin.html"

0 comments on commit fdc629f

Please sign in to comment.