Skip to content

Commit

Permalink
Recreate accounts app
Browse files Browse the repository at this point in the history
  • Loading branch information
kytta committed May 31, 2022
1 parent 0ff40c3 commit 94a94af
Show file tree
Hide file tree
Showing 14 changed files with 225 additions and 21 deletions.
Empty file added example/accounts/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions example/accounts/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AccountsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'accounts'
8 changes: 8 additions & 0 deletions example/accounts/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.contrib.auth.models import AbstractUser
from django.forms import ModelForm


class UserForm(ModelForm):
class Meta:
model = AbstractUser
fields = ('username', 'password')
46 changes: 46 additions & 0 deletions example/accounts/menus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from django.urls import reverse

from menu import Menu, MenuItem


def profile_title(request):
"""Return a personalized title for our profile menu item
"""
# we don't need to check if the user is authenticated because our menu
# item will have a check that does that for us
name = request.user.get_full_name() or request.user

return f"{name}'s Profile"


# this item will be shown to users who are not logged in
Menu.add_item("user", MenuItem("Sign in",
reverse('accounts:sign_in'),
icon='box-arrow-in-right',
check=lambda r: not r.user.is_authenticated))

# this will only be shown to logged in users and also demonstrates how to use
# a callable for the title to return a customized title for each request
Menu.add_item("user", MenuItem(profile_title,
reverse('accounts:profile'),
icon='person-circle',
check=lambda r: r.user.is_authenticated))

# this only shows to superusers
Menu.add_item("user", MenuItem("Administration",
reverse("admin:index"),
icon='shield-lock',
separator=True,
check=lambda r: r.user.is_superuser))

Menu.add_item("user", MenuItem("Sign out",
reverse('accounts:sign_out'),
icon='box-arrow-right',
check=lambda r: r.user.is_authenticated))

Menu.add_item("main", MenuItem("GitHub",
"https://github.com/jazzband/django-simple-menu",
icon="github"))
Menu.add_item("main", MenuItem("Docs",
"https://django-simple-menu.readthedocs.io/",
icon="journal-code"))
Empty file.
21 changes: 21 additions & 0 deletions example/accounts/templates/accounts/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends 'base.html' %}

{% block title %}
Accounts | {{ block.super }}
{% endblock %}

{% block body %}
<main class="py-5">
<div class="container">
<h1>Welcome!</h1>
<p>
This is an example website for
<a href="https://github.com/jazzband/django-simple-menu">django-simple-menu</a>.
</p>
<p>
Create a superuser and a normal user, then try to sign in with
those to see, how the menu works.
</p>
</div>
</main>
{% endblock %}
17 changes: 17 additions & 0 deletions example/accounts/templates/accounts/profile.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends 'base.html' %}

{% block title %}
Your profile | {{ block.super }}
{% endblock %}

{% block body %}
<main class="py-5">
<div class="container">
<h1>{{ request.user.get_full_name|default:"" }}
(@{{ request.user.username }})</h1>
<p>
<strong>E-Mail:</strong> {{ request.user.email }}
</p>
</div>
</main>
{% endblock %}
31 changes: 31 additions & 0 deletions example/accounts/templates/accounts/sign_in.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% extends 'base.html' %}

{% block title %}
Sign in | {{ block.super }}
{% endblock %}

{% block body %}
<main class="p-4">
<div class="container w-25 mx-auto">
<form action="{% url 'accounts:sign_in' %}" method="post">
{% csrf_token %}
<h1>Sign in</h1>

<div class="mb-3">
<label for="username">Username</label>
<input type="text" class="form-control" id="username"
name="username">
</div>
<div class="mb-3">
<label for="password">Password</label>
<input type="password" class="form-control"
id="password" name="password">
</div>

<button class="btn btn-lg btn-primary" type="submit">
Sign in
</button>
</form>
</div>
</main>
{% endblock %}
12 changes: 12 additions & 0 deletions example/accounts/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.urls import path

from accounts import views

app_name = 'accounts'
urlpatterns = [
path('sign_in/', views.SignInView.as_view(), name='sign_in'),
path('sign_out/', views.SignOutView.as_view(), name='sign_out'),

path('', views.IndexView.as_view(), name='index'),
path('profile/', views.ProfileView.as_view(), name='profile'),
]
22 changes: 22 additions & 0 deletions example/accounts/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django.contrib.auth.views import LoginView, LogoutView
from django.urls import reverse_lazy
from django.views.generic import TemplateView


class SignInView(LoginView):
template_name = 'accounts/sign_in.html'

def get_success_url(self):
return reverse_lazy('accounts:profile')


class SignOutView(LogoutView):
next_page = 'accounts:index'


class IndexView(TemplateView):
template_name = 'accounts/index.html'


class ProfileView(TemplateView):
template_name = 'accounts/profile.html'
12 changes: 5 additions & 7 deletions example/example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

Expand All @@ -27,7 +26,6 @@

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
Expand All @@ -37,6 +35,10 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'menu',

'accounts',
]

MIDDLEWARE = [
Expand All @@ -54,7 +56,7 @@
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [ BASE_DIR / 'templates' ],
'DIRS': [BASE_DIR / 'templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
Expand All @@ -69,7 +71,6 @@

WSGI_APPLICATION = 'example.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

Expand All @@ -80,7 +81,6 @@
}
}


# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators

Expand All @@ -99,7 +99,6 @@
},
]


# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/

Expand All @@ -111,7 +110,6 @@

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

Expand Down
17 changes: 3 additions & 14 deletions example/example/urls.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
"""example URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import include, path

urlpatterns = [
path('admin/', admin.site.urls),

path('', include('accounts.urls')),
]
41 changes: 41 additions & 0 deletions example/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{% load menu %}
{% generate_menu %}

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}django-simple-menu example{% endblock %}</title>

<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
crossorigin="anonymous"
>
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css"
>


{% block extra_head %}{% endblock extra_head %}
</head>
<body>
<header>
<nav class="navbar navbar-expand navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="{% url 'accounts:index' %}">dsm
example app</a>
{% with menu=menus.main %}
{% include 'menu.html' %}
{% endwith %}
{% with menu=menus.user navbar_class="ms-auto" %}
{% include 'menu.html' %}
{% endwith %}

</div>
</nav>
</header>
{% block body %}{% endblock body %}
</body>
</html>
13 changes: 13 additions & 0 deletions example/templates/menu.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<ul class="navbar-nav {{ navbar_class|default:"" }}">
{% for item in menu %}
<li class="nav-item">
<a class="nav-link {% if item.selected %}active{% endif %}"
href="{{ item.url }}">
{% if item.icon %}
<i class="bi bi-{{ item.icon }}"></i>
{% endif %}
{{ item.title }}
</a>
</li>
{% endfor %}
</ul>

0 comments on commit 94a94af

Please sign in to comment.