began adding addpost function

This commit is contained in:
CDaut 2022-06-02 08:29:52 +02:00 committed by CDaut
parent 81c455f014
commit c50d047233
15 changed files with 106 additions and 21 deletions

View file

@ -1,6 +1,6 @@
import factory
from blog.models import Topic
from blog.models import Topic, Blogpost, Tag
class TopicFactory(factory.Factory):
@ -9,3 +9,16 @@ class TopicFactory(factory.Factory):
name = factory.Faker("word")
numbered = False
class PostFactory(factory.Factory):
class Meta:
model = Blogpost
title = factory.Faker("word")
tags = Tag.objects.all()[0] if len(Tag.objects.all()) != 0 else None
if len(Topic.objects.all()) == 0:
TopicFactory.create_batch(10)
topics = Topic.objects.all()[0]
mdfile = "/tmp/test.md"

View file

@ -0,0 +1,19 @@
# Generated by Django 4.0.5 on 2022-06-01 19:13
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('blog', '0002_alter_blogpost_topics_alter_topic_roottopic'),
]
operations = [
migrations.AddField(
model_name='blogpost',
name='mdfile',
field=models.CharField(default='/tmp/test.md', max_length=255),
preserve_default=False,
),
]

View file

@ -0,0 +1,18 @@
# Generated by Django 4.0.5 on 2022-06-01 19:19
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('blog', '0003_blogpost_mdfile'),
]
operations = [
migrations.RenameField(
model_name='blogpost',
old_name='topics',
new_name='topic',
),
]

View file

@ -16,8 +16,13 @@ class Topic(models.Model):
class Blogpost(models.Model):
def __str__(self) -> str:
return str(self.title)
created = models.DateTimeField(auto_now_add=True)
last_modified = models.DateTimeField(auto_now=True)
title = models.CharField(max_length=255)
tags = models.ManyToManyField(Tag)
topics = 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)

View file

@ -5,5 +5,5 @@ urlpatterns = [
path('', viewblog, name="index"),
path('manage/addpost', addpost, name='addpost'),
path('manage/order', order, name='order'),
path('mock/topic/<int:n>/', createmocks, name='mock'),
path('mock/<str:object>/<int:n>/', createmocks, name='mock'),
]

View file

@ -4,7 +4,7 @@ from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import render, redirect
from blog.factories import TopicFactory
from blog.models import Topic
from blog.models import Topic, Tag, Blogpost
from markdownblog import settings
@ -15,16 +15,34 @@ def viewblog(request) -> HttpResponse:
@login_required
def order(request):
return None
return render(request, 'blog/order.html')
@login_required
def addpost(request) -> HttpResponse:
if request.method == 'POST':
title = request.POST['title']
markdown = request.POST['markdown']
tags = []
if 'tags' in request.POST and request.POST['tags'] != '':
tags_str = request.POST['tags'].split(" ")
for tag in tags_str:
tags.append(Tag.objects.filter(name=tag)[0])
topicstr = request.POST['topic']
topics = None if topicstr == "No topic" else Topic.objects.filter(name=topicstr)[0]
new_post = Blogpost.objects.create(title=title, topics=topics)
new_post.tags.set(tags)
context = {'alltopics': Topic.objects.all().order_by('name').values()}
return render(request, 'blog/addpost.html', context)
def createmocks(request, n) -> HttpResponse:
def createmocks(request, objtype, n) -> HttpResponse:
topics = TopicFactory.create_batch(n)
for topic in topics: