forked from pm2spy6xt/djangoBlogStudy
parent
2e4c61a5c8
commit
b41b5c210d
@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
|
||||
"""
|
||||
@version: ??
|
||||
@author: liangliangyy
|
||||
@license: MIT Licence
|
||||
@contact: liangliangyy@gmail.com
|
||||
@site: https://www.lylinux.org/
|
||||
@software: PyCharm
|
||||
@file: __init__.py.py
|
||||
@time: 2017/8/27 上午11:40
|
||||
"""
|
||||
@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
|
||||
"""
|
||||
@version: ??
|
||||
@author: liangliangyy
|
||||
@license: MIT Licence
|
||||
@contact: liangliangyy@gmail.com
|
||||
@site: https://www.lylinux.org/
|
||||
@software: PyCharm
|
||||
@file: blogapi.py
|
||||
@time: 2017/8/27 上午11:40
|
||||
"""
|
||||
from blog.models import Article, Category, Tag
|
||||
from haystack.query import EmptySearchQuerySet, SearchQuerySet
|
||||
|
||||
|
||||
class BlogApi():
|
||||
def __init__(self):
|
||||
self.searchqueryset = SearchQuerySet()
|
||||
self.searchqueryset.auto_query('')
|
||||
self.__max_takecount__ = 8
|
||||
|
||||
def search_articles(self, query):
|
||||
sqs = self.searchqueryset.auto_query(query)
|
||||
sqs = sqs.load_all()
|
||||
return sqs[:self.__max_takecount__]
|
||||
|
||||
def get_category_lists(self):
|
||||
return Category.objects.all()
|
||||
|
||||
def get_category_articles(self, categoryname):
|
||||
articles = Article.objects.filter(category__name=categoryname)
|
||||
if articles:
|
||||
return articles[:self.__max_takecount__]
|
||||
return None
|
||||
|
||||
def get_recent_articles(self):
|
||||
return Article.objects.all()[:self.__max_takecount__]
|
||||
@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
|
||||
"""
|
||||
@version: ??
|
||||
@author: liangliangyy
|
||||
@license: MIT Licence
|
||||
@contact: liangliangyy@gmail.com
|
||||
@site: https://www.lylinux.org/
|
||||
@software: PyCharm
|
||||
@file: commonapi.py
|
||||
@time: 2017/9/2 上午1:43
|
||||
"""
|
||||
import requests
|
||||
import json
|
||||
from DjangoBlog.utils import logger
|
||||
|
||||
|
||||
class TuLing():
|
||||
def __init__(self):
|
||||
self.__key__ = '2f1446eb0321804291b0a1e217c25bb5'
|
||||
self.__appid__ = 137762
|
||||
|
||||
def __build_req_url(self, content):
|
||||
return 'http://www.tuling123.com/openapi/api?key=%s&info=%s&userid=%s' % (
|
||||
self.__key__, content, self.__appid__)
|
||||
|
||||
def UserAgent(self, url):
|
||||
rsp = requests.get(url)
|
||||
return rsp.content
|
||||
|
||||
def getdata(self, content):
|
||||
requrl = self.__build_req_url(content)
|
||||
res = self.UserAgent(requrl)
|
||||
try:
|
||||
jsons = json.loads(res, encoding='utf-8')
|
||||
if str(jsons["code"]) == '100000':
|
||||
return jsons["text"]
|
||||
except Exception as e:
|
||||
print(e)
|
||||
logger.warn(e)
|
||||
return "哎呀,出错啦。"
|
||||
@ -1,3 +1,11 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
from .models import commands
|
||||
|
||||
|
||||
class CommandsAdmin(admin.ModelAdmin):
|
||||
pass
|
||||
|
||||
|
||||
admin.site.register(commands, CommandsAdmin)
|
||||
|
||||
@ -1,3 +1,66 @@
|
||||
from django.test import TestCase
|
||||
from django.test import Client, RequestFactory, TestCase
|
||||
from django.contrib.sites.models import Site
|
||||
from .models import commands
|
||||
import datetime
|
||||
from accounts.models import BlogUser
|
||||
from blog.models import Category, Article
|
||||
from .robot import search, category, recents
|
||||
from werobot.messages.messages import TextMessage
|
||||
from .robot import MessageHandler, CommandHandler
|
||||
|
||||
|
||||
# Create your tests here.
|
||||
class ServerManagerTest(TestCase):
|
||||
def setUp(self):
|
||||
self.client = Client()
|
||||
self.factory = RequestFactory()
|
||||
|
||||
def test_validate_comment(self):
|
||||
site = Site.objects.get_current().domain
|
||||
user = BlogUser.objects.create_superuser(email="liangliangyy1@gmail.com",
|
||||
username="liangliangyy1", password="liangliangyy1")
|
||||
|
||||
self.client.login(username='liangliangyy1', password='liangliangyy1')
|
||||
|
||||
c = Category()
|
||||
c.name = "categoryccc"
|
||||
c.created_time = datetime.datetime.now()
|
||||
c.last_mod_time = datetime.datetime.now()
|
||||
c.save()
|
||||
|
||||
article = Article()
|
||||
article.title = "nicetitleccc"
|
||||
article.body = "nicecontentccc"
|
||||
article.author = user
|
||||
article.category = c
|
||||
article.type = 'a'
|
||||
article.status = 'p'
|
||||
article.save()
|
||||
s = TextMessage([])
|
||||
s.content = "nicetitleccc"
|
||||
rsp = search(s, None)
|
||||
self.assertTrue(rsp != '没有找到相关文章。')
|
||||
rsp = category(None, None)
|
||||
self.assertIsNotNone(rsp)
|
||||
rsp = recents(None, None)
|
||||
self.assertTrue(rsp != '暂时还没有文章')
|
||||
|
||||
cmd = commands()
|
||||
cmd.title = "test"
|
||||
cmd.command = "ls"
|
||||
cmd.describe = "test"
|
||||
cmd.save()
|
||||
|
||||
cmdhandler = CommandHandler()
|
||||
rsp = cmdhandler.run('test')
|
||||
self.assertIsNotNone(rsp)
|
||||
s.source = 'u'
|
||||
s.content = 'test'
|
||||
msghandler = MessageHandler(s, {})
|
||||
d = msghandler.is_admin
|
||||
e = msghandler.is_password_set
|
||||
msghandler.userinfo.isPasswordSet = True
|
||||
msghandler.userinfo.isAdmin = True
|
||||
msghandler.handler()
|
||||
s.content = 'y'
|
||||
msghandler.handler()
|
||||
|
||||
Loading…
Reference in new issue