Merge branch 'master' into production

This commit is contained in:
CDaut 2022-09-12 00:40:30 +02:00 committed by CDaut
commit 28b43caac5
6 changed files with 52 additions and 1 deletions

29
README.md Normal file
View file

@ -0,0 +1,29 @@
# MDblog
This project intends to provide an easy blogging solution for people who like the markdown syntax and want an easy way to publish things. I didn't find a minimalistic enough blog that had markdown support so I wrote my own.
## Setup
This project has two main branches: master and production. You should always only use the production branch to run in production as the master branch does not use a secure server setup.
This project is also Django based. Which means that you will have to run `python3 manage.py migrate` if you spin up the server for the first time to set up the database.
If you are running the production version also make sure to collect the static files via `python3 manage.py collectstatic`.
### Enviroment variables
In order for this project to work you must set up some enviroment variables:
- `POSTGRES_PASSWORD` will be the password for the postgres Database.
- `POSTGRES_USER` is the user that runs the postgres db.
- `POSTGRES_DB` is the name of the database the Django server uses.
- `MD_FILE_PATH` path that points to a folder where all markdownfiles are saved.
- `DJANGO_SECRET_KEY` the secret key used by Django which should obviously be kept secret
- `DJANGO_DEBUG` indicates whether the Django server runs in Debug mode or not. Should be False iff the server runs in production
- `SALT_KEY` some type of hash salt the [2fa provider needs](https://github.com/neutron-sync/django-2fa/blob/main/docs/config.md#environmental-variables---required). Use a 36 character string.
### Docker
After saving the above mentioned enviroment variables in a `envvars.env` file (or adjusting the `docker-compose.yml` to use a different .env file) only a `docker compose up` should be required to bring the project up.
### Migrate
To initialize the Database you need to run a Django migration. To do this you enter the docker container via `docker exec -it mdblog-web-1 /bin/bash` and running the command `python3 manage.py migrate`. After that the DB should be set up and the project is accessible at [127.0.0.1:8000](127.0.0.1:8000). If not you can try putting a https:// before the URL.
### Create a superuser
You might want to create a user to be able to edit the blog. To do so, you have to enter the container, run `python3 manage.py createsuperuser` and follow the on screen prompt.
## Contributions
Please feel free to write issues and open pull requests!

View file

@ -1,7 +1,7 @@
FROM python:3.10-slim FROM python:3.10-slim
ENV PYTHONBUFFERED 1 ENV PYTHONBUFFERED 1
RUN apt update && apt upgrade RUN apt update && apt upgrade -y
RUN mkdir "/markdownblog" RUN mkdir "/markdownblog"
WORKDIR "/markdownblog" WORKDIR "/markdownblog"
ADD ./requirements.txt /markdownblog/ ADD ./requirements.txt /markdownblog/

View file

@ -0,0 +1,18 @@
# Generated by Django 4.0.5 on 2022-09-11 22:33
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('blog', '0002_alter_blogpost_title_alter_tag_name_alter_topic_name'),
]
operations = [
migrations.AddField(
model_name='blogpost',
name='views',
field=models.IntegerField(default=0),
),
]

View file

@ -30,3 +30,4 @@ class Blogpost(models.Model):
tags = models.ManyToManyField(Tag) tags = models.ManyToManyField(Tag)
topic = models.ForeignKey(Topic, blank=True, null=True, on_delete=models.CASCADE) topic = models.ForeignKey(Topic, blank=True, null=True, on_delete=models.CASCADE)
mdfile = models.CharField(max_length=255) mdfile = models.CharField(max_length=255)
views = models.IntegerField(default=0)

View file

@ -32,6 +32,8 @@ def render_md_file(path) -> Template:
def viewblog(request, title) -> HttpResponse: def viewblog(request, title) -> HttpResponse:
post = Blogpost.objects.get(title=title) post = Blogpost.objects.get(title=title)
post.views += 1
post.save()
filepath = os.path.join(os.environ.get("MD_FILE_PATH"), title + ".md") filepath = os.path.join(os.environ.get("MD_FILE_PATH"), title + ".md")
rendered_html = render_md_file(filepath) rendered_html = render_md_file(filepath)

View file

@ -19,6 +19,7 @@
<li class="topic_list_element" draggable="true" ondragstart="drag(event)" <li class="topic_list_element" draggable="true" ondragstart="drag(event)"
id="list_elem_post_{{ post.id }}"> id="list_elem_post_{{ post.id }}">
<a href="{% url 'readpost' title=post.title %}">{{ post }}</a> <a href="{% url 'readpost' title=post.title %}">{{ post }}</a>
<p>Views: {{ post.views }}</p>
<a href="/admin/blog/blogpost/{{ post.id }}/delete"> <a href="/admin/blog/blogpost/{{ post.id }}/delete">
<button class="btn waves-effect waves-light btn-danger"> <button class="btn waves-effect waves-light btn-danger">
<span class="material-icons">delete</span> <span class="material-icons">delete</span>