Skip to content

Commit

Permalink
🤝 Issue 1115 update profile page options (#1125)
Browse files Browse the repository at this point in the history
* a different approach to extra content

* Fields are present, but a bunch of tests fail

* ✨ added Profile extra fields feature
This adds extra fields for profiles similar to Mastodon's
implementation

refs #1115

* ✅ Fixed Failing Test
Refs ✨ Update Profile page options #1115

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* 📝 Update profiles/admin.py

Co-authored-by: Maksudul Haque <saad.mk112@gmail.com>

* 📝 Update profiles/forms.py

Co-authored-by: Maksudul Haque <saad.mk112@gmail.com>

* 📝 Update profiles/views.py

Co-authored-by: Maksudul Haque <saad.mk112@gmail.com>

* 📝 Adds missing import I broke

* 🔥 squash migrations

* 🔥 removing migration files for migration squashing

* ⚡️ Improve performance

* 🦺 Made URL required for ExtraFields

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* 🥅 Improved UX with better error messaging

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Implemented code review changes

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Jeff Triplett <jefftriplett@users.noreply.github.com>
Co-authored-by: Maksudul Haque <saad.mk112@gmail.com>
Co-authored-by: Jeff Triplett <jeff.triplett@gmail.com>
  • Loading branch information
5 people committed Apr 16, 2024
1 parent 4bdc4ff commit 3bb77fb
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 11 deletions.
8 changes: 7 additions & 1 deletion profiles/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.contrib import admin
from reversion.admin import VersionAdmin

from profiles.models import Profile
from profiles.models import Profile, ExtraField


@admin.display(description="User username")
Expand All @@ -19,3 +19,9 @@ class ProfileAdmin(VersionAdmin):
search_fields = ("user__username", "github_account", "user__email", "email")
list_display = ("github_account", "email", username, user_email)
raw_id_fields = ["user"]


@admin.register(ExtraField)
class ExtraFieldAdmin(VersionAdmin):
list_select_related = ["profile"]
raw_id_fields = ["profile"]
29 changes: 28 additions & 1 deletion profiles/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,33 @@
from crispy_forms.layout import HTML, ButtonHolder, Fieldset, Layout, Submit
from django import forms

from profiles.models import Profile
from profiles.models import Profile, ExtraField


class ExtraFieldForm(forms.ModelForm):
class Meta:
model = ExtraField
fields = (
"label",
"url",
)
widgets = {
"label": forms.TextInput(
attrs={"placeholder": "Label", "class": "textinput form-control"},
),
"url": forms.TextInput(
attrs={"placeholder": "URL", "class": "textinput form-control"}
),
}


ExtraFieldFormSet = forms.inlineformset_factory(
Profile,
ExtraField,
form=ExtraFieldForm,
extra=4,
max_num=4,
)


class ProfileForm(forms.ModelForm):
Expand All @@ -22,6 +48,7 @@ def __init__(self, *args, **kwargs):
"bitbucket_url",
"gitlab_url",
),
HTML("""{{ extra_fields_formset }}"""),
ButtonHolder(Submit("edit", "Edit", css_class="btn btn-default")),
)

Expand Down
33 changes: 33 additions & 0 deletions profiles/migrations/0005_add_extrafield.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 4.2.9 on 2024-04-15 22:39

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django_extensions.db.fields


class Migration(migrations.Migration):

replaces = [('profiles', '0005_add_extrafields'), ('profiles', '0006_alter_extrafields_value'), ('profiles', '0007_rename_extrafields_extrafield_and_more')]

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('profiles', '0004_remove_profile_google_code_url_profile_gitlab_url_and_more'),
]

operations = [
migrations.CreateModel(
name='ExtraField',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', django_extensions.db.fields.CreationDateTimeField(auto_now_add=True, verbose_name='created')),
('modified', django_extensions.db.fields.ModificationDateTimeField(auto_now=True, verbose_name='modified')),
('label', models.CharField(max_length=256)),
('url', models.URLField(max_length=256)),
('profile', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='profiles.profile')),
],
options={
'abstract': False,
},
),
]
9 changes: 9 additions & 0 deletions profiles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,12 @@ def can_edit_grid_element(self):
if getattr(settings, "RESTRICT_GRID_EDITORS", False):
return self.user.has_perm("grid.change_element")
return True


class ExtraField(BaseModel):
profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
label = models.CharField(max_length=256)
url = models.URLField(max_length=256)

def __str__(self):
return f"{self.profile} - {self.key}"
10 changes: 9 additions & 1 deletion profiles/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_view_not_loggedin(self):
response = self.client.get(url)
self.assertContains(response, "Profile for user")

def test_edit(self):
def test_edit_ryan(self):
self.assertTrue(
self.client.login(username=self.user.username, password=STOCK_PASSWORD)
)
Expand All @@ -48,6 +48,14 @@ def test_edit(self):
data = {
"bitbucket_url": "zerg",
"gitlab_url": "zerg",
"extrafield_set-TOTAL_FORMS": 2,
"extrafield_set-INITIAL_FORMS": 0,
"extrafield_set-MIN_NUM_FORMS": 0,
"extrafield_set-MAX_NUM_FORMS": 4,
"extrafield-0-label": "Key1",
"extrafield-0-url": "Value1",
"extrafield-1-label": "Key2",
"extrafield-1-url": "Value2",
}
response = self.client.post(url, data, follow=True)
self.assertContains(response, "Profile for user")
Expand Down
40 changes: 32 additions & 8 deletions profiles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from django.views.generic import RedirectView
from django.views.generic.edit import UpdateView

from profiles.forms import ProfileForm
from profiles.models import Profile
from profiles.forms import ProfileForm, ExtraFieldFormSet
from profiles.models import Profile, ExtraField

# from social_auth.signals import pre_update
# from social_auth.backends.contrib.github import GithubBackend
Expand All @@ -24,10 +24,12 @@ def profile_detail(request, github_account, template_name="profiles/profile.html
except MultipleObjectsReturned:
profile = Profile.objects.filter(github_account=github_account).latest("pk")

extra_fields = ExtraField.objects.filter(profile=profile)

return render(
request,
template_name,
{"local_profile": profile, "user": profile.user},
{"local_profile": profile, "user": profile.user, "extra_fields": extra_fields},
)


Expand All @@ -39,12 +41,34 @@ class ProfileEditUpdateView(LoginRequiredMixin, UpdateView):
def get_object(self):
return self.request.user.profile

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
if self.request.POST:
context["extra_fields_formset"] = ExtraFieldFormSet(
self.request.POST, instance=self.object
)
else:
context["extra_fields_formset"] = ExtraFieldFormSet(instance=self.object)
return context

def form_valid(self, form):
form.save()
messages.add_message(self.request, messages.INFO, "Profile Saved")
return HttpResponseRedirect(
reverse("profile_detail", kwargs={"github_account": self.get_object()})
)
context = self.get_context_data()
formset = context["extra_fields_formset"]
if formset.is_valid():
self.object = form.save()
formset.instance = self.object
formset.save()
messages.add_message(self.request, messages.INFO, "Profile Saved")
return HttpResponseRedirect(
reverse("profile_detail", kwargs={"github_account": self.get_object()})
)
else:
messages.add_message(
self.request, messages.ERROR, "There are errors in the form"
)
context["form"] = form
context["extra_fields_formset"] = formset
return self.render_to_response(context)


def github_user_update(sender, **kwargs):
Expand Down
9 changes: 9 additions & 0 deletions templates/profiles/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ <h2>{% blocktrans %}<span class="fn nickname">{{ user }}</span>{% endblocktrans
<span class="fn"><a href="https://gitlab.com/{{ local_profile.gitlab_url }}" target="_blank">{{ local_profile.gitlab_url }}</a></span>
</p>
{% endif %}

{% if extra_fields%}
{% for field in extra_fields%}
<p><b>{{ field.label }}:</b> {{ field.url }}</p>
{% endfor%}
{% endif %}



</div>

<div class="col-xs-5">
Expand Down

0 comments on commit 3bb77fb

Please sign in to comment.