Commits

Shinya Okano  committed 4142a9f

最初のコミット

  • Participants

Comments (0)

Files changed (8)

+syntax: glob
+*.pyc

File skypehub/__init__.py

Empty file added.

File skypehub/decorators.py

+from django.conf import settings
+
+from skypehub.utils import get_skype
+
+def skype_required(view):
+    def _inner(request, *args, **kwargs):
+        hook_options = getattr(settings, 'SKYPE_HOOK_OPTIONS',  {})
+        skype = get_skype(**hook_options)
+        return view(request, skype, *args, **kwargs)
+    return _inner

File skypehub/forms.py

+from django import forms
+
+class PostMessageForm(forms.Form):
+    message = forms.CharField()
+    chat = forms.CharField()

File skypehub/models.py

Empty file added.

File skypehub/urls.py

+from django.conf.urls.defaults import *
+
+urlpatterns = patterns('skypehub.views',
+    url(r'^list_chats$', 'list_chats', name='skype_list_chats'),
+    url(r'^post_message', 'post_message', name='skype_post_message'),
+)

File skypehub/utils.py

+import Skype4Py
+
+SKYPE_HOOK_OPTIONS = {
+    'Transport': 'x11',
+}
+
+def get_skype(**kwargs):
+    options = kwargs or SKYPE_HOOK_OPTIONS
+    return Skype4Py.Skype(**options)

File skypehub/views.py

+from django.http import HttpResponse
+
+try:
+    import json
+except ImportError:
+    from django.utils import simplejson as json
+
+from skypehub.decorators import skype_required
+from skypehub.forms import PostMessageForm
+
+def make_json_response(content):
+    return HttpResponse(json.dumps(content), content_type='application/javascript')
+
+@skype_required
+def list_chats(request, skype):
+    chats = []
+    for chat in skype.Chats:
+        chats.append({
+            'name': chat.Name,
+            'topic': chat.Topic,
+        })
+    return make_json_response({'chats': chats})
+
+@skype_required
+def post_message(request, skype):
+    form = PostMessageForm(request.POST or None)
+    if form.is_valid():
+        chat = skype.Chat(form.cleaned_data['chat'])
+        chat.SendMessage(form.cleaned_data['message'])
+        return make_json_response({'result': 'ok'})
+    return  make_json_response({'result': 'validation error.'})