Skip to content

Commit

Permalink
Merge pull request #400 from alllexxx1/feature_branch
Browse files Browse the repository at this point in the history
[#392] Add settings section for a user
  • Loading branch information
sgmdlt authored Mar 25, 2024
2 parents dcf5d79 + a1fd95d commit 0c0a590
Show file tree
Hide file tree
Showing 13 changed files with 543 additions and 411 deletions.
9 changes: 9 additions & 0 deletions auth/forms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django import forms as django_forms
from django.contrib.auth import forms

from auth.models import SiteUser
Expand All @@ -17,3 +18,11 @@ class UserChangeForm(forms.UserChangeForm):
class Meta(forms.UserChangeForm.Meta):
model = SiteUser
fields = ('username', 'email')


class UserTokenForm(django_forms.ModelForm):
"""User GitHub token change form."""

class Meta: # noqa: WPS306
model = SiteUser
fields = ['github_token']
19 changes: 19 additions & 0 deletions auth/migrations/0006_siteuser_github_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.11 on 2024-03-20 21:07

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("custom_auth", "0005_alter_siteuser_groups"),
]

operations = [
migrations.AddField(
model_name="siteuser",
name="github_token",
field=models.CharField(
blank=True, max_length=100, null=True, verbose_name="GitHub token"
),
),
]
7 changes: 7 additions & 0 deletions auth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
class SiteUser(AbstractUser):
"""Model representing a user account."""

github_token = models.CharField(
_("GitHub token"),
max_length=100,
blank=True,
null=True,
)

class Meta(object):
verbose_name = _("User")
verbose_name_plural = _("Users")
Expand Down
5 changes: 5 additions & 0 deletions contributors/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
app_name = 'contributors'
urlpatterns = [
path('', views.home.HomeView.as_view(), name='home'),
path(
'<slug:slug>/settings/account/edit',
views.user_settings.ChangeTokenView.as_view(),
name='user_settings',
),
path(
'organizations/',
views.organizations.ListView.as_view(),
Expand Down
1 change: 1 addition & 0 deletions contributors/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
pull_requests,
repositories,
repository,
user_settings,
webhook,
)
35 changes: 35 additions & 0 deletions contributors/views/mixins.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import operator
from functools import reduce

from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.core.paginator import Paginator
from django.db.models import Count, F, Q, Sum, Window # noqa: WPS347
from django.db.models.functions import Coalesce, RowNumber
from django.shortcuts import redirect
from django.views.generic.list import MultipleObjectMixin
from django_cte import With

Expand Down Expand Up @@ -210,3 +213,35 @@ def get_context_data(self, **kwargs):
),
)
return context


class AuthRequiredMixin(LoginRequiredMixin):
"""Verify that the current user is authenticated."""

not_auth_msg = None
redirect_url = None

def dispatch(self, request, *args, **kwargs):
"""Give permission if user is authenticated, deny otherwise."""
if not request.user.is_authenticated:
messages.warning(request, self.not_auth_msg)
return redirect(self.redirect_url)
return super().dispatch(request, *args, **kwargs)


class PermissionRequiredMixin(UserPassesTestMixin):
"""Verify that the current user has all specified permissions."""

no_permission_msg = None
redirect_url = None

def test_func(self):
"""Check user permissions."""
requested_user = self.kwargs.get('slug')
auth_user = self.request.user.contributor.login
return requested_user == auth_user

def handle_no_permission(self):
"""Redirect user without permissions."""
messages.warning(self.request, self.no_permission_msg)
return redirect(self.redirect_url)
52 changes: 52 additions & 0 deletions contributors/views/user_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from django.contrib import messages
from django.shortcuts import redirect
from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _
from django.views.generic import TemplateView

from auth.forms import UserTokenForm
from contributors.views.mixins import (
AuthRequiredMixin,
PermissionRequiredMixin,
)


class ChangeTokenView(
AuthRequiredMixin,
PermissionRequiredMixin,
TemplateView,
):
"""Changing user git_hub token page view."""

template_name = 'user_settings.html'
not_auth_msg = _('Please log in with your GitHub')
no_permission_msg = (
_("You haven't got permission to access this section")
)
redirect_url = reverse_lazy('contributors:home')

def get_context_data(self, **kwargs):
"""Add additional context for the settings."""
context = super().get_context_data(**kwargs)
context['user'] = self.request.user
return context

def post(self, request, *args, **kwargs):
"""Handle form submission."""
form = UserTokenForm(request.POST)
if form.is_valid():
user = request.user
github_token = form.cleaned_data['github_token']
user.github_token = github_token
user.save()
messages.success(request, _('GitHub token changed successfully'))
return redirect(reverse_lazy(
'contributors:user_settings',
kwargs={'slug': kwargs.get('slug')},
))

messages.error(request, _('An error occurred. Please try again'))
return redirect(reverse_lazy(
'contributors:user_settings',
kwargs={'slug': kwargs.get('slug')},
))
Binary file modified locale/ru/LC_MESSAGES/django.mo
Binary file not shown.
Loading

0 comments on commit 0c0a590

Please sign in to comment.