add basic usermanagement and login functionality

This commit is contained in:
simon 2021-10-21 19:33:27 +07:00
parent 401aa6c2f8
commit 3d0859ceec
5 changed files with 51 additions and 12 deletions

2
run.sh
View File

@ -13,7 +13,9 @@ until curl "$ES_URL" -fs; do
sleep 5 sleep 5
done done
python manage.py makemigrations
python manage.py migrate python manage.py migrate
python manage.py createsuperuser --noinput
python manage.py collectstatic --noinput -c python manage.py collectstatic --noinput -c
nginx & nginx &
celery -A home.tasks worker --loglevel=INFO & celery -A home.tasks worker --loglevel=INFO &

View File

@ -142,3 +142,5 @@ STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field # https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
LOGIN_URL = "/login/"

View File

@ -26,8 +26,9 @@
<h2>Your Self Hosted YouTube Media Server</h2> <h2>Your Self Hosted YouTube Media Server</h2>
<form action="/login/" method="POST" name="login"> <form action="/login/" method="POST" name="login">
{% csrf_token %} {% csrf_token %}
<input type="text" name="user" id="user" placeholder="Username"><br> {% for field in form %}
<input type="password" name="password" id="password" placeholder="Password"><br> {{ field }}<br>
{% endfor %}
<button type="submit">Login</button> <button type="submit">Login</button>
</form> </form>
<p class="login-links"><span><a href="https://github.com/bbilly1/tubearchivist" target="_blank">Github</a></span> <span><a href="https://github.com/bbilly1/tubearchivist#donate" target="_blank">Donate</a></span></p> <p class="login-links"><span><a href="https://github.com/bbilly1/tubearchivist" target="_blank">Github</a></span> <span><a href="https://github.com/bbilly1/tubearchivist#donate" target="_blank">Donate</a></span></p>

View File

@ -1,5 +1,6 @@
""" all home app urls """ """ all home app urls """
from django.contrib.auth.decorators import login_required
from django.urls import path from django.urls import path
from home.views import ( from home.views import (
AboutView, AboutView,
@ -10,23 +11,29 @@ from home.views import (
LoginView, LoginView,
SettingsView, SettingsView,
VideoView, VideoView,
process,
progress,
) )
from . import views
urlpatterns = [ urlpatterns = [
path("", HomeView.as_view(), name="home"), path("", login_required(HomeView.as_view()), name="home"),
path("login/", LoginView.as_view(), name="login"), path("login/", LoginView.as_view(), name="login"),
path("about/", AboutView.as_view(), name="about"), path("about/", AboutView.as_view(), name="about"),
path("downloads/", DownloadView.as_view(), name="downloads"), path(
path("settings/", SettingsView.as_view(), name="settings"), "downloads/", login_required(DownloadView.as_view()), name="downloads"
path("process/", views.process, name="process"), ),
path("downloads/progress/", views.progress, name="progress"), path("settings/", login_required(SettingsView.as_view()), name="settings"),
path("channel/", ChannelView.as_view(), name="channel"), path("process/", login_required(process), name="process"),
path("downloads/progress/", login_required(progress), name="progress"),
path("channel/", login_required(ChannelView.as_view()), name="channel"),
path( path(
"channel/<slug:channel_id_detail>/", "channel/<slug:channel_id_detail>/",
ChannelIdView.as_view(), login_required(ChannelIdView.as_view()),
name="channel_id", name="channel_id",
), ),
path("video/<slug:video_id>/", VideoView.as_view(), name="video"), path(
"video/<slug:video_id>/",
login_required(VideoView.as_view()),
name="video",
),
] ]

View File

@ -8,6 +8,10 @@ import json
import urllib.parse import urllib.parse
from time import sleep from time import sleep
from django import forms
from django.contrib.auth import login
from django.contrib.auth.forms import AuthenticationForm
from django.forms.widgets import PasswordInput, TextInput
from django.http import JsonResponse from django.http import JsonResponse
from django.shortcuts import redirect, render from django.shortcuts import redirect, render
from django.utils.http import urlencode from django.utils.http import urlencode
@ -126,6 +130,17 @@ class HomeView(View):
return redirect(search_url, permanent=True) return redirect(search_url, permanent=True)
class CustomAuthForm(AuthenticationForm):
"""better styled login form"""
username = forms.CharField(
widget=TextInput(attrs={"placeholder": "Username"}), label=False
)
password = forms.CharField(
widget=PasswordInput(attrs={"placeholder": "Password"}), label=False
)
class LoginView(View): class LoginView(View):
"""resolves to /login/ """resolves to /login/
Greeting and login page Greeting and login page
@ -134,11 +149,23 @@ class LoginView(View):
def get(self, request): def get(self, request):
"""handle get requests""" """handle get requests"""
colors = self.read_config() colors = self.read_config()
form = CustomAuthForm()
context = { context = {
"colors": colors, "colors": colors,
"form": form,
} }
return render(request, "home/login.html", context) return render(request, "home/login.html", context)
@staticmethod
def post(request):
"""handle login post request"""
form = AuthenticationForm(data=request.POST)
if form.is_valid():
user = form.get_user()
login(request, user)
return redirect("/")
@staticmethod @staticmethod
def read_config(): def read_config():
"""read needed values from redis""" """read needed values from redis"""