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

Closes #1451 Added user categories to profile and settings #1459

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions project/accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,29 @@ class ProfileEditForm(forms.ModelForm):

class Meta:
model = Profile

fields = [
"first_name",
"last_name",
"about_me",
"profile_image",
"username",
"email",
"categories",
]
widgets = {"categories": forms.CheckboxSelectMultiple()}

first_name = forms.CharField(label="First Name", max_length=63, required=False)
last_name = forms.CharField(label="Last Name", max_length=63, required=False)
about_me = forms.CharField(label="About Me", max_length=511, required=False)
email = forms.EmailField(label="Email", disabled=True)
username = forms.CharField(label="Username", disabled=True)
profile_image = forms.ImageField(required=False)
# categories = forms.ModelMultipleChoiceField(
brylie marked this conversation as resolved.
Show resolved Hide resolved
# queryset=Category.objects.all(),
# required=False,
# widget=forms.CheckboxSelectMultiple
# )


class UpdatePassword(forms.ModelForm):
Expand Down
23 changes: 14 additions & 9 deletions project/accounts/templates/accounts/account.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@

<!-- Prevent users from following themselves. -->
{% if not profile == request.user.profile %}
{% if profile not in request.user.profile.following.all %}
<a href="{% url 'profile-follow' profile.user.username %}" class="waves-effect waves-light btn follow-btn" id="sidebar-follow-btn">
follow
</a>
{% else %}
<a href="{% url 'profile-unfollow' profile.user.username %}" class="waves-effect waves-light btn follow-btn" id="sidebar-follow-btn">
unfollow
</a>
{% endif %}
{% if profile not in request.user.profile.following.all %}
<a href="{% url 'profile-follow' profile.user.username %}" class="waves-effect waves-light btn follow-btn" id="sidebar-follow-btn">
follow
</a>
{% else %}
<a href="{% url 'profile-unfollow' profile.user.username %}" class="waves-effect waves-light btn follow-btn" id="sidebar-follow-btn">
unfollow
</a>
{% endif %}
{% endif %}

<div class="user-info section">
Expand All @@ -45,6 +45,11 @@
<div class="section-title">ABOUT ME</div>
<div class="divider"></div>
<div class="about-me">{{ profile.about_me }}</div>
<div class="divider"></div>
<div class="section-title">CATEGORIES</div>
{% for category in categories %}
<div class="about-me">{{ category.name }}</div>
{% endfor %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to loop over all of the categories here, but would instead loop over the profile.categories and list them horizontally.

Since div is a block element, it causes the categories to stack. Instead of wrapping each category in its own div display the categories in a paragraph or span of text, separated by commas.

{% for category in profile.categories %}
{{ category }},
{% endfor %}

Notice that the above code lists all the profile categories separated by commas. For bonus points, we can later figure out how to make it, so the last category in the list doesn't have a trailing comma.

References

Check out the following documents for further info related to this comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's fine @brylie but, I already performed the logic in the backend

def get(self, request, username=None):
        profile = get_object_or_404(Profile, user__username=username)
        categories = profile.categories.all()

or will you'll prefer I do it using the Django templating syntax?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the profile object already available in the template would require less code since the categories property is already part of the profile.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @brylie please I'm getting a server error after implementing this
here is the error message
TypeError: 'ManyRelatedManager' object is not iterable
any suggestions on what I'm missing out?

Copy link
Member

@brylie brylie Nov 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That error seems common enough to find an answer via Google or Stack Overflow. Let me know what search results you get and what you tried to resolve the error. It would also help to see what the exact line of code is that produces the error.

It may be worth considering that the code I suggested during the code review won't work without modification since I was going from memory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @brylie I just pushed my code for review, I also helped fix the last trailing comma you mentioned, with the best logic I could come up with for now before we find something more permanent.

Also, I don't remember you mentioning anything about the issue I raised in the settings.html file

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add a comment in settings.html

</div>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions project/accounts/templates/accounts/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@
</div>
</div>
<div class="row center">
{%if not readonly %}
{% if not readonly %}
<button class='btn' type='submit' value="Save Changes">Save Changes</button>
{% endif%}
{% endif %}
</div>
</form>
<div class="row center">
Expand All @@ -86,7 +86,7 @@
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function(event) {
const button = document.getElementById('delete-account-button');

button.addEventListener('click', (event) => {
// Confirm user intent before committing to expunge
if (window.confirm('Are you certain you want to expunge your information? This action is irreversible.')) {
Expand Down
3 changes: 3 additions & 0 deletions project/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def get_initial(self):
"last_name": profile.last_name or None,
"about_me": profile.about_me or None,
"profile_image": profile.profile_image or None,
"categories": profile.categories.add() or None,
}
)
return super(SettingsView, self).get_initial()
Expand All @@ -178,12 +179,14 @@ class UserProfileView(LoginRequiredMixin, View):

def get(self, request, username=None):
profile = get_object_or_404(Profile, user__username=username)
categories = profile.categories.all()

return TemplateResponse(
request,
"account.html",
{
"profile": profile,
"categories": categories,
},
)

Expand Down