Images can be uploaded, thumbnails are created, they're displayed within the threads themselves. Just like four chans! There is not an upload size limit set yet. Gotta get on that next. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
92 lines
2.8 KiB
Python
92 lines
2.8 KiB
Python
from django.http import Http404, HttpResponseRedirect
|
|
from django.shortcuts import render, get_object_or_404
|
|
from django.views.generic import DetailView
|
|
from django.views.generic.edit import CreateView
|
|
|
|
from board.models import Post, Board
|
|
|
|
__all__ = ("BoardView", "PostView")
|
|
|
|
|
|
# TODO bump order calculation. this is kind of expensive and should *really*
|
|
# only be run every few seconds rather than every post.
|
|
# This is a scale problem and not a high priority.
|
|
|
|
|
|
def get_client_ip(request):
|
|
"Get the IP address of a client-side request. Shamelessly copy/pasted from StackOverflow."
|
|
x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")
|
|
if x_forwarded_for:
|
|
ip = x_forwarded_for.split(",")[0]
|
|
else:
|
|
ip = request.META.get("REMOTE_ADDR")
|
|
return ip
|
|
|
|
|
|
class BoardView(CreateView):
|
|
model = Post
|
|
fields = ["subject", "name", "text", "image"]
|
|
slug_field = "url"
|
|
slug_url_kwarg = "url"
|
|
|
|
template_name = "board/board_detail.html"
|
|
|
|
def get_context_data(self, **kwargs):
|
|
board_url = self.kwargs["url"]
|
|
board = get_object_or_404(Board, url=board_url)
|
|
kwargs["board"] = board
|
|
page = self.kwargs.get("page", 1)
|
|
|
|
# TODO - allow max number of pages to be specified per-board
|
|
if page not in range(1, 10 + 1):
|
|
raise Http404()
|
|
|
|
PER_PAGE = 10
|
|
start = (page - 1) * PER_PAGE
|
|
end = start + PER_PAGE
|
|
kwargs["threads"] = board.threads.order_by("-last_bump")[start:end]
|
|
kwargs["page"] = page
|
|
|
|
return super(BoardView, self).get_context_data(**kwargs)
|
|
|
|
def form_valid(self, form):
|
|
board_url = self.kwargs["url"]
|
|
board = get_object_or_404(Board, url=board_url)
|
|
|
|
form.instance.board = board
|
|
form.instance.ip = get_client_ip(self.request)
|
|
|
|
self.object = form.save()
|
|
return HttpResponseRedirect(self.get_success_url())
|
|
|
|
|
|
class PostView(CreateView):
|
|
model = Post
|
|
fields = ["name", "text", "image"]
|
|
slug_field = "url"
|
|
slug_url_kwarg = "url"
|
|
|
|
template_name = "board/post_detail.html"
|
|
|
|
def get_context_data(self, **kwargs):
|
|
board_url = self.kwargs["url"]
|
|
kwargs["board"] = get_object_or_404(Board, url=board_url)
|
|
post_id = self.kwargs["id"]
|
|
kwargs["post"] = get_object_or_404(Post, id=post_id)
|
|
return super(PostView, self).get_context_data(**kwargs)
|
|
|
|
def form_valid(self, form):
|
|
board_url = self.kwargs["url"]
|
|
board = get_object_or_404(Board, url=board_url)
|
|
post_id = self.kwargs["id"]
|
|
post = get_object_or_404(Post, id=post_id)
|
|
|
|
form.instance.board = board
|
|
form.instance.ip = get_client_ip(self.request)
|
|
form.instance.op = post
|
|
form.instance.reply = post
|
|
|
|
self.object = form.save()
|
|
|
|
return HttpResponseRedirect(post.get_absolute_url())
|