Skip to content

Commit

Permalink
Merge pull request #181 from dhmit/blog-hide-unpublished
Browse files Browse the repository at this point in the history
Hide Unpublished Blog Posts
  • Loading branch information
anastasia authored Jul 20, 2022
2 parents 7ffab39 + 3cc9abb commit 335c65d
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 3.2.14 on 2022-07-13 15:05
# Generated by Django 3.2.14 on 2022-07-13 20:56

from django.db import migrations, models
import taggit.managers
Expand All @@ -15,7 +15,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='blogpost',
name='tags',
field=taggit.managers.TaggableManager(blank=True, help_text='A comma-separated list of tags.', through='taggit.TaggedItem', to='taggit.Tag', verbose_name='Tags'),
field=taggit.managers.TaggableManager(help_text='A comma-separated list of tags.', through='taggit.TaggedItem', to='taggit.Tag', verbose_name='Tags'),
),
migrations.AlterField(
model_name='blogpost',
Expand Down
17 changes: 3 additions & 14 deletions backend/blog/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from django.db import models
from tinymce import models as tinymce_models
from django.contrib.flatpages.models import FlatPage
from django.contrib.auth.models import User, Group
from django.contrib.auth.models import User
from django.template.defaultfilters import slugify
from taggit.managers import TaggableManager
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from config.settings import BLOG_ROOT_URL


class BlogPost(models.Model):
Expand All @@ -26,19 +25,9 @@ class BlogPost(models.Model):
# TODO: Maybe add related blogposts field?

def get_absolute_url(self):
# TODO
return ""
return f"/{BLOG_ROOT_URL}/{self.slug}"

def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
super(BlogPost, self).save(*args, **kwargs)


# content_type = ContentType.objects.get_for_model(BlogPost)
#
# permission = Permission.objects.filter(content_type=content_type)
#
# BlogGroup = Group.objects.get_or_create(name="Blog Writer")
# # permission_list=["add_blogpost","change_blogpost","delete_blogpost","view_blogpost"]
# BlogGroup.permissions.set(permission)
21 changes: 14 additions & 7 deletions backend/blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
"""
from django.shortcuts import render
from blog.models import BlogPost
from taggit.models import Tag


def index(request):
"""
Blog home page
"""
posts = BlogPost.objects.all()
tags = Tag.objects.all()
print(posts)
# Set of all tags belonging to published posts
tags = set([tag for post in posts for tag in post.tags.names() if post.published])
context = {'posts': posts, 'tags': tags}
return render(request, 'blog/home.html', context)

Expand All @@ -21,7 +20,15 @@ def blog_post(request, slug):
"""
Single blog post view
"""
post = BlogPost.objects.get(slug=slug)
tags = Tag.objects.all()
context = {'post': post, 'tags': tags}
return render(request, 'blog/post.html', context)
posts = BlogPost.objects.filter(slug=slug)
if posts and (
posts[0].published
or request.user == posts[0].author
or request.user.has_perm('blog.view_blogpost')
or request.user.is_superuser
):
tags = posts[0].tags.names()
context = {'post': posts[0], 'tags': tags}
return render(request, 'blog/post.html', context)

return render(request, 'blog/blog_404.html') # Respond with 404 page
1 change: 1 addition & 0 deletions backend/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
TESSDATA_DIR = Path(PROJECT_ROOT, 'backend', 'data', 'tessdata')
TEXT_DETECTION_PATH = Path(BACKEND_DATA_DIR, 'frozen_east_text_detection.pb')
YOLO_DIR = Path(ANALYSIS_DIR, 'yolo_files')
BLOG_ROOT_URL = "blog"

# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

Expand Down
32 changes: 18 additions & 14 deletions backend/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, re_path, include
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path
from django.contrib.auth import views as auth_views

from app.common import render_react_view
from app import views
from app.common import render_react_view
from blog import views as blog_views
from config.settings import BLOG_ROOT_URL


def react_view_path(route, component_name):
Expand All @@ -44,8 +43,10 @@ def react_view_path(route, component_name):
path('api/photo/<int:map_square_number>/<int:photo_number>/', views.photo),
path('api/prev_next_photos/<int:map_square_number>/<int:photo_number>/',
views.previous_next_photos),
path('api/similar_photos/<int:map_square_number>/<int:photo_number>/<int:num_similar_photos>/',
views.get_photo_by_similarity),
path(
'api/similar_photos/<int:map_square_number>/<int:photo_number>/<int'
':num_similar_photos>/',
views.get_photo_by_similarity),
path('api/photographer/', views.get_photographer),
path('api/photographer/<int:photographer_number>/', views.get_photographer),
path('api/map_square/<int:map_square_number>/', views.get_map_square),
Expand All @@ -57,14 +58,17 @@ def react_view_path(route, component_name):
path('api/analysis/<str:analysis_name>/', views.get_photos_by_analysis),
path('api/clustering/<int:number_of_clusters>/<int:cluster_number>/',
views.get_photos_by_cluster),
path('api/analysis/<str:analysis_name>/<str:object_name>/', views.get_photos_by_analysis),
path('api/analysis/<str:analysis_name>/<str:object_name>/',
views.get_photos_by_analysis),
path('api/search/', views.search),
path('api/get_tags/', views.get_tags),
path('api/arrondissements_geojson/', views.get_arrondissements_geojson),
path('api/arrondissements_geojson/<int:arr_number>/',
views.get_arrondissements_geojson),
# path('api/faster_rcnn_object_detection/<str:object_name>/', views.get_photos_by_object_rcnn),
# path('api/model/<str:model_name>/<str:object_name>/', views.get_photos_by_object),
# path('api/faster_rcnn_object_detection/<str:object_name>/',
# views.get_photos_by_object_rcnn),
# path('api/model/<str:model_name>/<str:object_name>/',
# views.get_photos_by_object),
path('', views.index),
path('about/', views.about),
path('search/', views.search_view),
Expand All @@ -79,10 +83,10 @@ def react_view_path(route, component_name):
path('all_analysis/', views.all_analysis_view),
path('clustering/<int:num_of_clusters>/<int:cluster_num>/', views.cluster_view),
# blog urls
path('blog/', blog_views.index, name="blog_home"),
path('blog/<str:slug>/', blog_views.blog_post,
path(f'{BLOG_ROOT_URL}/', blog_views.index, name="blog_home"),
path(f'{BLOG_ROOT_URL}/<str:slug>/', blog_views.blog_post,
name='blog-detail'),
# sign up urls
path('login/',auth_views.LoginView.as_view(template_name='admin/login.html'),name='login'),
path('logout/',auth_views.LogoutView.as_view(),name='logout')
# log in/out urls
path('login/', auth_views.LoginView.as_view(template_name='admin/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout')
]
18 changes: 18 additions & 0 deletions backend/templates/blog/blog_404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% include "base.html" %}
{% block content %}
<div class="blog" id="app_root">
<div class="container mt-3">
<div class="row">
<div class="col-lg-8">
{{ post.content | safe }}
</div>
<div>
Blog page not found.
<a class="col-lg-8" href="/blog">
Return to Blog Home
</a>
</div>
</div>
</div>
</div>
{% endblock content %}
2 changes: 1 addition & 1 deletion backend/templates/blog/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ <h2 style="text-align: justify; margin-top: 50px">Blog</h2>
{% if post.published %}
<div class="card mb-4 border-0">
<div class="card-body">
<a href="/blog/{{ post.slug }}">
<a href={{ post.get_absolute_url }}>
<h1 class="card-title">
{{ post.title|truncatewords:4 }}
</h1>
Expand Down
7 changes: 7 additions & 0 deletions backend/templates/blog/post.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
{% include "base.html" %}
{% block content %}
<div class="blog" id="app_root">
{% if not post.published %}
<h5 style="white-space: pre-wrap">
This page is only a preview of the post and is only visible to the author and site admin.
To make it visible to anyone, the author or a user with blog edit access must click
"published" in the admin panel.
</h5>
{% endif %}
<div class="container mt-3">
<div class="row">
<div class="col-lg-8">
Expand Down

0 comments on commit 335c65d

Please sign in to comment.