takahe/activities/views/timelines.py

105 lines
3.2 KiB
Python
Raw Normal View History

2022-11-13 15:14:38 -08:00
from django import forms
from django.shortcuts import redirect
from django.template.defaultfilters import linebreaks_filter
from django.utils.decorators import method_decorator
from django.views.generic import FormView, TemplateView
from activities.models import Post, TimelineEvent
from users.decorators import identity_required
@method_decorator(identity_required, name="dispatch")
class Home(FormView):
template_name = "activities/home.html"
class form_class(forms.Form):
text = forms.CharField(
widget=forms.Textarea(
attrs={
"placeholder": "What's on your mind?",
},
)
)
content_warning = forms.CharField(
required=False,
widget=forms.TextInput(
attrs={
"placeholder": "Content Warning",
"class": "hidden",
},
),
)
def get_context_data(self):
context = super().get_context_data()
2022-11-13 17:42:47 -08:00
context["events"] = (
TimelineEvent.objects.filter(
2022-11-13 15:14:38 -08:00
identity=self.request.identity,
2022-11-13 17:42:47 -08:00
type__in=[TimelineEvent.Types.post, TimelineEvent.Types.boost],
2022-11-13 15:14:38 -08:00
)
.select_related("subject_post", "subject_post__author")
.order_by("-created")[:100]
2022-11-13 17:42:47 -08:00
)
2022-11-13 15:14:38 -08:00
context["current_page"] = "home"
return context
def form_valid(self, form):
Post.create_local(
author=self.request.identity,
content=linebreaks_filter(form.cleaned_data["text"]),
summary=form.cleaned_data.get("content_warning"),
)
return redirect(".")
2022-11-13 17:42:47 -08:00
@method_decorator(identity_required, name="dispatch")
class Local(TemplateView):
template_name = "activities/local.html"
def get_context_data(self):
context = super().get_context_data()
context["posts"] = (
Post.objects.filter(visibility=Post.Visibilities.public, author__local=True)
.select_related("author")
.order_by("-created")[:100]
)
context["current_page"] = "local"
return context
2022-11-13 15:14:38 -08:00
@method_decorator(identity_required, name="dispatch")
class Federated(TemplateView):
template_name = "activities/federated.html"
def get_context_data(self):
context = super().get_context_data()
2022-11-13 17:42:47 -08:00
context["posts"] = (
2022-11-13 15:14:38 -08:00
Post.objects.filter(visibility=Post.Visibilities.public)
.select_related("author")
.order_by("-created")[:100]
)
context["current_page"] = "federated"
return context
2022-11-13 17:42:47 -08:00
@method_decorator(identity_required, name="dispatch")
class Notifications(TemplateView):
template_name = "activities/notifications.html"
def get_context_data(self):
context = super().get_context_data()
context["events"] = (
TimelineEvent.objects.filter(
identity=self.request.identity,
)
.exclude(type__in=[TimelineEvent.Types.post, TimelineEvent.Types.boost])
.select_related("subject_post", "subject_post__author", "subject_identity")
)
context["current_page"] = "notifications"
return context