Skip to content

Commit 489a3fd

Browse files
committed
fixes, added button on capacity.py
1 parent fdf1bef commit 489a3fd

File tree

8 files changed

+48
-7
lines changed

8 files changed

+48
-7
lines changed

app/api/files.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,14 @@ def upload_presentation() -> (dict, int):
145145
'message': 'Presentation file should not exceed {}MB.'.format(
146146
Config.c.constants.presentation_file_max_size_in_megabytes
147147
)
148-
}, 404
148+
}, 413
149149
# check if file can be stored (*2.5 is an estimate of pdf and preview)
150150
if not DBManager().check_storage_limit(request.content_length * 2.5):
151151
return {
152-
'message': "Not enough place in database to store file"
153-
}, 404
152+
'message': "Not enough space in database to store file. "
153+
"Please contact at email: "
154+
f"<a href='mailto:{Config.c.bugreport.report_mail}'>{Config.c.bugreport.report_mail}</a>"
155+
}, 413
154156

155157
presentation_file = request.files['presentation']
156158

app/mongo_odm.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
BYTES_PER_MB = 1024*1024
3434

3535
class DBManager:
36-
def __new__(cls, max_size=20000): # max_size only on first creation, in MB
36+
def __new__(cls):
3737
if not hasattr(cls, 'init_done'):
3838
cls.instance = super(DBManager, cls).__new__(cls)
3939
connect(Config.c.mongodb.url + Config.c.mongodb.database_name)
4040
cls.instance.storage = GridFSStorage(GridFSBucket(_get_db()))
41-
cls.instance.max_size = max_size * BYTES_PER_MB
41+
cls.instance.max_size = float(Config.c.constants.storage_max_size_mbytes) * BYTES_PER_MB
4242
cls.init_done = True
4343
return cls.instance
4444

@@ -111,7 +111,15 @@ def check_storage_limit(self, new_file_size):
111111
)
112112
logger.info(inf_msg)
113113
return False if current_size + new_file_size > self.max_size else True
114-
114+
115+
def recalculate_used_storage_data(self):
116+
total_size = 0
117+
db = _get_db()
118+
for file_doc in db.fs.files.find():
119+
total_size += file_doc['length']
120+
self.set_used_storage_size(total_size)
121+
logger.info(f"Storage size recalculated: {total_size/BYTES_PER_MB:.2f} MB")
122+
115123

116124
class TrainingsDBManager:
117125
def __new__(cls):

app/routes/capacity.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@ def storage_capacity():
2323
max_size=round(max_size / BYTES_PER_MB, 2),
2424
ratio=round(ratio * 100, 1)
2525
)
26-
26+
27+
@routes_capacity.route('/refresh_capacity', methods=['POST'])
28+
def refresh_capacity():
29+
if not is_admin():
30+
return {}, 404
31+
DBManager().recalculate_used_storage_data()
32+
return {'message': 'OK'}

app/static/js/upload.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ $(function(){
2626
return;
2727
}
2828
$("#spinner").hide();
29+
30+
if (response.status == 413) {
31+
response.json().then(responseJson => {
32+
$("#alert").show();
33+
$("#error-text").html(responseJson["message"] || "Файл слишком большой или превышает лимит хранилища");
34+
});
35+
$('#button-submit')[0].value = button_value;
36+
37+
return;
38+
}
39+
2940
response.json().then(responseJson => {
3041
if (responseJson["message"] !== "OK"){
3142
$("#alert").show();

app/templates/admin.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@
1313
<a href="{{ url_for("routes_admin.view_dumps")}}" target="_blank">{{ t("Архивы БД") }}</a>
1414
<br>
1515
<a href="{{ url_for("routes_version.view_version")}}" target="_blank">{{ t("Версия") }}</a>
16+
<br>
17+
<a href="{{ url_for("routes_capacity.storage_capacity")}}" target="_blank">{{ t("Заполненность БД") }}</a>
1618
</div>
1719
{% endblock %}

app/templates/capacity.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ <h4 class="card-title">Загруженность Базы Данных</h4>
1414
</div>
1515
</div>
1616
</div>
17+
<button type="button" id="refresh_button">Обновить данные</button>
18+
19+
<script src="/static/js/libraries/jquery.min.js"></script>
20+
<script>
21+
$('#refresh_button').click(function() {
22+
$.post("{{ url_for('routes_capacity.refresh_capacity') }}", {}, function(data) {
23+
location.reload();
24+
});
25+
});
26+
</script>
1727
</div>
1828
</div>
1929
{% endblock %}

app_conf/config.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ lti_consumer_key=secretconsumerkey
55
lti_consumer_secret=supersecretconsumersecret
66
version_file=VERSION.json
77
backup_path=../dump/database-dump/
8+
storage_max_size_mbytes=20000
89

910
[mongodb]
1011
url=mongodb://db:27017/

app_conf/testing.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ lti_consumer_key=testing_lti_consumer_key
55
lti_consumer_secret=testing_lti_consumer_secret
66
version_file=VERSION.json
77
backup_path=../dump/database-dump/
8+
storage_max_size_mbytes=20000
89

910
[mongodb]
1011
url=mongodb://db:27017/

0 commit comments

Comments
 (0)