Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/video image candidate form #238

Merged
merged 6 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion app/org_eleicoes/votepeloclima/candidature/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

from django import forms
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.validators import FileExtensionValidator
from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _
from django.core.exceptions import ValidationError
from django.template.defaultfilters import filesizeformat

from django.contrib.postgres.forms import SimpleArrayField
from django_select2.forms import Select2Widget
Expand Down Expand Up @@ -185,6 +187,36 @@ def clean(self, value):
return value.replace("on-", "")


class VideoField(forms.FileField):
default_validators = [
FileExtensionValidator(
allowed_extensions=["MOV", "avi", "mp4", "webm", "mkv"],
message="Tipo de arquivo não suportado.",
)
]

def __init__(
self, *, max_size=50, max_length=None, allow_empty_file=False, **kwargs
):
super().__init__(
max_length=max_length, allow_empty_file=allow_empty_file, **kwargs
)
# 50MB
self.max_size = max_size * 1024 * 1024
self.widget.attrs["accept"] = "video/*"

def clean(self, value, initial):
value = super().clean(value, initial)
if value:
if "video" in value.content_type:
if value.size > self.max_size:
raise forms.ValidationError(
"Por favor, escolha um video com tamanho de até %s. Tamanho Atual %s"
% (filesizeformat(self.max_size), filesizeformat(value.size))
)

return value

class InlineArrayWidget(forms.MultiWidget):
template_name = "forms/widgets/inline_array.html"

Expand Down
10 changes: 6 additions & 4 deletions app/org_eleicoes/votepeloclima/candidature/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django import forms
from django.contrib.postgres.forms import SimpleArrayField
from django.template.defaultfilters import filesizeformat

from django.core.exceptions import ValidationError

from captcha.widgets import ReCaptchaV2Checkbox
Expand All @@ -9,7 +10,8 @@
StateCepField,
CityCepField,
CheckboxTextField,
InlineArrayField,
VideoField,
InlineArrayField
)


Expand Down Expand Up @@ -57,8 +59,8 @@ class Meta:


class ProfileForm(DisabledMixin, forms.Form):
video = forms.URLField(label="Vídeo", required=False)
photo = forms.URLField(label="Foto", required=False)
video = VideoField(label="Vídeo", required=False)
photo = forms.ImageField(label="Foto", required=False)
gender = forms.CharField(label="Gênero")
color = forms.CharField(label="Raça")
sexuality = forms.CharField(label="Sexualidade", required=False)
Expand Down
4 changes: 2 additions & 2 deletions app/org_eleicoes/votepeloclima/candidature/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class Candidature(models.Model):
is_collective_mandate = models.BooleanField(default=False, blank=True)
political_party = models.CharField(max_length=60)
# Step 3
video = models.FileField(upload_to="cadidatures/videos/", null=True, blank=True)
photo = models.FileField(upload_to="cadidatures/photos/", null=True, blank=True)
video = models.FileField(upload_to="candidatures/videos/", null=True, blank=True)
photo = models.FileField(upload_to="candidatures/photos/", null=True, blank=True)
gender = models.CharField(max_length=30)
color = models.CharField(max_length=30)
sexuality = models.CharField(max_length=30, null=True, blank=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
<input class="btn btn-secondary" name="wizard_goto_step" type="submit" value="{% translate "Continuar: " %}{{ next_step_title }}" />
{% endif %}
</form>
{% endblock %}
{% endblock %}
7 changes: 3 additions & 4 deletions app/org_eleicoes/votepeloclima/candidature/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
from django.views.generic import TemplateView
from django.http import HttpResponseForbidden
from django.urls import reverse_lazy,reverse
from django.core.files.storage import DefaultStorage

from formtools.wizard.views import NamedUrlSessionWizardView

from contrib.oauth.utils import send_confirmation_email
from .models import CandidatureFlow, CandidatureFlowStatus, Candidature
from .forms import register_form_list, InitialForm, FlagForm, AppointmentForm
from .forms import register_form_list, InitialForm, FlagForm, AppointmentForm, ProfileForm
from .locations_utils import get_choices


class RegisterView(NamedUrlSessionWizardView):
form_list = register_form_list
steps_hide_on_checkout = ["captcha"]
template_name = "candidature/wizard_form.html"
file_storage = DefaultStorage()

def render_done(self, form, **kwargs):
revalid = True
Expand Down Expand Up @@ -87,9 +89,6 @@ def process_step(self, form):
# print(flow.properties)
return form_data

def process_step_files(self, form):
return self.get_form_step_files(form)

def get_template_names(self):
if self.steps.current == "checkout":
return "candidature/done.html"
Expand Down
Loading