2026-03-01 21:19:12 -05:00
|
|
|
from django.contrib import messages
|
2026-03-04 00:07:10 -05:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2026-03-07 00:05:32 -05:00
|
|
|
from django.db.models import Max, Q
|
2026-03-05 14:45:55 -05:00
|
|
|
from django.http import HttpResponse, HttpResponseForbidden
|
2026-01-13 20:58:05 -05:00
|
|
|
from django.shortcuts import redirect, render
|
2026-02-21 23:13:23 -05:00
|
|
|
|
2026-03-05 14:45:55 -05:00
|
|
|
from apps.dashboard.forms import ExistingListItemForm, ItemForm
|
|
|
|
|
from apps.dashboard.models import Applet, Item, List, UserApplet
|
2026-02-08 22:18:41 -05:00
|
|
|
from apps.lyric.models import User
|
2026-01-13 20:58:05 -05:00
|
|
|
|
2026-02-21 23:13:23 -05:00
|
|
|
|
2026-03-06 21:34:43 -05:00
|
|
|
APPLET_ORDER = ["new-list", "my-lists", "username", "palette"]
|
2026-03-05 14:45:55 -05:00
|
|
|
UNLOCKED_PALETTES = frozenset(["palette-default"])
|
|
|
|
|
PALETTES = [
|
|
|
|
|
{"name": "palette-default", "label": "Earthman", "locked": False},
|
|
|
|
|
{"name": "palette-nirvana", "label": "Nirvana", "locked": True},
|
|
|
|
|
{"name": "palette-sheol", "label": "Sheol", "locked": True},
|
2026-03-07 15:05:49 -05:00
|
|
|
{"name": "palette-inferno", "label": "Inferno", "locked": True},
|
|
|
|
|
{"name": "palette-terrestre", "label": "Terrestre", "locked": True},
|
|
|
|
|
{"name": "palette-celestia", "label": "Celestia", "locked": True},
|
2026-03-02 15:45:12 -05:00
|
|
|
]
|
2026-03-02 13:57:03 -05:00
|
|
|
|
|
|
|
|
|
2026-03-07 00:05:32 -05:00
|
|
|
def _recent_lists(user, limit=3):
|
|
|
|
|
return (
|
|
|
|
|
List
|
|
|
|
|
.objects
|
|
|
|
|
.filter(Q(owner=user) | Q(shared_with=user))
|
|
|
|
|
.annotate(last_item=Max('item__id'))
|
|
|
|
|
.order_by('-last_item')
|
|
|
|
|
.distinct()[:limit]
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-05 16:08:40 -05:00
|
|
|
def _applet_context(user):
|
|
|
|
|
ua_map = {ua.applet_id: ua.visible for ua in user.user_applets.all()}
|
2026-03-06 21:34:43 -05:00
|
|
|
applets = {a.slug: a for a in Applet.objects.all()}
|
2026-03-05 16:08:40 -05:00
|
|
|
return [
|
2026-03-06 21:34:43 -05:00
|
|
|
{"applet": applets[slug], "visible": ua_map.get(applets[slug].pk, applets[slug].default_visible)}
|
|
|
|
|
for slug in APPLET_ORDER
|
|
|
|
|
if slug in applets
|
2026-03-05 16:08:40 -05:00
|
|
|
]
|
|
|
|
|
|
2026-03-07 00:05:32 -05:00
|
|
|
|
2026-01-13 20:58:05 -05:00
|
|
|
def home_page(request):
|
2026-03-07 00:05:32 -05:00
|
|
|
context = {
|
|
|
|
|
"form": ItemForm(),
|
|
|
|
|
"palettes": PALETTES,
|
|
|
|
|
"page_class": "page-dashboard",
|
|
|
|
|
}
|
2026-03-05 16:08:40 -05:00
|
|
|
if request.user.is_authenticated:
|
|
|
|
|
context["applets"] = _applet_context(request.user)
|
2026-03-07 00:05:32 -05:00
|
|
|
context["recent_lists"] = _recent_lists(request.user)
|
2026-03-05 16:08:40 -05:00
|
|
|
return render(request, "apps/dashboard/home.html", context)
|
2026-01-13 20:58:05 -05:00
|
|
|
|
|
|
|
|
def new_list(request):
|
2026-01-20 15:14:05 -05:00
|
|
|
form = ItemForm(data=request.POST)
|
|
|
|
|
if form.is_valid():
|
|
|
|
|
nulist = List.objects.create()
|
2026-02-08 22:33:15 -05:00
|
|
|
if request.user.is_authenticated:
|
|
|
|
|
nulist.owner = request.user
|
|
|
|
|
nulist.save()
|
2026-01-24 13:00:12 -05:00
|
|
|
form.save(for_list=nulist)
|
2026-01-20 15:14:05 -05:00
|
|
|
return redirect(nulist)
|
|
|
|
|
else:
|
2026-03-07 00:05:32 -05:00
|
|
|
context = {
|
|
|
|
|
"form": form,
|
|
|
|
|
"palettes": PALETTES,
|
|
|
|
|
"page_class": "page-dashboard",
|
|
|
|
|
}
|
2026-03-06 21:34:43 -05:00
|
|
|
if request.user.is_authenticated:
|
|
|
|
|
context["applets"] = _applet_context(request.user)
|
2026-03-07 00:05:32 -05:00
|
|
|
context["recent_lists"] = _recent_lists(request.user)
|
2026-03-06 21:34:43 -05:00
|
|
|
return render(request, "apps/dashboard/home.html", context)
|
2026-01-13 20:58:05 -05:00
|
|
|
|
2026-01-19 16:35:00 -05:00
|
|
|
def view_list(request, list_id):
|
|
|
|
|
our_list = List.objects.get(id=list_id)
|
2026-02-22 21:50:25 -05:00
|
|
|
|
|
|
|
|
if our_list.owner:
|
|
|
|
|
if not request.user.is_authenticated:
|
|
|
|
|
return redirect("/")
|
|
|
|
|
if request.user != our_list.owner and request.user not in our_list.shared_with.all():
|
|
|
|
|
return HttpResponseForbidden()
|
|
|
|
|
|
2026-01-23 22:30:42 -05:00
|
|
|
form = ExistingListItemForm(for_list=our_list)
|
2026-01-19 19:09:11 -05:00
|
|
|
|
2026-01-19 18:48:21 -05:00
|
|
|
if request.method == "POST":
|
2026-01-23 22:30:42 -05:00
|
|
|
form = ExistingListItemForm(for_list=our_list, data=request.POST)
|
2026-01-21 14:41:25 -05:00
|
|
|
if form.is_valid():
|
2026-01-23 22:39:12 -05:00
|
|
|
form.save()
|
2026-01-19 19:25:04 -05:00
|
|
|
return redirect(our_list)
|
2026-01-24 13:00:12 -05:00
|
|
|
return render(request, "apps/dashboard/list.html", {"list": our_list, "form": form})
|
2026-03-04 00:07:10 -05:00
|
|
|
|
2026-02-07 22:47:04 -05:00
|
|
|
def my_lists(request, user_id):
|
2026-02-08 22:18:41 -05:00
|
|
|
owner = User.objects.get(id=user_id)
|
2026-02-17 20:26:42 -05:00
|
|
|
if not request.user.is_authenticated:
|
|
|
|
|
return redirect("/")
|
|
|
|
|
if request.user.id != owner.id:
|
|
|
|
|
return HttpResponseForbidden()
|
2026-02-08 22:18:41 -05:00
|
|
|
return render(request, "apps/dashboard/my_lists.html", {"owner": owner})
|
2026-02-18 13:53:05 -05:00
|
|
|
|
|
|
|
|
def share_list(request, list_id):
|
|
|
|
|
our_list = List.objects.get(id=list_id)
|
2026-02-18 15:14:35 -05:00
|
|
|
try:
|
|
|
|
|
recipient = User.objects.get(email=request.POST["recipient"])
|
2026-02-22 21:18:22 -05:00
|
|
|
if recipient == request.user:
|
|
|
|
|
return redirect(our_list)
|
2026-02-18 15:14:35 -05:00
|
|
|
our_list.shared_with.add(recipient)
|
|
|
|
|
except User.DoesNotExist:
|
|
|
|
|
pass
|
2026-03-01 21:19:12 -05:00
|
|
|
messages.success(request, "An invite has been sent if that address is registered.")
|
2026-02-18 13:53:05 -05:00
|
|
|
return redirect(our_list)
|
2026-03-02 13:57:03 -05:00
|
|
|
|
2026-03-04 00:07:10 -05:00
|
|
|
@login_required(login_url="/")
|
2026-03-05 14:45:55 -05:00
|
|
|
def set_palette(request):
|
2026-03-02 13:57:03 -05:00
|
|
|
if request.method == "POST":
|
2026-03-05 14:45:55 -05:00
|
|
|
palette = request.POST.get("palette", "")
|
|
|
|
|
if palette in UNLOCKED_PALETTES:
|
|
|
|
|
request.user.palette = palette
|
|
|
|
|
request.user.save(update_fields=["palette"])
|
2026-03-02 13:57:03 -05:00
|
|
|
return redirect("home")
|
2026-03-04 00:07:10 -05:00
|
|
|
|
|
|
|
|
@login_required(login_url="/")
|
|
|
|
|
def set_profile(request):
|
|
|
|
|
if request.method == "POST":
|
|
|
|
|
username = request.POST.get("username", "")
|
|
|
|
|
request.user.username = username
|
|
|
|
|
request.user.save(update_fields=["username"])
|
|
|
|
|
return redirect("/")
|
2026-03-05 14:45:55 -05:00
|
|
|
|
|
|
|
|
@login_required(login_url="/")
|
|
|
|
|
def toggle_applets(request):
|
|
|
|
|
checked = request.POST.getlist("applets")
|
|
|
|
|
for applet in Applet.objects.all():
|
|
|
|
|
UserApplet.objects.update_or_create(
|
|
|
|
|
user=request.user,
|
|
|
|
|
applet=applet,
|
|
|
|
|
defaults={"visible": applet.slug in checked},
|
|
|
|
|
)
|
|
|
|
|
if request.headers.get("HX-Request"):
|
2026-03-05 16:08:40 -05:00
|
|
|
return render(request, "apps/dashboard/_partials/_applets.html", {
|
|
|
|
|
"applets": _applet_context(request.user),
|
|
|
|
|
"palettes": PALETTES,
|
2026-03-06 21:34:43 -05:00
|
|
|
"form": ItemForm(),
|
2026-03-07 00:05:32 -05:00
|
|
|
"recent_lists": _recent_lists(request.user),
|
2026-03-05 16:08:40 -05:00
|
|
|
})
|
2026-03-05 14:45:55 -05:00
|
|
|
return redirect("home")
|