Commit 4cc5c0cc authored by Staś Kycia's avatar Staś Kycia
Browse files

security patch: protected summernote and fixed local votes form

parent 89284d84
Loading
Loading
Loading
Loading
+16 −10
Changes for elections/models.py: 16 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@ from django.core.exceptions import ValidationError
from django.db import models
from django.utils import timezone
from django.utils.text import slugify
from django.db import transaction
from django.db.models import F


class Election(models.Model):
@@ -101,18 +103,22 @@ class Election(models.Model):
        return super().save(*args, **kwargs)

    def cast_vote(self, voter, choice, terminal=None):
        from vote.models import Participation
        with transaction.atomic():
            if choice == "invalid":
            self.invalid_votes += 1
            self.save()
                type(self).objects.filter(pk=self.pk).update(invalid_votes=F('invalid_votes') + 1)
                self.refresh_from_db(fields=['invalid_votes'])
            else:
            option = Option.objects.get(pk=choice)
            option.votes += 1
            option.save()

        from vote.models import Participation

        participation = Participation(voter=voter, election=self, assigned_by=(terminal.assigned_by if terminal else None), is_remote=(False if terminal is not None else True))
        participation.save()
                updated_count = Option.objects.filter(pk=choice).update(votes=F('votes') + 1)
                if updated_count == 0:
                    raise Option.DoesNotExist("The selected option does not exist.")

            Participation.objects.create(
                voter=voter,
                election=self,
                assigned_by=terminal.assigned_by if terminal else None,
                is_remote=(terminal is None)
            )

    class Meta:
        verbose_name = "głosowanie"
+1 −1
Changes for hybrid/admin.py: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -266,7 +266,7 @@ class EnterVotesAdmin(admin.ModelAdmin):

        options_queryset = election.options.all()
        expected_votes_count = Participation.objects.filter(
            election=election, is_remote=False
            election=election, is_remote=False, card_issued_by__isnull=False,
        ).count()

        if not election.is_hybrid or election.is_active:
+18 −0
Changes for stats/migrations/0005_alter_electionreport_invalid_votes_count.py: 18 added lines, 0 removed lines.
Original line number Diff line number Diff line
# Generated by Django 6.0.9 on 2026-10-19 10:18

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('stats', '0004_alter_electionreport_generated_at'),
    ]

    operations = [
        migrations.AlterField(
            model_name='electionreport',
            name='invalid_votes_count',
            field=models.IntegerField(blank=True, null=True, verbose_name='liczba głosów nieważnych'),
        ),
    ]
+2 −2
Changes for stats/models.py: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ class ElectionReport(models.Model):
        blank=False, verbose_name="liczba głosów zdalnych"
    )
    invalid_votes_count = models.IntegerField(
        blank=True, verbose_name="liczba głosów nieważnych"
        blank=True, null=True, verbose_name="liczba głosów nieważnych"
    )

    @property
@@ -35,7 +35,7 @@ class ElectionReport(models.Model):

    @property
    def is_valid(self):
        votes_from_options = self.invalid_votes_count
        votes_from_options = self.invalid_votes_count or 0
        for option in self.option_votes.all():
            votes_from_options += option.votes
        if (
+4 −0
Changes for wybory/apps.py: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -14,3 +14,7 @@ class WyborySummernoteConfig(DjangoSummernoteConfig):

        Attachment._meta.verbose_name = "Załącznik"
        Attachment._meta.verbose_name_plural = "Załączniki"


def check_if_user_can_upload(request):
    return request.user.is_authenticated and (request.user.is_superuser or (request.user.has_perm("pages.add_page") or request.user.has_perm("pages.change_page")))
 No newline at end of file
Loading