diff --git a/.travis.yml b/.travis.yml index 4349f2d..566a1ab 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,4 +10,4 @@ install: script: - "python secret_key_gen.py" - "python manage.py migrate" - - "flake8 --count --exclude ./docs,./manage_room/diff_match_patch,./manage_room/migrations,./manage_chat/migrations" + - "flake8 --count --ignore=E501,E302,W293,F841 --exclude ./docs,./manage_room/diff_match_patch,./manage_room/migrations,./manage_chat/migrations,./manage.py" diff --git a/coding_night_live/consumers.py b/coding_night_live/consumers.py index 8ed1d62..6a327a6 100644 --- a/coding_night_live/consumers.py +++ b/coding_night_live/consumers.py @@ -1,6 +1,6 @@ import json -from channels import Channel, Group +from channels import Channel from channels.auth import channel_session_user_from_http, channel_session_user from manage_room.models import Room @@ -11,11 +11,13 @@ def ws_connect(message): message.reply_channel.send({'accept': True}) message.channel_session['room'] = [] + def ws_receive(message): payload = json.loads(message['text']) payload['reply_channel'] = message.content['reply_channel'] Channel('room.receive').send(payload) + @channel_session_user def ws_disconnect(message): for room_label in message.channel_session.get("room", set()): @@ -24,4 +26,3 @@ def ws_disconnect(message): room.websocket_group.discard(message.reply_channel) except Room.DoesNotExist: pass - diff --git a/coding_night_live/routing.py b/coding_night_live/routing.py index 9a3aad8..0a6e5d1 100644 --- a/coding_night_live/routing.py +++ b/coding_night_live/routing.py @@ -10,17 +10,7 @@ channel_routing = [ # /Room/ include("coding_night_live.routing.websocket_routing", path=r"^/room/"), - #include("manage_room.routing.websocket_routing", path=r"^/room/user_count/"), include("manage_room.routing.custom_routing"), # /Chat/ - #include("manage_chat.routing.websocket_routing", path=r"^/talk/"), include("manage_chat.routing.custom_routing"), ] - -""" -websocket_routing = [ - route("websocket.connect", ws_connect), - route("websocket.receive", ws_receive), - route("websocket.disconnect", ws_disconnect), -] -""" diff --git a/coding_night_live/settings.py b/coding_night_live/settings.py index d6dd4d6..338b9c1 100644 --- a/coding_night_live/settings.py +++ b/coding_night_live/settings.py @@ -29,6 +29,7 @@ with open(secret_file, 'r') as f: secret = json.loads(f.read()) + def get_secret(setting, secret=secret): try: return secret[setting] @@ -36,6 +37,7 @@ def get_secret(setting, secret=secret): error_msg = "Set the {0} environment variable".format(setting) raise ImproperlyConfigured(error_msg) + SECRET_KEY = get_secret('SECRET_KEY') # SECURITY WARNING: don't run with debug turned on in production! @@ -81,7 +83,7 @@ def get_secret(setting, secret=secret): TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [os.path.join(BASE_DIR, 'templates'),], # templates DIR + 'DIRS': [os.path.join(BASE_DIR, 'templates'), ], # templates DIR 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ @@ -103,9 +105,9 @@ def get_secret(setting, secret=secret): # django social auth setting SOCIALACCOUNT_PROVIDERS = \ - { 'google': - { 'SCOPE': ['profile', 'email'], - 'AUTH_PARAMS': { 'access_type': 'online' } }} + {'google': + {'SCOPE': ['profile', 'email'], + 'AUTH_PARAMS': {'access_type': 'online'}}} # Database # https://docs.djangoproject.com/en/1.10/ref/settings/#databases @@ -117,7 +119,8 @@ def get_secret(setting, secret=secret): } } -redis_host = os.environ.get('REDIS_HOST', 'localhost') # redis host definition +# redis host definition +redis_host = os.environ.get('REDIS_HOST', 'localhost') # channel layer definitions CHANNEL_LAYERS = { @@ -187,4 +190,4 @@ def get_secret(setting, secret=secret): # https://docs.djangoproject.com/en/1.10/howto/static-files/ STATIC_URL = '/static/' -STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'), ] #static files DIR +STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'), ] # static files DIR diff --git a/coding_night_live/views.py b/coding_night_live/views.py index e9765a7..6c54f67 100644 --- a/coding_night_live/views.py +++ b/coding_night_live/views.py @@ -1,5 +1,4 @@ -from django.shortcuts import render_to_response, render, redirect -from django.template import RequestContext +from django.shortcuts import render from django.http import HttpResponseRedirect from django.views.generic.base import TemplateView @@ -7,6 +6,7 @@ # Create your views here. class MainView(TemplateView): template_name = 'main.html' + def dispatch(self, request, *args, **kwargs): if request.user.is_authenticated(): return HttpResponseRedirect('/services/') diff --git a/manage_chat/admin.py b/manage_chat/admin.py index 22297d1..b6a3944 100644 --- a/manage_chat/admin.py +++ b/manage_chat/admin.py @@ -11,6 +11,7 @@ class PollAdmin(admin.ModelAdmin): class ChatAndReplyAdmin(admin.ModelAdmin): list_display = ('room', '_id', 'time', 'description', 'hash_value', 'is_reply') + admin.site.register(Notice, NoticeAdmin) admin.site.register(Poll, PollAdmin) admin.site.register(ChatAndReply, ChatAndReplyAdmin) diff --git a/manage_chat/consumers.py b/manage_chat/consumers.py index 4bdacc5..df75ae7 100644 --- a/manage_chat/consumers.py +++ b/manage_chat/consumers.py @@ -2,10 +2,8 @@ import json from django.db import transaction -from channels import Channel, Group -from channels.auth import channel_session_user_from_http, channel_session_user +from channels.auth import channel_session_user -from manage_room.models import Room from manage_room.consumers import check_admin from manage_room.utils import get_room_or_error, catch_client_error diff --git a/manage_chat/models.py b/manage_chat/models.py index 0137f7e..11168e0 100644 --- a/manage_chat/models.py +++ b/manage_chat/models.py @@ -16,7 +16,7 @@ def _createHash(): return hash.hexdigest()[:7] class Notice(models.Model): - room = models.ForeignKey(Room, on_delete=models.CASCADE) #fk + room = models.ForeignKey(Room, on_delete=models.CASCADE) _id = models.AutoField(primary_key=True) time = models.DateTimeField(default=timezone.now) description = models.TextField() @@ -65,10 +65,10 @@ def websocket_group(self): def start_poll(self, label): final_msg = { - 'start_poll': label, - 'question': self.question, - 'answer': self.answer, - 'hash_value': self.hash_value, + 'start_poll': label, + 'question': self.question, + 'answer': self.answer, + 'hash_value': self.hash_value, } self.websocket_group.send( {"text": json.dumps(final_msg)} @@ -76,11 +76,11 @@ def start_poll(self, label): def result_poll(self, label): final_msg = { - 'result_poll': label, - 'question': self.question, - 'hash_value': self.hash_value, - 'answer': self.answer, # json list - 'answer_count': self.answer_count, # json list + 'result_poll': label, + 'question': self.question, + 'hash_value': self.hash_value, + 'answer': self.answer, # json list + 'answer_count': self.answer_count, # json list } self.websocket_group.send( {"text": json.dumps(final_msg)} diff --git a/manage_chat/routing.py b/manage_chat/routing.py index 21ae40e..cb29ec7 100644 --- a/manage_chat/routing.py +++ b/manage_chat/routing.py @@ -1,13 +1,6 @@ from channels import route from .consumers import new_chat, new_notice, new_poll, end_poll, get_poll -#websocket_routing = [ -# route("websocket.connect", ws_connect), -# route("websocket.receive", ws_receive), -# route("websocket.disconnect", ws_disconnect), -#] - -# Websocket command : custom_routing = [ route("room.receive", new_chat, command="^new_chat$"), route("room.receive", new_notice, command="^notice$"), diff --git a/manage_chat/tests.py b/manage_chat/tests.py deleted file mode 100644 index 7ce503c..0000000 --- a/manage_chat/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/manage_chat/views.py b/manage_chat/views.py index e9b4b0b..1fe23f0 100644 --- a/manage_chat/views.py +++ b/manage_chat/views.py @@ -1,7 +1,3 @@ -import json - -from django.shortcuts import render - from .models import ChatAndReply, Notice, Poll from manage_room.models import Room @@ -24,7 +20,7 @@ def get_notice_list(request): return notices def get_poll_list(request): - #request is room's label + # request is room's label room = Room.objects.get(label=request) polls = Poll.objects.filter(room=room).order_by('time') diff --git a/manage_room/admin.py b/manage_room/admin.py index e782dd7..b13bd97 100644 --- a/manage_room/admin.py +++ b/manage_room/admin.py @@ -8,5 +8,6 @@ class RoomAdmin(admin.ModelAdmin): class SlideAdmin(admin.ModelAdmin): list_display = ('title', 'md_blob', 'now_id', 'next_id') + admin.site.register(Room, RoomAdmin) admin.site.register(Slide, SlideAdmin) diff --git a/manage_room/consumers.py b/manage_room/consumers.py index 35472e7..ee4081f 100644 --- a/manage_room/consumers.py +++ b/manage_room/consumers.py @@ -3,8 +3,8 @@ from django.db import transaction from django.core.cache import cache -from channels import Channel, Group -from channels.auth import channel_session_user_from_http, channel_session_user +from channels import Group +from channels.auth import channel_session_user from .models import Room, Slide @@ -14,27 +14,13 @@ from .diff_match_patch.java_hashcode_conv import javaHash from .diff_match_patch import diff_match_patch -### Chat channel handling ### - -# Channel_session_user loads the user out from the channel session and presents -# it as message.user. There's also a http_session_user if you want to do this on -# a low-level HTTP handler, or just channel_session if all you want is the -# message.channel_session object without the auth fetching overhead. +# Chat channel handling @channel_session_user @catch_client_error def room_join(message): - # Find the room they requested (by ID) and add ourselves to the send group - # Note that, because of channel_session_user, we have a message.user - # object that works just like request.user would. Security! room = get_room_or_error(message["room"]) - - # OK, add them in. The websocket_group is what we'll send messages - # to so that everyone in the chat room gets them. room.websocket_group.add(message.reply_channel) - #message.channel_session['room'] = list(set(message.channel_session['room']).union([room.label])) - # Send a message back that will prompt them to open the room - # Done server-side so that we could, for example, make people - # join rooms automatically. + message.reply_channel.send({ "text": json.dumps({ "join": str(room.label), @@ -60,7 +46,7 @@ def room_leave(message): room = get_room_or_error(message["room"]) room.websocket_group.discard(message.reply_channel) - #message.channel_session['room'] = list(set(message.channel_session['room']).difference([room.label])) + # message.channel_session['room'] = list(set(message.channel_session['room']).difference([room.label])) # Send a message back that will prompt them to close the room message.reply_channel.send({ "text": json.dumps({ @@ -103,7 +89,8 @@ def del_slide(message): if check_admin(message): room = get_room_or_error(message["room"]) is_last = Slide.objects.filter(room=room).count() - if is_last <=2: + + if is_last <= 2: raise ClientError("CANNOT_DELETE_LAST") else: with transaction.atomic(): @@ -322,7 +309,7 @@ def check_admin(message): is_admin = False if not message.user.is_anonymous(): try: - check_admin = Room.objects.get(admin_user=message.user, label=message["room"]) + admin = Room.objects.get(admin_user=message.user, label=message["room"]) is_admin = True except: pass diff --git a/manage_room/models.py b/manage_room/models.py index f8c95e3..6828ba1 100644 --- a/manage_room/models.py +++ b/manage_room/models.py @@ -1,5 +1,4 @@ import json -import allauth from django.db import models from django.utils import timezone @@ -9,12 +8,12 @@ # Create your models here. class Room(models.Model): - admin_user = models.ForeignKey(User, on_delete=models.CASCADE) #fk - #admin_user = models.ForeignKey('auth.User', on_delete=models.CASCADE) - #admin_user = models.ForeignKey(allauth.socialaccount.models.SocialAccount, on_delete=models.CASCADE) + admin_user = models.ForeignKey(User, on_delete=models.CASCADE) + # admin_user = models.ForeignKey('auth.User', on_delete=models.CASCADE) + # admin_user = models.ForeignKey(allauth.socialaccount.models.SocialAccount, on_delete=models.CASCADE) title = models.CharField(max_length=255, default="NoTitle") - link = models.URLField(primary_key=True) #pk + link = models.URLField(primary_key=True) time = models.DateTimeField(default=timezone.now) label = models.SlugField(unique=True) @@ -34,7 +33,7 @@ def send_title(self, new_title): self.title = new_title self.save() - final_msg = {'rename_title': str(self.label), 'title': str(self.title),} + final_msg = {'rename_title': str(self.label), 'title': str(self.title), } self.websocket_group.send( {"text": json.dumps(final_msg)} @@ -44,10 +43,10 @@ class Slide(models.Model): room = models.ForeignKey(Room, on_delete=models.CASCADE) title = models.CharField(max_length=35, default="Unnamed slide") md_blob = models.TextField(default="") - #id linked list - now_id = models.AutoField(primary_key=True) #pk - next_id = models.PositiveSmallIntegerField(default=0) #if 0: last element - #next_id = models.PositiveSmallIntegerField(unique=True) #if 0: last element + # id ; linked list + now_id = models.AutoField(primary_key=True) + next_id = models.PositiveSmallIntegerField(default=0) # if 0: last element + # next_id = models.PositiveSmallIntegerField(unique=True) # if 0: last element def __str__(self): return str(self.now_id) @@ -61,7 +60,7 @@ def websocket_group(self): return Group(self.room.label) def send_idx(self, command): - final_msg = {command: str(self.now_id),} # Return idx + final_msg = {command: str(self.now_id), } # Return idx self.websocket_group.send( {"text": json.dumps(final_msg)} diff --git a/manage_room/tests.py b/manage_room/tests.py deleted file mode 100644 index 7ce503c..0000000 --- a/manage_room/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/manage_room/utils.py b/manage_room/utils.py index 4d91499..9356b56 100644 --- a/manage_room/utils.py +++ b/manage_room/utils.py @@ -18,7 +18,6 @@ def inner(message, *args, **kwargs): return inner -#def get_room_or_error(room_label, user): def get_room_or_error(room_label): """ Tries to fetch a room for the user, checking permissions along the way. diff --git a/manage_room/views.py b/manage_room/views.py index 84362cd..26f761f 100644 --- a/manage_room/views.py +++ b/manage_room/views.py @@ -1,13 +1,10 @@ # /services/list.html - manage the room (create, delete, ..) -import random +from django.http import HttpResponse, HttpResponseRedirect -from django.http import HttpResponse, HttpResponseRedirect, HttpRequest - -from django.shortcuts import render, get_object_or_404 +from django.shortcuts import render from django.db import transaction from django.views.generic.base import TemplateView from django.contrib.sites.models import Site -from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.decorators import login_required from haikunator import Haikunator @@ -21,7 +18,7 @@ def RoomCreateView(request): url = 'http://' + Site.objects.get_current().domain + '/' room = None - + while not room: with transaction.atomic(): share_link = Haikunator.haikunate() # Ex) 'icy-dream-4198' @@ -69,7 +66,7 @@ def MarkdownToPdfView(request, label): class RedirectRoomView(TemplateView): - template_name='room.html' + template_name = 'room.html' def get_context_data(self, **kwargs): label = self.request.path @@ -111,7 +108,7 @@ def get_context_data(self, **kwargs): is_admin = False if not self.request.user.is_anonymous(): try: - check_admin = Room.objects.get(label=label, admin_user=self.request.user) #check admin user + admin = Room.objects.get(label=label, admin_user=self.request.user) # check admin user is_admin = True except: # Matching query does not exist - request.user is not a admin_user