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

User option to subscribe to emails and non-unique order IDs #42

Merged
merged 3 commits into from
Jul 16, 2019
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
19 changes: 19 additions & 0 deletions src/problems/migrations/0007_nonunique-order-id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.1.5 on 2019-07-16 12:59

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('problems', '0006_reorder-problems'),
]

operations = [
migrations.AlterField(
model_name='problem',
name='order_id',
field=models.IntegerField(null=True, validators=[django.core.validators.MinValueValidator(0)]),
),
]
2 changes: 1 addition & 1 deletion src/problems/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Problem(models.Model):
id = models.AutoField(primary_key=True)

# The order of the problems in the problem list/table.
order_id = models.IntegerField(unique=True, validators=[MinValueValidator(0)], editable=True, null=True)
order_id = models.IntegerField(unique=False, validators=[MinValueValidator(0)], editable=True, null=True)

# Unique name for database and urls, e.g. "earthquake-epicenters".
name = models.CharField(unique=True, max_length=256, editable=True)
Expand Down
2 changes: 1 addition & 1 deletion src/users/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class EditUserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ['display_name', 'about', 'birthday', 'country', 'location']
fields = ['display_name', 'about', 'birthday', 'country', 'location', 'subscribe_to_emails']
widgets = {
'country': CountrySelectWidget(
layout='{widget}<span class="icon"><img class="country-select-flag" id="{flag_id}" style="margin: 6px 4px 0" src="{country.flag}"></span>'
Expand Down
18 changes: 18 additions & 0 deletions src/users/migrations/0015_subscribe_to_email_option.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.1.5 on 2019-07-16 13:18

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('users', '0014_fixed_default_avatar_filepath_v5'),
]

operations = [
migrations.AddField(
model_name='userprofile',
name='subscribe_to_emails',
field=models.BooleanField(default=True, help_text='Subscribe to emails.'),
),
]
5 changes: 5 additions & 0 deletions src/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

from django_countries.fields import CountryField


def avatar_file_name(instance, filename):
# We want a relative URL, not an absolute URL so I didn't use '/'.join(). Relative to MEDIA_ROOT.
return "users/" + instance.user.username + "/" + filename


class UserProfile(models.Model):
id = models.AutoField(primary_key=True)
user = models.OneToOneField(User, on_delete=models.CASCADE, unique=True)
Expand All @@ -34,8 +36,11 @@ class UserProfile(models.Model):
validators=[MaxLengthValidator(50)],
help_text="Maximum length of 50 characters.")

subscribe_to_emails = models.BooleanField(default=True, help_text="Subscribe to emails.")

# The default avatar actually resides in media/static/img. I couldn't get it to link to the actual static/...
avatar = models.ImageField(upload_to=avatar_file_name, max_length=100, default="static/img/default_avatar.png")

problems_solved = models.IntegerField(default=0, validators=[MinValueValidator(0)])
submissions_made = models.IntegerField(default=0, validators=[MinValueValidator(0)])

Expand Down
39 changes: 23 additions & 16 deletions src/users/templates/users/editprofile.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,36 @@ <h4 class="subtitle is-5 has-text-centered is-italic"><strong>{{ profile.problem
{% for field in form.visible_fields %}
<div class="field">
<label class="label" for="{{ field.id_for_label }}">{{ field.label }}</label>
<div class="control has-icons-left">
<div class="control has-icons-left">
{% if field.name == 'country' %}
<div class="select">{{ field }}</div>
{% elif field.name == 'subscribe_to_emails' %}
{{ field }} Receive emails about new problems and features.
{% else %}
{{ field | add_class:'input' }}
{% endif %}

<span class="icon is-small is-left">
{% if field.name == 'display_name' %}
<i class="fas fa-user"></i>
{% elif field.name == 'about' %}
<i class="fas fa-pen"></i>
{% elif field.name == 'birthday' %}
<i class="fas fa-calendar"></i>
{% elif field.name == 'location' %}
<i class="fas fa-location-arrow"></i>
{% endif %}
</span>
{% if field.name != 'subscribe_to_emails' %}
<span class="icon is-small is-left">
{% if field.name == 'display_name' %}
<i class="fas fa-user"></i>
{% elif field.name == 'about' %}
<i class="fas fa-pen"></i>
{% elif field.name == 'birthday' %}
<i class="fas fa-calendar"></i>
{% elif field.name == 'location' %}
<i class="fas fa-location-arrow"></i>
{% endif %}
</span>
{% endif %}
</div>
<p class="help">{{ field.help_text }}</p>
{% for error in field.errors %}
<p class="help is-danger">{{ error }}</p>
{% endfor %}

{% if field.name != 'subscribe_to_emails' %}
<p class="help">{{ field.help_text }}</p>
{% for error in field.errors %}
<p class="help is-danger">{{ error }}</p>
{% endfor %}
{% endif %}
</div>
{% endfor %}
<button class="button is-primary" type="submit">Save changes</button>
Expand Down