303
.gitignore
vendored
Normal file
303
.gitignore
vendored
Normal file
@@ -0,0 +1,303 @@
|
||||
|
||||
# Created by https://www.toptal.com/developers/gitignore/api/python,django,vim,visualstudiocode
|
||||
# Edit at https://www.toptal.com/developers/gitignore?templates=python,django,vim,visualstudiocode
|
||||
|
||||
### Django ###
|
||||
*.log
|
||||
*.pot
|
||||
*.pyc
|
||||
__pycache__/
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
media
|
||||
|
||||
# If your build process includes running collectstatic, then you probably don't need or want to include staticfiles/
|
||||
# in your Git repository. Update and uncomment the following line accordingly.
|
||||
# <django-project-name>/staticfiles/
|
||||
|
||||
### Django.Python Stack ###
|
||||
# Byte-compiled / optimized / DLL files
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
|
||||
# Django stuff:
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
.pybuilder/
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
# For a library or package, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# .python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# poetry
|
||||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||
#poetry.lock
|
||||
|
||||
# pdm
|
||||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||
#pdm.lock
|
||||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
||||
# in version control.
|
||||
# https://pdm.fming.dev/#use-with-ide
|
||||
.pdm.toml
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
# pytype static type analyzer
|
||||
.pytype/
|
||||
|
||||
# Cython debug symbols
|
||||
cython_debug/
|
||||
|
||||
# PyCharm
|
||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
|
||||
### Python ###
|
||||
# Byte-compiled / optimized / DLL files
|
||||
|
||||
# C extensions
|
||||
|
||||
# Distribution / packaging
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
|
||||
# Installer logs
|
||||
|
||||
# Unit test / coverage reports
|
||||
|
||||
# Translations
|
||||
|
||||
# Django stuff:
|
||||
|
||||
# Flask stuff:
|
||||
|
||||
# Scrapy stuff:
|
||||
|
||||
# Sphinx documentation
|
||||
|
||||
# PyBuilder
|
||||
|
||||
# Jupyter Notebook
|
||||
|
||||
# IPython
|
||||
|
||||
# pyenv
|
||||
# For a library or package, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# .python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
|
||||
# poetry
|
||||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||
|
||||
# pdm
|
||||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
||||
# in version control.
|
||||
# https://pdm.fming.dev/#use-with-ide
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||
|
||||
# Celery stuff
|
||||
|
||||
# SageMath parsed files
|
||||
|
||||
# Environments
|
||||
|
||||
# Spyder project settings
|
||||
|
||||
# Rope project settings
|
||||
|
||||
# mkdocs documentation
|
||||
|
||||
# mypy
|
||||
|
||||
# Pyre type checker
|
||||
|
||||
# pytype static type analyzer
|
||||
|
||||
# Cython debug symbols
|
||||
|
||||
# PyCharm
|
||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
|
||||
### Vim ###
|
||||
# Swap
|
||||
[._]*.s[a-v][a-z]
|
||||
!*.svg # comment out if you don't need vector files
|
||||
[._]*.sw[a-p]
|
||||
[._]s[a-rt-v][a-z]
|
||||
[._]ss[a-gi-z]
|
||||
[._]sw[a-p]
|
||||
|
||||
# Session
|
||||
Session.vim
|
||||
Sessionx.vim
|
||||
|
||||
# Temporary
|
||||
.netrwhist
|
||||
*~
|
||||
# Auto-generated tag files
|
||||
tags
|
||||
# Persistent undo
|
||||
[._]*.un~
|
||||
|
||||
### VisualStudioCode ###
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
!.vscode/*.code-snippets
|
||||
|
||||
# Local History for Visual Studio Code
|
||||
.history/
|
||||
|
||||
# Built Visual Studio Code Extensions
|
||||
*.vsix
|
||||
|
||||
### VisualStudioCode Patch ###
|
||||
# Ignore all local history of files
|
||||
.history
|
||||
.ionide
|
||||
|
||||
# Support for Project snippet scope
|
||||
.vscode/*.code-snippets
|
||||
|
||||
# Ignore code-workspaces
|
||||
*.code-workspace
|
||||
|
||||
# End of https://www.toptal.com/developers/gitignore/api/python,django,vim,visualstudiocode
|
||||
13
Pipfile
Normal file
13
Pipfile
Normal file
@@ -0,0 +1,13 @@
|
||||
[[source]]
|
||||
url = "https://pypi.org/simple"
|
||||
verify_ssl = true
|
||||
name = "pypi"
|
||||
|
||||
[packages]
|
||||
django = "*"
|
||||
|
||||
[dev-packages]
|
||||
mypy = "*"
|
||||
|
||||
[requires]
|
||||
python_version = "3.10"
|
||||
0
board/__init__.py
Normal file
0
board/__init__.py
Normal file
3
board/admin.py
Normal file
3
board/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
6
board/apps.py
Normal file
6
board/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class BoardConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'board'
|
||||
0
board/migrations/__init__.py
Normal file
0
board/migrations/__init__.py
Normal file
64
board/models.py
Normal file
64
board/models.py
Normal file
@@ -0,0 +1,64 @@
|
||||
from django.db import models
|
||||
from django.db.models.signals import post_save
|
||||
from django.dispatch import receiver
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
|
||||
|
||||
class Board(models.Model):
|
||||
# The short URL name for the board
|
||||
url = models.CharField(max_length=255, null=False, blank=False, unique=True)
|
||||
# Human-readable name for the board
|
||||
name = models.CharField(max_length=255, null=False, blank=False)
|
||||
|
||||
@property
|
||||
def threads(self):
|
||||
return Post.objects.filter(board=self, op=None)
|
||||
|
||||
|
||||
class Post(models.Model):
|
||||
# Board that this post was made on
|
||||
board = models.ForeignKey("Board", on_delete=models.CASCADE)
|
||||
# Thread that this is a part of
|
||||
op = models.ForeignKey(
|
||||
"self", null=True, on_delete=models.CASCADE, related_name="all_replies"
|
||||
)
|
||||
# Post that this is replying to
|
||||
reply = models.ForeignKey(
|
||||
"self", null=True, on_delete=models.CASCADE, related_name="replies"
|
||||
)
|
||||
# User's supplied name for this post
|
||||
name = models.CharField(max_length=255, null=True, blank=True)
|
||||
# User's supplied subject for this post
|
||||
subject = models.CharField(max_length=255, null=True, blank=True)
|
||||
# Text of this post
|
||||
text = models.TextField(max_length=10000, null=False, blank=True)
|
||||
# The IP address of the user that made this post
|
||||
ip = models.GenericIPAddressField()
|
||||
# Creation time
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
# Last bump time
|
||||
last_bump = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
# TODO : images
|
||||
|
||||
def get_absolute_url(self):
|
||||
if self.op is None:
|
||||
return reverse(
|
||||
"board:post_detail", kwargs={"url": self.board.url, "id": self.id}
|
||||
)
|
||||
else:
|
||||
return (
|
||||
reverse(
|
||||
"board:post_detail",
|
||||
kwargs={"url": self.board.url, "id": self.op.id},
|
||||
)
|
||||
+ f"#p{self.id}"
|
||||
)
|
||||
|
||||
|
||||
@receiver(post_save, sender=Post)
|
||||
def post_created(sender, instance, created, **kwargs):
|
||||
if created and instance.op:
|
||||
instance.op.last_bump = timezone.now()
|
||||
instance.op.save()
|
||||
2
board/static/board/jquery-3.6.0.min.js
vendored
Normal file
2
board/static/board/jquery-3.6.0.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
1
board/static/board/jquery.js
vendored
Symbolic link
1
board/static/board/jquery.js
vendored
Symbolic link
@@ -0,0 +1 @@
|
||||
jquery-3.6.0.min.js
|
||||
9
board/static/board/post.js
Normal file
9
board/static/board/post.js
Normal file
@@ -0,0 +1,9 @@
|
||||
function doQuote(sender) {
|
||||
let id_text = $("#id_text");
|
||||
let caret = id_text[0].selectionStart;
|
||||
let text = id_text.val();
|
||||
let to_add = ">>" + sender.target.innerText + "\n";
|
||||
id_text.val(text.substring(0, caret) + to_add + text.substring(caret));
|
||||
}
|
||||
|
||||
$(document).on("click", ".post_id", doQuote);
|
||||
84
board/static/board/style.css
Normal file
84
board/static/board/style.css
Normal file
@@ -0,0 +1,84 @@
|
||||
hr {
|
||||
color: #ededed;
|
||||
}
|
||||
|
||||
.column {
|
||||
float: left;
|
||||
width: 33.33%;
|
||||
}
|
||||
|
||||
/* Clear floats after the columns */
|
||||
.row:after {
|
||||
content: "";
|
||||
display: table;
|
||||
clear: both;
|
||||
}
|
||||
|
||||
/* Posts */
|
||||
/*.post_body { }*/
|
||||
|
||||
.post_id {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.post_id:hover {
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.post_subject {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.post_name {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.post {
|
||||
background-color: #d9d9d9;
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
.reply {
|
||||
background-color: #eee;
|
||||
padding: 10px;
|
||||
margin: 5px 0 0 0;
|
||||
}
|
||||
|
||||
.replies_elided {
|
||||
font-weight: italic;
|
||||
font-size: small;
|
||||
}
|
||||
|
||||
.quote {
|
||||
color: #595;
|
||||
}
|
||||
|
||||
.post_link {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.post_link_broken {
|
||||
text-decoration: line-through;
|
||||
}
|
||||
|
||||
/* Misc */
|
||||
|
||||
a:link {
|
||||
color:#555;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
a:visited {
|
||||
color: #555;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
color: #888;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
a:active {
|
||||
color: #888;
|
||||
text-decoration: none;
|
||||
}
|
||||
18
board/templates/board/base.html
Normal file
18
board/templates/board/base.html
Normal file
@@ -0,0 +1,18 @@
|
||||
{% load static %}
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
<title>{% block title %}{% if title %}{{title}}{% else %}Index{% endif %}{% endblock %}</title>
|
||||
<link rel="stylesheet" href="{% static 'board/style.css' %}">
|
||||
<script src="{% static 'board/jquery.js' %}"></script>
|
||||
<script src="{% static 'board/post.js' %}"></script>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
{% block content %}{% endblock content %}
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
57
board/templates/board/board_detail.html
Normal file
57
board/templates/board/board_detail.html
Normal file
@@ -0,0 +1,57 @@
|
||||
{% extends "board/base.html" %}
|
||||
|
||||
{% block title %}
|
||||
{% with title=board.url %}
|
||||
{{ block.super }}
|
||||
{% endwith %}
|
||||
{% endblock title %}
|
||||
|
||||
{% block content %}
|
||||
{# board header #}
|
||||
<div class="row">
|
||||
<div class="column"> </div>
|
||||
<div class="column">
|
||||
<h1>/{{ board.url }}/ - {{ board.name }}</h1>
|
||||
</div>
|
||||
<div class="column"> </div>
|
||||
</div>
|
||||
<hr />
|
||||
{# post creation form #}
|
||||
<div class="row">
|
||||
<div class="column"> </div>
|
||||
<div class="column">
|
||||
<div class="row">
|
||||
<h2>Create a new thread</h2>
|
||||
</div>
|
||||
<div class="row">
|
||||
<form method="post" action="{% url 'board:board_detail' url=board.url %}">
|
||||
{% csrf_token %}
|
||||
<table>
|
||||
{{ form.as_table }}
|
||||
<tr><td> </td><td><input type="submit" value="Submit" /></td></tr>
|
||||
</table>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<div class="column"> </div>
|
||||
</div>
|
||||
<hr />
|
||||
{# posts #}
|
||||
{% for post in threads %}
|
||||
<div class="row post" id="p{{post.id}}">
|
||||
{# TODO we need some way to parameterize the last N threads #}
|
||||
{% with reply_link=True replies_elided=post.replies.all|length|add:"-3" %}
|
||||
{% include "board/post_snippet.html" %}
|
||||
{% endwith %}
|
||||
|
||||
{# get the last 3 replies #}
|
||||
{% for post in post.replies.all|dictsortreversed:"id"|slice:":3" reversed %}
|
||||
<div class="row reply" id="p{{post.id}}">
|
||||
{% include "board/post_snippet.html" %}
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
<hr />
|
||||
{% endfor %}
|
||||
|
||||
{% endblock content %}
|
||||
9
board/templates/board/post_create.html
Normal file
9
board/templates/board/post_create.html
Normal file
@@ -0,0 +1,9 @@
|
||||
{% extends "board/base.html" %}
|
||||
{% block content %}
|
||||
<div class="row">
|
||||
<h1>/{{ board.url }}/</h1>
|
||||
</div>
|
||||
<div class="row">
|
||||
{% include "board/post_create_form.html" %}
|
||||
</div>
|
||||
{% endblock content %}
|
||||
43
board/templates/board/post_detail.html
Normal file
43
board/templates/board/post_detail.html
Normal file
@@ -0,0 +1,43 @@
|
||||
{% extends "board/base.html" %}
|
||||
|
||||
{% block title %}
|
||||
{% with title=board.url %}
|
||||
{{ block.super }}
|
||||
{% endwith %}
|
||||
{% endblock title %}
|
||||
|
||||
{% block content %}
|
||||
{# board header #}
|
||||
<div class="row">
|
||||
<div class="column"> </div>
|
||||
<div class="column">
|
||||
<h1>/{{ board.url }}/ - {{ board.name }}</h1>
|
||||
</div>
|
||||
<div class="column"> </div>
|
||||
</div>
|
||||
<hr />
|
||||
{# post creation form #}
|
||||
<div class="row">
|
||||
<div class="column"> </div>
|
||||
<div class="column">
|
||||
<form method="post" action="{% url 'board:post_detail' url=board.url id=post.id %}">
|
||||
{% csrf_token %}
|
||||
<table>
|
||||
{{ form.as_table }}
|
||||
<tr><td> </td><td><input type="submit" value="Submit" /></td></tr>
|
||||
</table>
|
||||
</form>
|
||||
</div>
|
||||
<div class="column"> </div>
|
||||
</div>
|
||||
<hr />
|
||||
{# posts #}
|
||||
<div class="row post">
|
||||
{% include "board/post_snippet.html" %}
|
||||
{% for post in post.replies.all %}
|
||||
<div class="row reply">
|
||||
{% include "board/post_snippet.html" %}
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endblock content %}
|
||||
21
board/templates/board/post_snippet.html
Normal file
21
board/templates/board/post_snippet.html
Normal file
@@ -0,0 +1,21 @@
|
||||
{% load post_body %}
|
||||
<div id="p{{post.id}}">
|
||||
{# TODO images #}
|
||||
<a href="#p{{post.id}}">#.</a>
|
||||
<span class="post_id">{{post.id}}</span>
|
||||
{% if post.subject %}
|
||||
<span class="post_subject">{{post.subject}}</span>
|
||||
{% endif %}
|
||||
by <span class="post_name">{{post.name|default:"Anonymous"}}</span>
|
||||
at {{post.created}}
|
||||
{% if reply_link %}
|
||||
[<a href="{{post.get_absolute_url}}">Reply</a>]
|
||||
{% endif %}
|
||||
{% if replies_elided > 0 %}
|
||||
<br/>
|
||||
<span class="replies_elided">
|
||||
({{replies_elided}} replies elided, click reply to view)
|
||||
</span>
|
||||
{% endif %}
|
||||
<p class="post_body">{{post|post_body|safe}}</p>
|
||||
</div>
|
||||
0
board/templatetags/__init__.py
Normal file
0
board/templatetags/__init__.py
Normal file
93
board/templatetags/post_body.py
Normal file
93
board/templatetags/post_body.py
Normal file
@@ -0,0 +1,93 @@
|
||||
import re
|
||||
from django import template
|
||||
from board.models import Post
|
||||
|
||||
|
||||
REPLY_START_RE = re.compile(r"^>>\d+")
|
||||
REPLY_RE = re.compile(r">>\d+")
|
||||
QUOTE_RE = re.compile(r">.+$")
|
||||
register = template.Library()
|
||||
|
||||
|
||||
def htmlspecialchars(t: str):
|
||||
return (
|
||||
t.replace("&", "&")
|
||||
.replace('"', """)
|
||||
.replace("<", "<")
|
||||
.replace(">", ">")
|
||||
)
|
||||
|
||||
|
||||
class ReplyBuilder:
|
||||
def __init__(self, text: str):
|
||||
self.text = text
|
||||
self.index = 0
|
||||
self.final = ""
|
||||
|
||||
@property
|
||||
def c(self):
|
||||
return self.lookahead(0)
|
||||
|
||||
@property
|
||||
def n(self):
|
||||
return self.lookahead(1)
|
||||
|
||||
@property
|
||||
def remain(self):
|
||||
return self.text[self.index :]
|
||||
|
||||
def lookahead(self, n):
|
||||
if (self.index + n) < len(self.text):
|
||||
return self.text[self.index + n]
|
||||
else:
|
||||
return None
|
||||
|
||||
def adv(self, n=1):
|
||||
self.index += n
|
||||
|
||||
def build(self) -> str:
|
||||
while self.c:
|
||||
if self.remain[:8] == ">>" and self.lookahead(8) in "0123456789":
|
||||
self.do_reply()
|
||||
elif self.remain[:4] == ">":
|
||||
self.do_quote()
|
||||
else:
|
||||
self.final += self.c
|
||||
self.adv()
|
||||
return self.final
|
||||
|
||||
def do_quote(self):
|
||||
self.adv(4)
|
||||
self.final += '<span class="quote">>'
|
||||
while self.c and self.c != "\n":
|
||||
if self.remain[:8] == ">>":
|
||||
self.do_reply()
|
||||
else:
|
||||
self.final += self.c
|
||||
self.adv()
|
||||
self.final += "</span>"
|
||||
|
||||
def do_reply(self):
|
||||
# Skip past >>
|
||||
self.adv(8)
|
||||
# Get the post ID
|
||||
post_id = ""
|
||||
while self.c and self.c in "0123456789":
|
||||
post_id += self.c
|
||||
self.adv()
|
||||
post = Post.objects.filter(id=int(post_id)).first()
|
||||
if post:
|
||||
self.final += f'<a class="post_link" href="{post.get_absolute_url()}">>>{post_id}</a>'
|
||||
else:
|
||||
self.final += f'<span class="post_link_broken">>>{post_id}</span>'
|
||||
|
||||
|
||||
@register.filter(name="post_body")
|
||||
def post_body(post: Post) -> str:
|
||||
text = htmlspecialchars(post.text)
|
||||
|
||||
text = ReplyBuilder(text).build()
|
||||
|
||||
# Finally, replace linebreaks
|
||||
text = text.replace("\n", "<br/>")
|
||||
return text
|
||||
3
board/tests.py
Normal file
3
board/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
10
board/urls.py
Normal file
10
board/urls.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from django.urls import path
|
||||
from board.views import *
|
||||
|
||||
|
||||
app_name = "board"
|
||||
urlpatterns = [
|
||||
path("<slug:url>/", BoardView.as_view(), name="board_detail"),
|
||||
path("<slug:url>/page/<int:page>/", BoardView.as_view(), name="board_detail"),
|
||||
path("<slug:url>/post/<int:id>/", PostView.as_view(), name="post_detail"),
|
||||
]
|
||||
92
board/views.py
Normal file
92
board/views.py
Normal file
@@ -0,0 +1,92 @@
|
||||
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"]
|
||||
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"]
|
||||
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())
|
||||
22
manage.py
Executable file
22
manage.py
Executable file
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env python
|
||||
"""Django's command-line utility for administrative tasks."""
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
"""Run administrative tasks."""
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'threadchat.settings')
|
||||
try:
|
||||
from django.core.management import execute_from_command_line
|
||||
except ImportError as exc:
|
||||
raise ImportError(
|
||||
"Couldn't import Django. Are you sure it's installed and "
|
||||
"available on your PYTHONPATH environment variable? Did you "
|
||||
"forget to activate a virtual environment?"
|
||||
) from exc
|
||||
execute_from_command_line(sys.argv)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
0
threadchat/__init__.py
Normal file
0
threadchat/__init__.py
Normal file
16
threadchat/asgi.py
Normal file
16
threadchat/asgi.py
Normal file
@@ -0,0 +1,16 @@
|
||||
"""
|
||||
ASGI config for threadchat project.
|
||||
|
||||
It exposes the ASGI callable as a module-level variable named ``application``.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.asgi import get_asgi_application
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'threadchat.settings')
|
||||
|
||||
application = get_asgi_application()
|
||||
126
threadchat/settings.py
Normal file
126
threadchat/settings.py
Normal file
@@ -0,0 +1,126 @@
|
||||
"""
|
||||
Django settings for threadchat project.
|
||||
|
||||
Generated by 'django-admin startproject' using Django 4.0.4.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/4.0/topics/settings/
|
||||
|
||||
For the full list of settings and their values, see
|
||||
https://docs.djangoproject.com/en/4.0/ref/settings/
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Sequence
|
||||
|
||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
# Quick-start development settings - unsuitable for production
|
||||
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
|
||||
|
||||
# SECURITY WARNING: keep the secret key used in production secret!
|
||||
SECRET_KEY = "django-insecure-kxgmmxo@b=75fh!pvsq$w!ure!!*i)2sdr-5^l1o4q^)j)$a=9"
|
||||
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = True
|
||||
|
||||
with open("ALLOWED_HOSTS") as fp:
|
||||
ALLOWED_HOSTS: Sequence[str] = list(filter(len, map(str.strip, fp)))
|
||||
|
||||
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
"django.contrib.admin",
|
||||
"django.contrib.auth",
|
||||
"django.contrib.contenttypes",
|
||||
"django.contrib.sessions",
|
||||
"django.contrib.messages",
|
||||
"django.contrib.staticfiles",
|
||||
"board",
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
"django.middleware.security.SecurityMiddleware",
|
||||
"django.contrib.sessions.middleware.SessionMiddleware",
|
||||
"django.middleware.common.CommonMiddleware",
|
||||
"django.middleware.csrf.CsrfViewMiddleware",
|
||||
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
||||
"django.contrib.messages.middleware.MessageMiddleware",
|
||||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||
]
|
||||
|
||||
ROOT_URLCONF = "threadchat.urls"
|
||||
|
||||
TEMPLATES = [
|
||||
{
|
||||
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
||||
"DIRS": [],
|
||||
"APP_DIRS": True,
|
||||
"OPTIONS": {
|
||||
"context_processors": [
|
||||
"django.template.context_processors.debug",
|
||||
"django.template.context_processors.request",
|
||||
"django.contrib.auth.context_processors.auth",
|
||||
"django.contrib.messages.context_processors.messages",
|
||||
],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
WSGI_APPLICATION = "threadchat.wsgi.application"
|
||||
|
||||
|
||||
# Database
|
||||
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
|
||||
|
||||
DATABASES = {
|
||||
"default": {
|
||||
"ENGINE": "django.db.backends.sqlite3",
|
||||
"NAME": BASE_DIR / "db.sqlite3",
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Password validation
|
||||
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators
|
||||
|
||||
AUTH_PASSWORD_VALIDATORS = [
|
||||
{
|
||||
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
|
||||
},
|
||||
{
|
||||
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
|
||||
},
|
||||
{
|
||||
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
|
||||
},
|
||||
{
|
||||
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
# Internationalization
|
||||
# https://docs.djangoproject.com/en/4.0/topics/i18n/
|
||||
|
||||
LANGUAGE_CODE = "en-us"
|
||||
|
||||
TIME_ZONE = "UTC"
|
||||
|
||||
USE_I18N = True
|
||||
|
||||
USE_TZ = True
|
||||
|
||||
|
||||
# Static files (CSS, JavaScript, Images)
|
||||
# https://docs.djangoproject.com/en/4.0/howto/static-files/
|
||||
|
||||
STATIC_URL = "static/"
|
||||
|
||||
# Default primary key field type
|
||||
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
|
||||
|
||||
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
||||
22
threadchat/urls.py
Normal file
22
threadchat/urls.py
Normal file
@@ -0,0 +1,22 @@
|
||||
"""threadchat URL Configuration
|
||||
|
||||
The `urlpatterns` list routes URLs to views. For more information please see:
|
||||
https://docs.djangoproject.com/en/4.0/topics/http/urls/
|
||||
Examples:
|
||||
Function views
|
||||
1. Add an import: from my_app import views
|
||||
2. Add a URL to urlpatterns: path('', views.home, name='home')
|
||||
Class-based views
|
||||
1. Add an import: from other_app.views import Home
|
||||
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
|
||||
Including another URLconf
|
||||
1. Import the include() function: from django.urls import include, path
|
||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||
"""
|
||||
from django.contrib import admin
|
||||
from django.urls import include, path
|
||||
|
||||
urlpatterns = [
|
||||
path("admin/", admin.site.urls),
|
||||
path("", include("board.urls")),
|
||||
]
|
||||
16
threadchat/wsgi.py
Normal file
16
threadchat/wsgi.py
Normal file
@@ -0,0 +1,16 @@
|
||||
"""
|
||||
WSGI config for threadchat project.
|
||||
|
||||
It exposes the WSGI callable as a module-level variable named ``application``.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'threadchat.settings')
|
||||
|
||||
application = get_wsgi_application()
|
||||
Reference in New Issue
Block a user