Skip to content
This repository was archived by the owner on Nov 23, 2024. It is now read-only.
15 changes: 14 additions & 1 deletion Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ $msg_up = <<MSG_UP
System Wyborczy v2

- Panel administratora:
http://localhost:8080/admin
http://localhost:8080/admin

(Default)
Login:
admin
Pass:
haker7

- Konsola maszyny wirtualnej:
$ vagrant ssh
Expand All @@ -19,6 +25,13 @@ System Wyborczy v2

Panel administratora:
http://localhost:8080/admin

(Default)
Login:
admin
Pass:
haker7


Logi:
$ sw-logs
Expand Down
60 changes: 44 additions & 16 deletions sw-admin/sw_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
login_manager = LoginManager()
login_manager.init_app(app)

#machine Timezone
machTz = datetime.now().astimezone().tzinfo

# use dicts and lists as json in postgres
import psycopg2.extras
psycopg2.extensions.register_adapter(dict, psycopg2.extras.Json)
Expand Down Expand Up @@ -233,7 +236,7 @@ def admin_copypoll():
name, options, choice_type, possible_recipients, sending_out_to, closes_on_date, mail_template, max_choices, description = row
return render_template('copypoll.html', poll_id=poll_id, name=name, options=options, choice_type=choice_type,
possible_recipients=possible_recipients, sending_out_to_amount=0,
closes_on_date=closes_on_date, mail_template=mail_template, should_disable=False,
closes_on_date=closes_on_date.strftime("%Y-%m-%d %H:%M"), mail_template=mail_template, should_disable=False,
max_choices=max_choices, description=description)

@app.route('/admin/editpoll', methods=['GET'])
Expand All @@ -249,7 +252,7 @@ def admin_editpoll():
should_disable = "disabled" if sending_out_to_amount > 0 else ""
return render_template('editpoll.html', poll_id=poll_id, name=name, options=options, choice_type=choice_type,
possible_recipients=possible_recipients, sending_out_to_amount=sending_out_to_amount,
closes_on_date=closes_on_date, mail_template=mail_template, should_disable=should_disable,
closes_on_date=closes_on_date.strftime("%Y-%m-%d %H:%M"), mail_template=mail_template, should_disable=should_disable,
max_choices=max_choices, description=description)

@app.route('/admin/editpoll', methods=['POST'])
Expand Down Expand Up @@ -353,10 +356,28 @@ def admin_sendout_plan_post():
if not row:
return "Głosowanie nie istnieje", 400

closeOnDate = query_db_one('select closes_on_date from polls where id=%s and owner_user=%s', [poll_id, int(current_user.get_id())])


timestamp = f'{request.form["planSendoutDate"]} {request.form["planSendoutTime"]}'
db_execute('set time zone \'Europe/Warsaw\'; update polls set planned_start_sending=%s::timestamptz where id=%s and owner_user=%s',
[timestamp, poll_id, int(current_user.get_id())])
get_db().commit()

#todo: improve name. Tired lol
beforeCloseMins = 5


t1=datetime.strptime(timestamp,"%Y-%m-%d %H:%M")
t2=datetime.now()
tClose = closeOnDate[0].replace(tzinfo=None) #ByPassTMP
difCl = tClose - t1

if t1 <= t2:
flash("Data wysyłki jest z przeszłości")
elif difCl.total_seconds() / 60 < beforeCloseMins:
flash("Wysłanie maili musi nastapić " + str(beforeCloseMins) + " min najpóźniej przed końcem wyborów")
else:
db_execute('set time zone \'Europe/Warsaw\'; update polls set planned_start_sending=%s::timestamptz where id=%s and owner_user=%s',
[timestamp, poll_id, int(current_user.get_id())])
get_db().commit()

return redirect(f'/admin/sendout?id={poll_id}', code=303)

Expand Down Expand Up @@ -454,21 +475,28 @@ def admin_results():
return "Głosowanie nie istnieje", 400
name, options, max_choices, possible_recipients_len, sending_out_to_len, sent_to_len = row
print(f"ilosc opcji :{ max_choices}")
vote_results = result_getter(poll_id, max_choices)

voted_len = int(subprocess.Popen(['du', '--inodes', '-sS', f"/opt/sw/poll/{poll_id}/results/"], stdout=subprocess.PIPE).stdout.read().decode('utf8').split('\t')[0]) - 1

results = []
for i, option in enumerate(options):
results.append({ 'score': vote_results.get(f"option_{i}", 0), 'name': option['name'], 'description': option['description'] })

try:
vote_results = result_getter(poll_id, max_choices)

voted_len = int(subprocess.Popen(['du', '--inodes', '-sS', f"/opt/sw/poll/{poll_id}/results/"], stdout=subprocess.PIPE).stdout.read().decode('utf8').split('\t')[0]) - 1


for i, option in enumerate(options):
results.append({ 'score': vote_results.get(f"option_{i}", 0), 'name': option['name'], 'description': option['description'] })
except:
return "Nikt nie zagłosował v Brak plików", 400


return render_template('results.html',
name=name,
possible_recipients_len=possible_recipients_len,
sending_out_to_len=sending_out_to_len,
sent_to_len=sent_to_len,
voted_len=voted_len,
results=results)
name=name,
possible_recipients_len=possible_recipients_len,
sending_out_to_len=sending_out_to_len,
sent_to_len=sent_to_len,
voted_len=voted_len,
results=results)

@app.route('/admin/peek', methods=['GET'])
@login_required
Expand Down
12 changes: 12 additions & 0 deletions sw-admin/templates/sendout.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
{% extends "baseloggedin.html" %}
{% block title %}Rozsyłka maili dla głosowania "{{name}}"{% endblock %}
{% block content %}

<!-- Miejsce na błędy, na przykład "nie można edytować głosowania ponieważ wiadomości zostały już rozesłane -->
{% with messages = get_flashed_messages() %}
{% if messages %}
<div class="my-4">
{% for message in messages %}
<div class="alert alert-danger" role="alert">{{ message }}</div>
{% endfor %}
</div>
{% endif %}
{% endwith %}

<div class="row">
<div class="col-md-12">
<h3>Rozsyłka maili dla głosowania "{{name}}"</h3>
Expand Down
6 changes: 3 additions & 3 deletions sw-database/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ create table polls(
-- Czas kiedy głosowanie się kończy
-- Format: "YYYY-MM-DD hh:mm"
-- Na przykład: "2022-01-01 13:30"
-- Issue na zmianę typu na `timestamp with time zone`: https://github.com/informatyzacja-sspwr-projekty/SWv2/issues/10
closes_on_date text not null,
closes_on_date timestamptz not null,

-- Czy sw-mailsender wysyła maile z `sending_out_to`?
mailing_active boolean default false not null,
Expand All @@ -82,6 +81,7 @@ create table polls(
closed boolean default false not null,

-- Jeżeli wartość nie jest NULL, o danej godzinie głosowanie zostanie rozpocząte
-- Przy Tworzeniu wyborów domyślnie jest NULL
-- (między innymi ustawiając `mailing_active` na true)
planned_start_sending timestamp with time zone
planned_start_sending timestamptz
);