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

WIP: Move forms to accounts app #641

Closed
wants to merge 1 commit into from
Closed
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
39 changes: 32 additions & 7 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
{
"recommendations": [
"ms-python.python",
"PeterJausovec.vscode-docker",
"thebarkman.vscode-djaneiro",
"Zignd.html-css-class-completion"
]
}
"recommendations": [
// Python: Linting, Debugging (multi-threaded, remote), Intellisense,
// code formatting, refactoring, unit tests, snippets, and more.
"ms-python.python",

// Django: Beautiful syntax and scoped snippets
"batisteo.vscode-django",

// Docker: Adds syntax highlighting, commands, hover tips, and linting
// for Dockerfile and docker-compose files
"PeterJausovec.vscode-docker",

// IntelliSense for CSS class names in HTML
"Zignd.html-css-class-completion"

// Auto Rename Tag: Auto rename paired HTML/XML tag
"formulahendry.auto-rename-tag",

// Beautify: Beautify code in place for VS Code
"hookyqr.beautify",

// CSS Peek: Allow peeking to css ID and class strings as definitions
// from html files to respective CSS.Allows peek and goto definition.
"pranaygp.vscode-css-peek",

// HTMLHint: A Static Code Analysis Tool for HTML
"mkaufman.htmlhint",

// HTML CSS Support: CSS support for HTML documents
"ecmel.vscode-html-css"
]
}
84 changes: 46 additions & 38 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,49 @@
{
"editor.trimAutoWhitespace": true,
"files.exclude": {
"**/.git": true,
"**/.DS_Store": true,
"**/*.pyc": true,
"**/__pycache__": true,
"staticfiles": true,
"editor.trimAutoWhitespace": true,
"files.exclude": {
"**/.git": true,
"**/.DS_Store": true,
"**/*.pyc": true,
"**/__pycache__": true,
"staticfiles": true,
},
"files.associations": {
"**/templates/*.html": "django-html",
"**/templates/*": "django-txt",
"**/requirements{/**,*}.{txt,in}": "pip-requirements"
},
"emmet.includeLanguages": {
"django-html": "html"
},
"python.linting.enabled": true,
"python.linting.ignorePatterns": [
".vscode/*.py",
"**/site-packages/**/*.py",
"**/migrations/**",
],
"python.linting.pylintEnabled": false,
"python.linting.pep8Enabled": true,
"python.linting.pep8Path": "pycodestyle",
"python.linting.pep8Args": [
"--max-line-length=120",
],
"[python]": {
"editor.rulers": [120],
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
"files.associations": {
"*.html": "django-html",
},
"[git-commit]": {
"editor.rulers": [72],
},
"[json]": {
"editor.quickSuggestions": {
"strings": true,
},
"python.linting.enabled": true,
"python.linting.ignorePatterns": [
".vscode/*.py",
"**/site-packages/**/*.py",
"**/migrations/**",
],
"python.linting.pylintEnabled": false,
"python.linting.pep8Enabled": true,
"python.linting.pep8Path": "pycodestyle",
"python.linting.pep8Args": [
"--max-line-length=120",
],
"[python]": {
"editor.rulers": [120],
},
"[git-commit]": {
"editor.rulers": [72],
},
"[json]": {
"editor.quickSuggestions": {
"strings": true,
},
},
"[markdown]": {
"editor.wordWrap": "on",
"editor.quickSuggestions": false,
"editor.trimAutoWhitespace": false,
},
}
},
"[markdown]": {
"editor.wordWrap": "on",
"editor.quickSuggestions": false,
"editor.trimAutoWhitespace": false,
},
}
224 changes: 224 additions & 0 deletions accounts/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
from django import forms

import html5.forms.widgets as html5_widgets

from coderdojochi.models import CDCUser, Guardian, Mentor, RaceEthnicity


class CDCModelForm(forms.ModelForm):
# strip leading or trailing whitespace
def _clean_fields(self):
for name, field in list(self.fields.items()):
# value_from_datadict() gets the data from the data dictionaries.
# Each widget type knows how to retrieve its own data, because some
# widgets split data over several HTML fields.
value = field.widget.value_from_datadict(
self.data,
self.files,
self.add_prefix(name)
)

try:
if isinstance(field, forms.FileField):
initial = self.initial.get(name, field.initial)
value = field.clean(value, initial)
else:
if isinstance(value, str):
# regex normalizes carriage return
# and cuts them to two at most
value = re.sub(r'\r\n', '\n', value)
value = re.sub(r'\n{3,}', '\n\n', value)
value = field.clean(value.strip())
else:
value = field.clean(value)

self.cleaned_data[name] = value

if hasattr(self, f"clean_{name}"):
value = getattr(self, f"clean_{name}")()
self.cleaned_data[name] = value

except forms.ValidationError as e:
self.add_error(name, e)

class Meta:
model = CDCUser
fields = ('first_name', 'last_name')


class GuardianForm(CDCModelForm):
phone = forms.CharField(
widget=forms.TextInput(
attrs={
'placeholder': 'Phone Number',
'class': 'form-control'
}
),
label='Phone Number'
)

zip = forms.CharField(
widget=forms.TextInput(
attrs={
'placeholder': 'Zip Code',
'class': 'form-control'
}
),
label='Zip Code'
)

gender = forms.CharField(
widget=forms.TextInput(
attrs={
'placeholder': '',
'class': 'form-control'
}
),
label='Gender',
required=True
)

race_ethnicity = forms.ModelMultipleChoiceField(
widget=forms.SelectMultiple,
queryset=RaceEthnicity.objects.filter(is_visible=True),
required=True
)

birthday = forms.CharField(
widget=forms.DateInput(
attrs={
'class': 'form-control'
}
),
required=True
)

class Meta:
model = Guardian
fields = ('phone', 'zip', 'gender', 'race_ethnicity', 'birthday')


class MentorForm(CDCModelForm):
bio = forms.CharField(
widget=forms.Textarea(
attrs={
'placeholder': 'Short Bio',
'class': 'form-control',
'rows': 4
}
),
label='Short Bio',
required=False
)

gender = forms.CharField(
widget=forms.TextInput(
attrs={
'placeholder': '',
'class': 'form-control'
}
),
label='Gender',
required=True
)

race_ethnicity = forms.ModelMultipleChoiceField(
widget=forms.SelectMultiple,
queryset=RaceEthnicity.objects.filter(is_visible=True),
required=True
)

birthday = forms.CharField(
widget=html5_widgets.DateInput(
attrs={
'class': 'form-control'
}
),
required=True
)

work_place = forms.CharField(
widget=forms.TextInput(
attrs={
'placeholder': '',
'class': 'form-control'
}
),
label='Work Place',
required=False
)

phone = forms.CharField(
widget=forms.TextInput(
attrs={
'placeholder': '',
'class': 'form-control'
}
),
label='Phone',
required=False
)

home_address = forms.CharField(
widget=forms.TextInput(
attrs={
'placeholder': '',
'class': 'form-control'
}
),
label='Home Address',
required=False
)

class Meta:
model = Mentor
fields = ('bio', 'avatar', 'gender', 'race_ethnicity', 'birthday', 'phone', 'home_address', 'work_place')

def clean_avatar(self):
avatar = self.cleaned_data['avatar']

if avatar is None:
return avatar

try:
w, h = get_image_dimensions(avatar)

# validate dimensions
max_width = max_height = 1000
if w > max_width or h > max_height:
raise forms.ValidationError(
f'Please use an image that is {max_width} x {max_height}px or smaller.'
)

min_width = min_height = 500
if w < min_width or h < min_height:
raise forms.ValidationError(
f'Please use an image that is {min_width} x {min_height}px or larger.'
)

# validate content type
main, sub = avatar.content_type.split('/')
if (
not (
main == 'image' and
sub in ['jpeg', 'pjpeg', 'gif', 'png']
)
):
raise forms.ValidationError(
'Please use a JPEG, GIF or PNG image.'
)

# validate file size
if len(avatar) > (2000 * 1024):
raise forms.ValidationError(
'Avatar file size may not exceed 2MB.'
)

except AttributeError:
"""
Handles case when we are updating the user profile
and do not supply a new avatar
"""
pass

return avatar
Loading