Skip to content

Commit

Permalink
feat: Allow movie votes data to be saved as CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
oscie57 committed Jul 7, 2024
1 parent 6b0cc95 commit 4e72785
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
17 changes: 17 additions & 0 deletions templates/vote_category_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@

{% block table_listing %}

<p>
{% if g.oidc_user.logged_in %}
<div class="field has-addons" style="display:flex; flex-direction: column; gap: 8px;">
<div style="display:flex; flex-direction:row;">
<p class="control">
<a href="{{ url_for('votes_download') }}"
class="button is-info">
<span class="icon is-small">
<i class="fas fa-download"></i>
</span>
<span>Download CSV</span>
</a>
</p>
</div>
{% endif %}
</p>

<p>
There is a total of {{ votes['count'] }} vote entries.
<br><br>
Expand Down
43 changes: 43 additions & 0 deletions theunderground/votes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
import config
import io
import csv

from flask import (
render_template,
Expand All @@ -8,13 +10,15 @@
send_from_directory,
request,
url_for,
make_response,
)
from flask_wtf.file import FileRequired
from werkzeug import exceptions

from asset_data import NormalCategoryAsset
from models import Categories, Movies, EvaluateData
from room import app, s3
from theunderground.admin import oidc
from theunderground.forms import CategoryForm
from theunderground.operations import manage_delete_item
from theunderground.mobiclip import (
Expand Down Expand Up @@ -143,3 +147,42 @@ def votes_get_movie_thumbnail(movie_id):
return redirect(f"{config.url1_cdn_url}/{movie_dir}/{movie_id}.img")

return send_from_directory(movie_dir, f"{movie_id}.img")


@app.route("/theunderground/votes/data.csv")
@oidc.require_login
def votes_download():

evaluatedata = EvaluateData.query.all()

si = io.StringIO()
cw = csv.writer(si)

cw.writerow(
[
"id",
"movie_id",
"gender",
"blood",
"age",
"vote"
]
)

for data in evaluatedata:
cw.writerow(
[
data.id,
data.movie_id,
data.gender,
data.blood,
data.age,
data.vote
]
)

output = make_response(si.getvalue())
output.headers["Content-Disposition"] = "attachment; filename=votes.csv"
output.headers["Content-Type"] = "text/csv"

return output

0 comments on commit 4e72785

Please sign in to comment.