Skip to content

Commit

Permalink
utf8 match test
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier-Lam committed Feb 14, 2019
1 parent ea1904e commit 941acae
Show file tree
Hide file tree
Showing 49 changed files with 279 additions and 61 deletions.
File renamed without changes.
20 changes: 8 additions & 12 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#encoding: utf-8
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from setuptools import find_packages, setup

import wechat_django
Expand All @@ -8,27 +10,21 @@

with open("requirements.txt") as f:
requirements = [l for l in f.read().splitlines() if l]
try:
dev_requirements = requirements.copy()
except AttributeError:
import copy
dev_requirements = copy.copy(requirements)
dev_requirements += ["cryptography>=2.5", "httmock==1.2.6"]

setup(
name="wechat-django",
name=wechat_django.__title__,
version=wechat_django.__version__,
author=wechat_django.__author__,
author_email="Lam.Xavier@hotmail.com",
url="https://github.com/Xavier-Lam/wechat-django",
author_email=wechat_django.__author_email__,
url=wechat_django.__url__,
packages=find_packages(),
keywords="WeChat, weixin, wx, micromessenger, 微信, django",
description="WeChat Django Extension",
description=wechat_django.__description__,
long_description=long_description,
long_description_content_type="text/markdown",
install_requires=requirements,
include_package_data=True,
tests_require=dev_requirements,
tests_require=requirements + ["cryptography>=2.5", "httmock==1.2.6"],
test_suite="runtests.main",
classifiers=[
"Development Status :: 2 - Pre-Alpha",
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ envlist =

[testenv]
deps =
-rrequirements.dev.txt
-rrequirements.tox.txt
django111: Django>=1.11,<2.0
django20: Django>=2.0,<2.1
django21: Django>=2.1
Expand Down
7 changes: 7 additions & 0 deletions wechat_django/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

__title__ = "wechat-django"
__description__ = "Django WeChat Extension"
__url__ = "https://github.com/Xavier-Lam/wechat-django"
__version__ = "0.1.0"
__author__ = "Xavier-Lam"
__author_email__ = "Lam.Xavier@hotmail.com"

__all__ = ("urls", )

Expand Down
4 changes: 3 additions & 1 deletion wechat_django/_patch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#encoding: utf-8
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import time
from wechatpy import replies
from wechatpy.fields import *
Expand Down
4 changes: 3 additions & 1 deletion wechat_django/admin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#encoding: utf-8
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.utils.module_loading import import_string

from .. import settings
Expand Down
3 changes: 3 additions & 0 deletions wechat_django/admin/article.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django import forms
from django.contrib import admin, messages
from django.db import models as m
Expand Down
4 changes: 3 additions & 1 deletion wechat_django/admin/bases.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#encoding: utf-8
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from contextlib import contextmanager
import logging

Expand Down
4 changes: 3 additions & 1 deletion wechat_django/admin/material.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#encoding: utf-8
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django import forms
from django.contrib import admin, messages
from django.db import models as m
Expand Down
4 changes: 3 additions & 1 deletion wechat_django/admin/menu.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#encoding: utf-8
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django import forms
from django.contrib import admin, messages
from django.db import models as m
Expand Down
4 changes: 3 additions & 1 deletion wechat_django/admin/messagehandler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#encoding: utf-8
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django import forms
from django.contrib import admin, messages
from django.utils import timezone
Expand Down
3 changes: 3 additions & 0 deletions wechat_django/admin/messagelog.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin
from django.db import models as m
from django.utils import timezone
Expand Down
4 changes: 3 additions & 1 deletion wechat_django/admin/user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#encoding: utf-8
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django import forms
from django.contrib import admin, messages
from django.utils import timezone
Expand Down
4 changes: 3 additions & 1 deletion wechat_django/admin/wechatapp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#encoding: utf-8
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import re

from django import forms
Expand Down
3 changes: 3 additions & 0 deletions wechat_django/apps.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.apps import AppConfig


Expand Down
7 changes: 5 additions & 2 deletions wechat_django/decorators.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#encoding: utf-8
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from functools import wraps

from django.conf.urls import url
from django.http import response
from django.utils.decorators import available_attrs
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods
from six import text_type

from . import url_patterns
from .models import WeChatApp
Expand All @@ -26,7 +29,7 @@ def wrapped_view(message):

return wraps(view_func, assigned=available_attrs(view_func))(wrapped_view)

if isinstance(names, str):
if isinstance(names, text_type):
names = [names]
elif callable(names):
return decorator(names)
Expand Down
3 changes: 3 additions & 0 deletions wechat_django/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from .msgtype import EventType, MsgType, ReceiveMsgType
from .permission import permissions

Expand Down
6 changes: 3 additions & 3 deletions wechat_django/models/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#encoding: utf-8

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import datetime
Expand All @@ -12,6 +11,7 @@
from django.utils.module_loading import import_string
from django.utils.translation import ugettext as _
from jsonfield import JSONField
from six import text_type

from .. import settings
from ..patches import WeChatOAuth
Expand Down Expand Up @@ -70,7 +70,7 @@ def client(self):
""":rtype: wechatpy.WeChatClient"""
if not hasattr(self, "_client"):
# session
if isinstance(settings.SESSIONSTORAGE, str):
if isinstance(settings.SESSIONSTORAGE, text_type):
settings.SESSIONSTORAGE = import_string(settings.SESSIONSTORAGE)
if callable(settings.SESSIONSTORAGE):
session = settings.SESSIONSTORAGE(self)
Expand Down
4 changes: 3 additions & 1 deletion wechat_django/models/article.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#encoding: utf-8
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models as m, transaction
from django.utils.translation import ugettext as _
from wechatpy.exceptions import WeChatClientException
Expand Down
4 changes: 3 additions & 1 deletion wechat_django/models/material.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#encoding: utf-8
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import mimetypes
import re

Expand Down
4 changes: 3 additions & 1 deletion wechat_django/models/menu.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#encoding: utf-8
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import datetime
from hashlib import md5
import json
Expand Down
4 changes: 3 additions & 1 deletion wechat_django/models/message.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#encoding: utf-8
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from . import WeChatUser


Expand Down
6 changes: 4 additions & 2 deletions wechat_django/models/messagehandler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#encoding: utf-8
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import random

from django.db import models as m, transaction
Expand Down Expand Up @@ -43,7 +45,7 @@ class ReplyStrategy(object):
updated = m.DateTimeField(_("updated"), auto_now=True)

class Meta:
ordering = ("-weight", "-created")
ordering = ("-weight", "-created", "-id")
index_together = (
("app", "weight", "created"),
)
Expand Down
4 changes: 4 additions & 0 deletions wechat_django/models/messagelog.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models as m
from django.utils.translation import ugettext as _
from jsonfield import JSONField
Expand Down Expand Up @@ -28,6 +31,7 @@ def from_msg(cls, message, app=None):
:type message: wechatpy.messages.BaseMessage
:type app: WeChatApp
"""
# TODO: 是否记录原始记录
content = {
key: getattr(message, key)
for key in message._fields
Expand Down
3 changes: 3 additions & 0 deletions wechat_django/models/msgtype.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

class MsgType(object):
TEXT = "text"
IMAGE = "image"
Expand Down
4 changes: 3 additions & 1 deletion wechat_django/models/permission.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#encoding: utf-8
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from collections import defaultdict
import itertools
import re
Expand Down
7 changes: 5 additions & 2 deletions wechat_django/models/reply.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#encoding: utf-8
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models as m
from django.utils.module_loading import import_string
from django.utils.translation import ugettext as _
from jsonfield import JSONField
import requests
from six import text_type
from wechatpy import replies

from ..utils.admin import enum2choices
Expand Down Expand Up @@ -78,7 +81,7 @@ def reply_custom(self, message):
reply = func(WeChatMessage(self.app, message))
if not reply:
return ""
elif isinstance(reply, str):
elif isinstance(reply, text_type):
reply = replies.TextReply(content=reply)
reply.source = message.target
reply.target = message.source
Expand Down
16 changes: 10 additions & 6 deletions wechat_django/models/rule.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#encoding: utf-8
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import re

from django.db import models as m
Expand Down Expand Up @@ -37,21 +39,23 @@ def match(self, message):
"""
:type message: wechatpy.messages.BaseMessage
"""
s_eq = lambda a, b: a.lower() == b.lower()

if self.type == self.Type.ALL:
return True
elif self.type == self.Type.MSGTYPE:
return message.type == self.rule["msg_type"]
elif self.type == self.Type.EVENT:
return (message.type == ReceiveMsgType.EVENT
and message.event == self.rule["event"])
and s_eq(message.event, self.rule["event"]))
elif self.type == self.Type.EVENTKEY:
return (message.type == ReceiveMsgType.EVENT
and message.event == self.rule["event"]
and s_eq(message.event, self.rule["event"])
and hasattr(message, "key")
and message.key == self.rule["key"])
elif self.type == self.Type.CONTAIN:
return (message.type == ReceiveMsgType.TEXT
and message.content.find(self.rule["pattern"]) >= 0)
and self.rule["pattern"] in message.content)
elif self.type == self.Type.EQUAL:
return (message.type == ReceiveMsgType.TEXT
and message.content == self.rule["pattern"])
Expand All @@ -69,6 +73,6 @@ def from_mp(cls, data, handler=None):
)

def __str__(self):
if self.handler:
if hasattr(self, "handler") and self.handler is not None:
return self.handler.name
return super(Rule, self).__str__()
return "<Rule: {0}>".format(self.type)
4 changes: 3 additions & 1 deletion wechat_django/models/user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#encoding: utf-8
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import math
import re

Expand Down
4 changes: 3 additions & 1 deletion wechat_django/oauth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#encoding: utf-8
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from functools import wraps
import logging

Expand Down
4 changes: 3 additions & 1 deletion wechat_django/patches.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#encoding: utf-8
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.utils.http import urlencode
import logging

Expand Down
3 changes: 3 additions & 0 deletions wechat_django/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.conf import settings

ADMINSITE = getattr(settings, "WECHAT_ADMINSITE",
Expand Down
3 changes: 3 additions & 0 deletions wechat_django/tests/bases.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.test import TestCase

from ..models import WeChatApp
Expand Down
3 changes: 3 additions & 0 deletions wechat_django/tests/interceptors.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import json
from httmock import all_requests, HTTMock, response, urlmatch

Expand Down
Loading

0 comments on commit 941acae

Please sign in to comment.