Skip to content

Commit

Permalink
add user accounts system
Browse files Browse the repository at this point in the history
  • Loading branch information
sirodoht committed Jun 10, 2024
1 parent 961b583 commit 0411b4b
Show file tree
Hide file tree
Showing 19 changed files with 252 additions and 29 deletions.
9 changes: 9 additions & 0 deletions main/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm as DjUserCreationForm


class UserCreationForm(DjUserCreationForm):
class Meta:
model = get_user_model()
fields = ["username", "email"]
9 changes: 8 additions & 1 deletion main/templates/assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ body { font-family: monospace; line-height: 1.3; }
a { color: black; }

/* forms */
form { margin: 0 16px; }
label { display: block; margin-bottom: 2px; }

input { font-family: monospace; }
input[type="submit"] { cursor: pointer; }

.form-inline { display: inline-block; }
.form-inline input[type="submit"] { border: none; background: unset; padding: 0; }
.form-inline input[type="submit"]:hover { text-decoration: underline; }

aside { padding: 8px 16px; }
aside:has(.alert-error) { border: 8px solid #ff00005e; }
aside:has(.alert-success) { border: 8px solid #0080005e; }
Expand Down
2 changes: 1 addition & 1 deletion main/templates/main/contact_create.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{% block content %}
<main>
<form method="post">
<form method="post" style="margin-left: 8px;">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save</button>
Expand Down
2 changes: 1 addition & 1 deletion main/templates/main/domain_create.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{% block content %}
<main>
<form method="post">
<form method="post" style="margin-left: 8px;">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save</button>
Expand Down
20 changes: 20 additions & 0 deletions main/templates/main/domain_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends 'main/layout.html' %}

{% block content %}
<main>
{% for domain in domain_list %}
<div class="cell">
<div>
{{ domain.domain_name }}
<small>
<a href="">(visit ↗)</a>
</small>
</div>
<div>{{ domain.created_at }}</div>
<div>
<a href="">manage</a>
</div>
</div>
{% endfor %}
</main>
{% endblock %}
15 changes: 2 additions & 13 deletions main/templates/main/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,8 @@

{% block content %}
<main>
{% for domain in domain_list %}
<div class="cell">
<div>
{{ domain.domain_name }}
<small>
<a href="">(visit ↗)</a>
</small>
</div>
<div>{{ domain.created_at }}</div>
<div>
<a href="">manage</a>
</div>
<div style="margin-left: 8px;">
<h1>Hi!</h1>
</div>
{% endfor %}
</main>
{% endblock %}
20 changes: 17 additions & 3 deletions main/templates/main/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,31 @@ <h1>
</h1>
<div></div>
<div id="controls">
{% if request.user.is_authenticated %}
<div class="controls-item" style="border-right: 1px solid black;">
<a href="">sirodoht</a>
<a href="">{{ request.user.username }}</a>
</div>
<div class="controls-item">
<a href="">logout</a>
<div style="display: flex; align-items: center; padding: 20px;">
<form method="post" action="{% url 'logout' %}" class="form-inline">
{% csrf_token %}
<input type="submit" value="logout">
</form>
</div>
</div>
{% else %}
<div class="controls-item">
<a href="{% url 'login' %}">login</a>
</div>
<div class="controls-item" style="border-left: 1px solid black;">
<a href="{% url 'user_create' %}">sign up</a>
</div>
{% endif %}
</div>
</nav>

<div style="padding: 8px; border: 1px solid black;">
[ <a href="{% url 'index' %}">Domains</a>
[ <a href="{% url 'domain_list' %}">Domains</a>
| <a href="{% url 'domain_create' %}">Register Domain</a>
| <a href="{% url 'contact_list' %}">Contacts</a>
| <a href="{% url 'contact_create' %}">Add Contact</a>
Expand Down
14 changes: 14 additions & 0 deletions main/templates/main/user_create.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends 'main/layout.html' %}

{% block content %}
<main>
<div style="margin-left: 8px;">
<h1>Sign up</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save</button>
</form>
</div>
</main>
{% endblock %}
32 changes: 32 additions & 0 deletions main/templates/registration/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{% extends 'main/layout.html' %}

{% block title %}Log in{% endblock %}

{% block content %}
<main>
<div style="margin-left: 8px;">
{% if next %}
{% if user.is_authenticated %}
<p class="alert">
Your account doesn't have access to this page.
To proceed please login with an account that has access.
</p>
{% endif %}
{% endif %}

<h1>Log in</h1>
<form method="post">
{{ form.as_p }}
{% csrf_token %}
<input type="submit" value="login">
</form>

<p>
<a href="{% url 'password_reset' %}">forgot password?</a>
</p>
<p>
<a href="{% url 'user_create' %}">sign up?</a>
</p>
</div>
</main>
{% endblock %}
12 changes: 12 additions & 0 deletions main/templates/registration/password_change_done.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends 'main/layout.html' %}

{% block title %}Password change successful{% endblock %}

{% block content %}
<main>
<div style="margin-left: 8px;">
<h1>Password change successful</h1>
<p>Your password was changed.</p>
</div>
</main>
{% endblock %}
20 changes: 20 additions & 0 deletions main/templates/registration/password_change_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends 'main/layout.html' %}

{% block title %}Password change{% endblock %}

{% block content %}
<main>
<div style="margin-left: 8px;">
<h1>Password change</h1>
<p>
Please enter your old password, for security's sake, and then enter your
new password twice so we can verify you typed it in correctly.
</p>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Change my password">
</form>
</div>
</main>
{% endblock %}
15 changes: 15 additions & 0 deletions main/templates/registration/password_reset_complete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends 'main/layout.html' %}

{% block title %}Email Sent{% endblock %}

{% block content %}
<main>
<div style="margin-left: 8px;">
<h1>Password reset complete</h1>
<p>
Your new password has been set. You can log in now on the
<a href="{% url 'login' %}">log in page</a>.
</p>
</div>
</main>
{% endblock %}
16 changes: 16 additions & 0 deletions main/templates/registration/password_reset_confirm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends 'main/layout.html' %}

{% block title %}Enter new password{% endblock %}

{% block content %}
<main>
<div style="margin-left: 8px;">
<h1>Set a new password!</h1>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Change my password">
</form>
</div>
</main>
{% endblock %}
15 changes: 15 additions & 0 deletions main/templates/registration/password_reset_done.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends 'main/layout.html' %}

{% block title %}Email Sent{% endblock %}

{% block content %}
<main>
<div style="margin-left: 8px;">
<h1>Check your inbox.</h1>
<p>
We've emailed you instructions for setting your password. You should
receive the email shortly!
</p>
</div>
</main>
{% endblock %}
12 changes: 12 additions & 0 deletions main/templates/registration/password_reset_email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% load i18n %}{% autoescape off %}
{% blocktrans %}You're receiving this email because you requested a password reset for your user account at {{ site_name }}.{% endblocktrans %}

{% trans "Please go to the following page and choose a new password:" %}
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
{% endblock %}
{% trans "Your username, in case you've forgotten:" %} {{ user.get_username }}

{% blocktrans %}{{ site_name }}{% endblocktrans %}

{% endautoescape %}
17 changes: 17 additions & 0 deletions main/templates/registration/password_reset_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends 'main/layout.html' %}

{% block title %}Forgot Your Password?{% endblock %}

{% block content %}
<main>
<div style="margin-left: 8px;">
<h1>Forgot your password?</h1>
<p>Enter your email address below, and we'll email instructions for setting a new one.</p>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="send instructions">
</form>
</div>
</main>
{% endblock %}
7 changes: 6 additions & 1 deletion main/urls.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
from main import views

from django.urls import path
from django.urls import path, include


# General functionality
urlpatterns = [
path("", views.index, name="index"),
# contacts
path("add-contact/", views.ContactCreate.as_view(), name="contact_create"),
path("contacts/", views.ContactList.as_view(), name="contact_list"),
# domains
path("add-domain/", views.DomainCreate.as_view(), name="domain_create"),
path("domains/", views.DomainList.as_view(), name="domain_list"),
# payments
path("checkout/success/", views.checkout_success, name="checkout_success"),
path("checkout/failure/", views.checkout_failure, name="checkout_failure"),
# user accounts
path("accounts/", include("django.contrib.auth.urls")),
path("accounts/create/", views.UserCreate.as_view(), name="user_create"),
]
42 changes: 33 additions & 9 deletions main/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import stripe
from django.contrib.auth.views import LogoutView as DjLogoutView
from django.contrib.auth import authenticate, login
from django.contrib import messages
from django.http import HttpResponseRedirect
from django.conf import settings
Expand All @@ -9,18 +11,20 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.decorators import login_required

from main import models, centralnic
from main import models, centralnic, forms


@login_required
def index(request):
return render(
request,
"main/index.html",
{
"domain_list": models.Domain.objects.filter(pending=False),
},
)
if request.user.is_authenticated:
return redirect("domain_list")
return render(request, "main/index.html")


class DomainList(LoginRequiredMixin, ListView):
model = models.Domain

def get_queryset(self):
return models.Domain.objects.filter(pending=False)


class ContactCreate(LoginRequiredMixin, CreateView):
Expand Down Expand Up @@ -115,3 +119,23 @@ def checkout_failure(request):
# respond with message
messages.error(request, "payment failed")
return redirect("domain_create")


# User accounts
class UserCreate(CreateView):
form_class = forms.UserCreationForm
template_name = "main/user_create.html"
success_message = "welcome to tofunames.com"

def get_success_url(self):
return reverse_lazy("index")

def form_valid(self, form):
self.object = form.save()
user = authenticate(
username=form.cleaned_data.get("username"),
password=form.cleaned_data.get("password1"),
)
login(self.request, user)
messages.success(self.request, self.success_message)
return HttpResponseRedirect(self.get_success_url())
2 changes: 2 additions & 0 deletions tofunames/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
WSGI_APPLICATION = "tofunames.wsgi.application"

AUTH_USER_MODEL = "main.User"
LOGIN_REDIRECT_URL = "index"
LOGOUT_REDIRECT_URL = "index"


# Database
Expand Down

0 comments on commit 0411b4b

Please sign in to comment.