Skip to content
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

升级至django 2.0 😌 #73

Merged
merged 2 commits into from
Jan 6, 2018
Merged
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
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,12 @@ services:
env:
global:
- DJANGO_SETTINGS_MODULE="travis_test.travis_settings"
matrix:
- DJANGO="Django==1.8"
- DJANGO="Django==1.9"
- DJANGO="Django==1.10"
- DJANGO="Django==1.11"
branches:
only:
- master
# command to install dependencies
install:
- pip install -r travis_test/requirements.txt
- pip install "$DJANGO"
- pip install python-coveralls
- pip install coverage
before_script:
Expand Down
2 changes: 1 addition & 1 deletion DjangoBlog/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
'compressor'
]

MIDDLEWARE_CLASSES = [
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.gzip.GZipMiddleware',
Expand Down
2 changes: 1 addition & 1 deletion DjangoBlog/sitemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from blog.models import Article, Category, Tag
from accounts.models import BlogUser
from django.contrib.sitemaps import GenericSitemap
from django.core.urlresolvers import reverse
from django.urls import reverse


class StaticViewSitemap(Sitemap):
Expand Down
2 changes: 1 addition & 1 deletion DjangoBlog/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from blog.models import Article, Category, Tag
from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from django.urls import reverse
import datetime
from DjangoBlog.utils import *

Expand Down
10 changes: 5 additions & 5 deletions DjangoBlog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('blog.urls', namespace='blog', app_name='blog')),
url(r'', include('blog.urls', namespace='blog')),

url(r'', include('comments.urls', namespace='comment', app_name='comments')),
url(r'', include('accounts.urls', namespace='account', app_name='accounts')),
url(r'', include('oauth.urls', namespace='oauth', app_name='oauth')),
url(r'', include('comments.urls', namespace='comment')),
url(r'', include('accounts.urls', namespace='account')),
url(r'', include('oauth.urls', namespace='oauth')),
url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap'),
url(r'^feed/$', DjangoBlogFeed()),
url(r'^search', include('haystack.urls'), name='search'),
url(r'', include('servermanager.urls', namespace='servermanager', app_name='servermanagers'))
url(r'', include('servermanager.urls', namespace='servermanager'))
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# DjangoBlog

基于`python3.5`和`Django1.10`的博客。
基于`python3.5`和`Django2.0`的博客。

[![Build Status](https://travis-ci.org/liangliangyy/DjangoBlog.svg?branch=master)](https://travis-ci.org/liangliangyy/DjangoBlog) [![Coverage Status](https://coveralls.io/repos/github/liangliangyy/DjangoBlog/badge.svg?branch=master)](https://coveralls.io/github/liangliangyy/DjangoBlog?branch=master) [![Requirements Status](https://requires.io/github/liangliangyy/DjangoBlog/requirements.svg?branch=master)](https://requires.io/github/liangliangyy/DjangoBlog/requirements/?branch=master) [![license](https://img.shields.io/github/license/liangliangyy/djangoblog.svg)]() [![GitHub release](https://img.shields.io/github/release/liangliangyy/djangoblog.svg)]() [![python3.5](https://img.shields.io/badge/python-3.5-brightgreen.svg)]() [![django1.10](https://img.shields.io/badge/django-1.10-brightgreen.svg)]()
[![Build Status](https://travis-ci.org/liangliangyy/DjangoBlog.svg?branch=master)](https://travis-ci.org/liangliangyy/DjangoBlog) [![Coverage Status](https://coveralls.io/repos/github/liangliangyy/DjangoBlog/badge.svg?branch=master)](https://coveralls.io/github/liangliangyy/DjangoBlog?branch=master) [![Requirements Status](https://requires.io/github/liangliangyy/DjangoBlog/requirements.svg?branch=master)](https://requires.io/github/liangliangyy/DjangoBlog/requirements/?branch=master) [![license](https://img.shields.io/github/license/liangliangyy/djangoblog.svg)]() [![GitHub release](https://img.shields.io/github/release/liangliangyy/djangoblog.svg)]() [![python3.5](https://img.shields.io/badge/python-3.5-brightgreen.svg)]() [![django1.10](https://img.shields.io/badge/django-2.0-brightgreen.svg)]()

## 主要功能:
- 文章,页面,分类目录,标签的添加,删除,编辑等。文章及页面支持`Markdown`,支持代码高亮。
Expand All @@ -14,7 +14,6 @@
- 简单的SEO功能,新建文章等会自动通知Google和百度。
- 集成了简单的图床功能。
- 集成`django-compressor`,自动压缩`css`,`js`。
- 基于`python3`,支持`Django`多版本。`Django`的1.8,1.9,1.10,1.11均测试通过。
- 网站异常邮件提醒,若有未捕捉到的异常会自动发送提醒邮件。
- 集成了微信公众号功能,现在可以使用微信公众号来管理你的vps了。
## 安装
Expand Down
3 changes: 2 additions & 1 deletion accounts/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.db import models
from django.contrib.auth.models import AbstractUser, BaseUserManager
from django.core.urlresolvers import reverse
from django.urls import reverse
from django.contrib.sites.models import Site
from django.utils.timezone import now

Expand All @@ -9,6 +9,7 @@

class BlogUser(AbstractUser):
nickname = models.CharField('昵称', max_length=50, blank=True)
nickname = models.CharField('昵称', max_length=100, blank=True)
mugshot = models.ImageField('头像', upload_to='upload/mugshots', blank=True)
created_time = models.DateTimeField('创建时间', default=now)
last_mod_time = models.DateTimeField('修改时间', default=now)
Expand Down
2 changes: 1 addition & 1 deletion accounts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.contrib.sites.models import Site
import datetime
from accounts.models import BlogUser
from django.core.urlresolvers import reverse
from django.urls import reverse


# Create your tests here.
Expand Down
2 changes: 2 additions & 0 deletions accounts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from . import views
from .forms import LoginForm

app_name="accounts"

urlpatterns = [
url(r'^login/$', views.LoginView.as_view(success_url='/'), name='login', kwargs={'authentication_form': LoginForm}),
url(r'^register/$', views.RegisterView.as_view(success_url="/"), name='register'),
Expand Down
2 changes: 1 addition & 1 deletion accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.views.generic import FormView, RedirectView
from django.contrib.auth import get_user_model
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.urls import reverse
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.views.decorators.csrf import csrf_protect
Expand Down
32 changes: 8 additions & 24 deletions blog/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,17 @@


class OnlineMiddleware(object):
def process_request(self, request):
self.start_time = time.time()
def __init__(self, get_response=None):
self.get_response = get_response
super().__init__()

def process_view(self, request, view_func, view_args, view_kwargs):
"""
处理当前在线人数
"""
def __call__(self, request):
start_time = time.time()
response = self.get_response(request)
http_user_agent = request.META.get('HTTP_USER_AGENT', [])
if 'Spider' in http_user_agent or 'spider' in http_user_agent:
return
return response

online_ips = cache.get("online_ips", [])
if online_ips:
online_ips = cache.get_many(online_ips).keys()
online_ips = list(online_ips)
ip = get_real_ip(request)

cache.set(ip, 0, 5 * 60)

if ip not in online_ips:
online_ips.append(ip)
s = type(online_ips)
cache.set("online_ips", online_ips)

def process_response(self, request, response):
cast_time = 0.921
if self.__dict__ and 'start_time' in self.__dict__:
cast_time = time.time() - self.start_time
cast_time = time.time() - start_time
response.content = response.content.replace(b'<!!LOAD_TIMES!!>', str.encode(str(cast_time)[:5]))
return response
4 changes: 2 additions & 2 deletions blog/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.db import models
from django.core.urlresolvers import reverse
from django.urls import reverse
from django.conf import settings
from uuslug import slugify

Expand Down Expand Up @@ -125,7 +125,7 @@ def prev_article(self):
class Category(BaseModel):
"""文章分类"""
name = models.CharField('分类名', max_length=30, unique=True)
parent_category = models.ForeignKey('self', verbose_name="父级分类", blank=True, null=True)
parent_category = models.ForeignKey('self', verbose_name="父级分类", blank=True, null=True, on_delete=models.CASCADE)

class Meta:
ordering = ['name']
Expand Down
6 changes: 4 additions & 2 deletions blog/templatetags/blog_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe
import random
from django.core.urlresolvers import reverse
from django.urls import reverse
from blog.models import Article, Category, Tag, Links, SideBar
from django.utils.encoding import force_text
from django.shortcuts import get_object_or_404
Expand Down Expand Up @@ -266,7 +266,7 @@ def gravatar(email, size=40):
return mark_safe('<img src="%s" height="%d" width="%d">' % (url, size, size))


@register.assignment_tag
@register.simple_tag
def query(qs, **kwargs):
""" template tag which allows queryset filtering. Usage:
{% query books author=author as mybooks %}
Expand All @@ -275,3 +275,5 @@ def query(qs, **kwargs):
{% endfor %}
"""
return qs.filter(**kwargs)


40 changes: 17 additions & 23 deletions blog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,32 @@
@time: 2016/11/2 下午7:15
"""

from django.conf.urls import url
from django.urls import path
from django.views.decorators.cache import cache_page
from . import views
from haystack.forms import ModelSearchForm
from haystack.query import SearchQuerySet
from haystack.views import SearchView

app_name = "blog"
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^page/(?P<page>\d+)$', views.IndexView.as_view(), name='index_page'),
path(r'', views.IndexView.as_view(), name='index'),
path(r'page/<int:page>/', views.IndexView.as_view(), name='index_page'),

url(r'^article/(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/(?P<article_id>\d+).html$',
views.ArticleDetailView.as_view(),
name='detailbyid'),
path(r'article/<int:year>/<int:month>/<int:day>/<int:article_id>.html',
views.ArticleDetailView.as_view(),
name='detailbyid'),

url(r'^blogpage/(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/(?P<page_id>\d+)-(?P<slug>[\w-]+).html$',
views.ArticleDetailView.as_view(),
name='pagedetail'),
path(r'category/<slug:category_name>.html', views.CategoryDetailView.as_view(), name='category_detail'),
path(r'category/<slug:category_name>/<int:page>.html', views.CategoryDetailView.as_view(),
name='category_detail_page'),

url(r'^category/(?P<category_name>[\w-]+).html$', views.CategoryDetailView.as_view(), name='category_detail'),
url(r'^category/(?P<category_name>[\w-]+)/(?P<page>\d+).html$', views.CategoryDetailView.as_view(),
name='category_detail_page'),
# url(r'^category/(?P<category_name>[\w-]+)/(?P<page>\d+).html$', views.CategoryDetailView.as_view(),
# name='category_detail'),
path(r'author/<author_name>.html', views.AuthorDetailView.as_view(), name='author_detail'),
path(r'author/<author_name>/<int:page>).html', views.AuthorDetailView.as_view(),
name='author_detail_page'),

url(r'^author/(?P<author_name>\w+).html$', views.AuthorDetailView.as_view(), name='author_detail'),
url(r'^author/(?P<author_name>\w+)/(?P<page>\d+).html$', views.AuthorDetailView.as_view(),
name='author_detail_page'),

url(r'^tag/(?P<tag_name>[\w-]+).html$', views.TagDetailView.as_view(), name='tag_detail'),
url(r'^tag/(?P<tag_name>[\w-]+)/(?P<page>\d+).html$', views.TagDetailView.as_view(), name='tag_detail_page'),

url(r'^upload', views.fileupload, name='upload'),
url(r'^refresh', views.refresh_memcache, name='refresh')
path(r'tag/<slug:tag_name>.html', views.TagDetailView.as_view(), name='tag_detail'),
path(r'tag/<slug:tag_name>/<int:page>).html', views.TagDetailView.as_view(), name='tag_detail_page'),
path(r'upload', views.fileupload, name='upload'),
path(r'refresh', views.refresh_memcache, name='refresh')
]
16 changes: 11 additions & 5 deletions blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,11 @@ def fileupload(request):
isimage = len([i for i in imgextensions if fname.find(i) >= 0]) > 0

basepath = r'/var/www/resource/{type}/{timestr}'.format(
type='files' if not isimage else'image', timestr=timestr)
type='files' if not isimage else 'image', timestr=timestr)
if settings.TESTING:
basepath = settings.BASE_DIR + '/uploads'
url = 'https://resource.lylinux.net/{type}/{timestr}/{filename}'.format(
type='files' if not isimage else'image', timestr=timestr, filename=filename)
type='files' if not isimage else 'image', timestr=timestr, filename=filename)
if not os.path.exists(basepath):
os.makedirs(basepath)
savepath = os.path.join(basepath, filename)
Expand Down Expand Up @@ -256,17 +256,23 @@ def refresh_memcache(request):
return HttpResponse(e)


def page_not_found_view(request):
def page_not_found_view(request, exception):
if exception:
logger.error(exception)
url = request.get_full_path()
return render(request, 'blog/error_page.html',
{'message': '哎呀,您访问的地址 ' + url + ' 是一个未知的地方。请点击首页看看别的?', 'statuscode': '404'})


def server_error_view(request):
def server_error_view(request, exception):
if exception:
logger.error(exception)
return render(request, 'blog/error_page.html',
{'message': '哎呀,出错了,我已经收集到了错误信息,之后会抓紧抢修,请点击首页看看别的?', 'statuscode': '500'})


def permission_denied_view(request):
def permission_denied_view(request, exception):
if exception:
logger.error(exception)
return render(request, 'blog/error_page.html',
{'message': '哎呀,您没有权限访问此页面,请点击首页看看别的?', 'statuscode': '403'})
2 changes: 1 addition & 1 deletion comments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Comment(models.Model):
last_mod_time = models.DateTimeField('修改时间', default=now)
author = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='作者', on_delete=models.CASCADE)
article = models.ForeignKey(Article, verbose_name='文章', on_delete=models.CASCADE)
parent_comment = models.ForeignKey('self', verbose_name="上级评论", blank=True, null=True)
parent_comment = models.ForeignKey('self', verbose_name="上级评论", blank=True, null=True, on_delete=models.CASCADE)
is_enable = models.BooleanField('是否显示', default=True, blank=False, null=False)

class Meta:
Expand Down
2 changes: 1 addition & 1 deletion comments/templatetags/comments_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
register = template.Library()


@register.assignment_tag
@register.simple_tag
def parse_commenttree(commentlist, comment):
"""获得当前评论子评论的列表
用法: {% parse_commenttree article_comments comment as childcomments %}
Expand Down
2 changes: 1 addition & 1 deletion comments/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from blog.models import Article, Category, Tag
from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from django.urls import reverse
import datetime
from accounts.models import BlogUser
from comments.templatetags.comments_tags import *
Expand Down
7 changes: 4 additions & 3 deletions comments/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
@time: 2016/11/12 下午3:03
"""

from django.conf.urls import url
from django.urls import path
from . import views

app_name = "comments"
urlpatterns = [
# url(r'^postcomment/(?P<article_id>\d+)$', views.CommentPostView.as_view(), name='postcomment'),
url(r'^article/(?P<article_id>\d+)/postcomment$', views.CommentPostView.as_view(), name='postcomment'),
# url(r'^po456stcomment/(?P<article_id>\d+)$', views.CommentPostView.as_view(), name='postcomment'),
path('article/<int:article_id>/postcomment', views.CommentPostView.as_view(), name='postcomment'),
]
10 changes: 3 additions & 7 deletions comments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def form_valid(self, form):

article_id = self.kwargs['article_id']
article = Article.objects.get(pk=article_id)
if not self.request.user.is_authenticated():
if not self.request.user.is_authenticated:
email = form.cleaned_data['email']
username = form.cleaned_data['name']

Expand All @@ -64,12 +64,8 @@ def form_valid(self, form):
comment.save(True)

from DjangoBlog.blog_signals import comment_save_signal
port = 80
try:
# django1.8 没有这个方法...
port = self.request.get_port()
except:
pass

port = self.request.get_port()
username = self.request.user.username if self.request.user else ''
comment_save_signal.send(sender=self.__class__, comment_id=comment.id, username=username, serverport=port)
return HttpResponseRedirect("%s#div-comment-%d" % (article.get_absolute_url(), comment.pk))
3 changes: 2 additions & 1 deletion oauth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@


class OAuthUser(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='用户', blank=True, null=True)
author = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='用户', blank=True, null=True,
on_delete=models.CASCADE)
openid = models.CharField(max_length=50)
nikename = models.CharField(max_length=50, verbose_name='昵称')
token = models.CharField(max_length=150, null=True, blank=True)
Expand Down
2 changes: 1 addition & 1 deletion oauth/templatetags/oauth_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@time: 2017/3/4 下午3:22
"""
from oauth.oauthmanager import get_oauth_apps
from django.core.urlresolvers import reverse
from django.urls import reverse
from django import template
from django.conf import settings

Expand Down
Loading