Skip to content

Completed FB Login #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# created by virtualenv automatically
*.pyc
*.db
*.sqlite3
*.sqlite3
18 changes: 16 additions & 2 deletions blogging/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
from django.contrib import admin

from blogging.models import Post, Category

class CategoryAdmin(admin.ModelAdmin):
list_display = ('name', 'description')

exclude = ('posts',)

class CategoryInLine(admin.TabularInline):
model = Category.posts.through

class PostAdmin(admin.ModelAdmin):
inlines = [CategoryInLine,]

list_display = ('title', 'created_date',)


admin.site.register(Post)
admin.site.register(Category)
admin.site.register(Post, PostAdmin)
admin.site.register(Category, CategoryAdmin)
20 changes: 20 additions & 0 deletions blogging/feeds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.contrib.syndication.views import Feed
from django.urls import reverse
from blogging.models import Post

class LatestEntriesFeed(Feed):
title = 'Most Recent Posts'
link = '/sitenews/'
description = '5 Most Recent Posts to Site'

def items(self):
return Post.objects.order_by('-published_date')[:5]

def item_title(self, item):
return item.title

def item_author(self, item):
return item.author

def item_link(self, item):
return reverse('post', args=[item.pk])
2 changes: 1 addition & 1 deletion blogging/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 2.1.1 on 2019-10-29 01:39
# Generated by Django 2.1.1 on 2021-06-26 19:07

from django.conf import settings
from django.db import migrations, models
Expand Down
4 changes: 2 additions & 2 deletions blogging/migrations/0002_category.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 2.1.1 on 2019-11-05 03:35
# Generated by Django 2.1.1 on 2021-07-01 03:23

from django.db import migrations, models

Expand All @@ -16,7 +16,7 @@ class Migration(migrations.Migration):
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=128)),
('description', models.TextField(blank=True)),
('posts', models.ManyToManyField(blank=True, related_name='categories', to='blogging.Post')),
('posts', models.ManyToManyField(blank=True, related_name='Categories', to='blogging.Post')),
],
),
]
21 changes: 16 additions & 5 deletions blogging/models.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
from typing import Set
from django.db import models
from django.contrib.auth.models import User

class Post(models.Model):
#title - CharField max_length = 60
title = models.CharField(max_length=128)
#text - TextField
text = models.TextField(blank=True)
#author - CharField max_length = 30
author = models.ForeignKey(User, on_delete=models.CASCADE)
#created_date - DateField auto_now_add=False
created_date = models.DateTimeField(auto_now_add=True)
#modified_date - DateField auto_now=True
modified_date = models.DateTimeField(auto_now=True)
#published_date - - DateField auto_now_add=True
published_date = models.DateTimeField(blank=True, null=True)

def __str__(self):
return self.title


class Category(models.Model):
#name
name = models.CharField(max_length=128)
#description
description = models.TextField(blank=True)
posts = models.ManyToManyField(Post, blank=True, related_name='categories')

class Meta:
verbose_name_plural = 'Categories'

#posts
posts = models.ManyToManyField(Post, blank=True, related_name='Categories')

def __str__(self):
return self.name

class Meta:
verbose_name_plural = 'Categories'
27 changes: 27 additions & 0 deletions blogging/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from django.contrib.auth.models import User, Group
from rest_framework import serializers
from blogging.models import Post, Category

class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ['url', 'username', 'email', 'groups']


class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Group
fields = ['url', 'name']

class PostSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Post
fields = [
'url', 'title', 'text', 'author', 'created_date',
'modified_date','published_date',
]

class CategorySerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Category
fields = ['url', 'name', 'description', 'posts']
2 changes: 1 addition & 1 deletion blogging/templates/blogging/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ <h1>{{ post }}</h1>
<li>{{ category }}</li>
{% endfor %}
</ul>
{% endblock %}
{% endblock %}
4 changes: 2 additions & 2 deletions blogging/templates/blogging/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ <h1>Recent Posts</h1>
{% for post in posts %}
<div class="post">
<h2>
<a href="{% url 'blog_detail' post.pk %}">{{ post }}</a>
<a href="{% url 'blog_detail' post.pk %}">{{ post }}</a>
</h2>
<p class="byline">
Posted by {{ post.author.username }} &mdash; {{ post.published_date }}
Expand All @@ -19,4 +19,4 @@ <h2>
</ul>
</div>
{% endfor %}
{% endblock %}
{% endblock %}
10 changes: 2 additions & 8 deletions blogging/tests.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import datetime

from django.test import TestCase
from django.contrib.auth.models import User
from django.utils.timezone import utc

from blogging.models import Post
from blogging.models import Category


class PostTestCase(TestCase):
fixtures = ['blogging_test_fixture.json', ]
fixtures = ['blogging_test_fixture.json',]

def setUp(self):
self.user = User.objects.get(pk=1)
Expand All @@ -20,16 +17,13 @@ def test_string_representation(self):
actual = str(p1)
self.assertEqual(expected, actual)


class CategoryTestCase(TestCase):

def test_string_representation(self):
expected = "A Category"
c1 = Category(name=expected)
actual = str(c1)
self.assertEqual(expected, actual)


class FrontEndTestCase(TestCase):
"""test views provided in the front-end"""
fixtures = ['blogging_test_fixture.json', ]
Expand Down Expand Up @@ -69,4 +63,4 @@ def test_details_only_published(self):
self.assertEqual(resp.status_code, 200)
self.assertContains(resp, title)
else:
self.assertEqual(resp.status_code, 404)
self.assertEqual(resp.status_code, 404)
6 changes: 4 additions & 2 deletions blogging/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.urls import path
from blogging.views import list_view, detail_view
from blogging.views import stub_view
from blogging.views import list_view
from blogging.views import detail_view

urlpatterns = [
path('', list_view, name="blog_index"),
path('posts/<int:post_id>/', detail_view, name="blog_detail"),
]
]
79 changes: 73 additions & 6 deletions blogging/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.template import loader
from django.contrib.auth.models import User, Group
from blogging import serializers
from rest_framework import viewsets
from rest_framework import permissions
from blogging.models import Category, Post
from blogging.serializers import CategorySerializer, PostSerializer, UserSerializer, GroupSerializer
from django.contrib.syndication.views import Feed
from django.urls import reverse

from blogging.models import Post
def stub_view(request, *args, **kwargs):
body = "Stub View\n\n"
if args:
body += "Args:\n"
body += "\n".join(["\t%s" % a for a in args])
if kwargs:
body += "Kwargs:\n"
body += "\n".join(["\t%s: %s" % i for i in kwargs.items()])
return HttpResponse(body, content_type="text/plain")

def list_view(request):
published = Post.objects.exclude(published_date__exact=None)
posts = published.order_by('-published_date')
context = {'posts': posts}
return render(request, 'blogging/list.html', context)

def detail_view(request, post_id):
published = Post.objects.exclude(published_date__exact=None)
Expand All @@ -14,9 +35,55 @@ def detail_view(request, post_id):
context = {'post': post}
return render(request, 'blogging/detail.html', context)

class UserViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
permission_classes = [permissions.IsAuthenticated]

def list_view(request):
published = Post.objects.exclude(published_date__exact=None)
posts = published.order_by('-published_date')
context = {'posts': posts}
return render(request, 'blogging/list.html', context)

class GroupViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited.
"""
queryset = Group.objects.all()
serializer_class = GroupSerializer
permission_classes = [permissions.IsAuthenticated]

class PostViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows Post to be viewed or edited.
"""
queryset = Post.objects.all()
serializer_class = PostSerializer
permission_classes = [permissions.IsAuthenticated]

class CategoryViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows Categories to be viewed or edited.
"""
queryset = Category.objects.all()
serializer_class = CategorySerializer
permission_classes = [permissions.IsAuthenticated]

class LatestEntriesFeed(Feed):
title = 'Most Recent Posts'
link = '/sitenews/'
description = '5 Most Recent Posts to Site'

def items(self):
return Post.objects.order_by('-published_date')[:5]

def item_title(self, item):
return item.title

def item_author(self, item):
return item.author

def item_id(self, item):
return item.pk

def item_link(self, item):
return reverse('latest-feed', kwargs= {"id" : self.item_id})
16 changes: 16 additions & 0 deletions mysite/production.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os

import dj_database_url

from .settings import *


DATABASES = {
'default': dj_database_url.config(default='sqlite:///' + os.path.join(BASE_DIR, 'db.sqlite3'))
}

DEBUG = False
TEMPLATE_DEBUG = False
ALLOWED_HOSTS = [os.environ.get('ALLOWED_HOSTS'), 'localhost']
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
SECRET_KEY = os.environ.get('SECRET_KEY')
18 changes: 18 additions & 0 deletions mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
'polling',
'blogging',
'rest_framework',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -123,3 +129,15 @@

LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = '/'

REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10
}

AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',
]

SITE_ID = 1
Loading