Skip to content

Commit

Permalink
Merge pull request #91 from dduk-ddak/issue_85
Browse files Browse the repository at this point in the history
Issue 85 Done!
  • Loading branch information
juice500ml authored Mar 18, 2017
2 parents 69441a0 + eed8838 commit 0898cba
Show file tree
Hide file tree
Showing 14 changed files with 258 additions and 28 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,9 @@ ENV/
*.rdb
*.swp

collected_static/

# For hide SECRET_KEY
secret.json
pw.txt

2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ install:
- "pip install flake8"

script:
- "python secret_key_gen.py"
- "python manage.py migrate"
- "python manage.py createsuperuserauto"
- "flake8 --count --ignore=E501,E302,W293,F841 --exclude ./docs,./manage_room/diff_match_patch,./manage_room/migrations,./manage_chat/migrations,./manage.py"
46 changes: 46 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
default: start

sudo:
sudo -v

start: prepare sudo
sudo service redis-server start
python3 manage.py runworker &
daphne -b 0.0.0.0 -p 8001 coding_night_live.asgi:channel_layer &
sudo service nginx start # FIXME

db.sqlite3:
python3 manage.py migrate

pw.txt: db.sqlite3
python3 manage.py createsuperuserauto

collected_static/:
yes yes | python3 manage.py collectstatic

secret.json: db.sqlite3
python3 manage.py autodeploy

prepare: deps-install db.sqlite3 pw.txt collected_static/ secret.json


OS := $(shell uname)
deps-install:
ifeq ($(OS),Linux)
sudo apt-get install redis-server
sudo apt-get install nginx
else
echo 'ACITON REQUIRED) Need to install redis and nginx before this.'
endif

stop: sudo
-sudo service nginx stop
-sudo killall -9 daphne # FIXME
-sudo killall -9 python3 # FIXME
-sudo service redis-server stop

clean:
-rm secret.json db.sqlite3
-rm -r collected_static

uninstall: stop clean
56 changes: 56 additions & 0 deletions coding_night_live/management/commands/autodeploy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import json

from django.core.management.base import BaseCommand
from django.contrib.sites.models import Site
from django.utils.crypto import get_random_string

from allauth.socialaccount.models import SocialApp


class Command(BaseCommand):

def open_secret(self):
print('* Please write your OAuth Client ID')
client_id = input('>')
print('** Please write your OAuth Secret')
secret = input('>')
print('*** Please write your Server Domain (ex. example.com)')
domain = input('>')

chars = 'qwertyuiopasdfghjklzxcvbnm0987654321!@#$%^&*(-_=+)'
SECRET_KEY = get_random_string(50, chars)

result = {}

result['CLIENT_ID'] = str(client_id)
result['SECRET'] = str(secret)
result['DOMAIN'] = str(domain)
result['SECRET_KEY'] = SECRET_KEY

with open('secret.json', 'w') as f:
json.dump(result, f)
with open('secret.json', 'r') as f:
secret = json.loads(f.read())
self.social_app_setting(secret['DOMAIN'], secret['CLIENT_ID'], secret['SECRET'])

def social_app_setting(self, domain, client_id, secret):
default_site_1 = Site.objects.get(id=1)
default_site_1.domain = domain
default_site_1.name = domain
default_site_1.save()

new_social_app = SocialApp(
id=1,
provider='google',
name=domain,
client_id=client_id,
secret=secret,
key='',
)

new_social_app.save()
new_social_app.sites.add(default_site_1)
new_social_app.save()

def handle(self, *args, **options):
self.open_secret()
5 changes: 5 additions & 0 deletions coding_night_live/management/commands/createsuperuserauto.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ class Command(BaseCommand):

def handle(self, *args, **options):
password = get_random_string()

pwfile = open('pw.txt', 'w')
pwfile.write(password)
pwfile.close()

print('Generated root password : %s' % (password,))
admin = User.objects.create_superuser(
email='root@localhost',
Expand Down
11 changes: 6 additions & 5 deletions coding_night_live/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
secret_file = os.path.join(BASE_DIR, 'secret.json')

with open(secret_file, 'r') as f:
secret = json.loads(f.read())

try:
secret_file = os.path.join(BASE_DIR, 'secret.json')
with open(secret_file, 'r') as f:
secret = json.loads(f.read())
except FileNotFoundError:
secret = {'SECRET_KEY': True}

def get_secret(setting, secret=secret):
try:
Expand Down
3 changes: 2 additions & 1 deletion coding_night_live/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
from django.conf.urls import url, include
from django.contrib import admin

from coding_night_live.views import MainView, PageNotFound
from coding_night_live.views import MainView, PageNotFound, withdraw

import manage_room.views

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
url(r'^services/withdraw/$', withdraw, name='withdraw'),
url(r'^$', MainView.as_view(), name='main'),
url(r'^admin/', admin.site.urls),
url(r'^accounts/', include('allauth.urls')),
Expand Down
8 changes: 8 additions & 0 deletions coding_night_live/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.shortcuts import render

from django.http import HttpResponseRedirect
from django.contrib.auth.models import User
from django.views.generic.base import TemplateView

# Create your views here.
Expand All @@ -14,3 +15,10 @@ def dispatch(self, request, *args, **kwargs):

def PageNotFound(request):
return render(request, '404.html', status=404)

def withdraw(request):
if request.method == 'POST':
User.objects.get(email=request.user.email).delete()
else:
print('Request method is not a GET.')
return HttpResponseRedirect('/')
55 changes: 55 additions & 0 deletions deploy_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import os
import sys


# Check OS
platform = sys.platform
if platform in ('win32', 'win64'):
print('Error: Cannot run in Windows..')
exit(0)

cmd = 'python3'

# Check Python Version (3 or 2)
if sys.version_info[0] == 2:
print('Error: Cannot run in Python 2.x..')
exit(-1)

# Install python packages
try:
import pip
except ImportError:
print("Installing pip...")
if platform == 'linux':
os.system('sudo apt-get install python3-pip')
import pip
pip.main(['install', '-r', 'requirements.txt'])

# DB Migration
os.system('%s manage.py migrate' % cmd)

# Admin user setting
os.system('%s manage.py createsuperuserauto' % cmd)

# Install redis-server / nginx
if platform == 'linux':
os.system('sudo apt-get install redis-server')
os.system('sudo apt-get install nginx')

# Find nginx location
# nginx = subprocess.checkoutput('sudo find / -name nginx.conf', shell=True)

# Server Deploy
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
os.system('sudo rm -rf /etc/nginx/sites-enabled/local_nginx.conf')
os.system('sudo ln -s %s/nginx/local_nginx.conf /etc/nginx/sites-enabled/' % BASE_DIR)

# OAuth setting
os.system('%s manage.py collectstatic' % cmd)
os.system('%s manage.py autodeploy' % cmd)

# Server run
os.system('redis-server &')
os.system('%s manage.py runworker &' % cmd)
os.system('daphne -b 0.0.0.0 -p 8001 coding_night_live.asgi:channel_layer &')
os.system('sudo service nginx start')
6 changes: 3 additions & 3 deletions local_nginx.conf → nginx/local_nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@

server {
listen 80;
server_name localhost;
server_name coding-night-live.japaneast.cloudapp.azure.com;
# ex1) server_name localhost;
# ex2) server_name coding-night-live.cloudapp.net;
charset utf-8;
client_max_body_size 20M;

location /static/ {
alias /opt/coding-night-live/collected_static/; # Please edit this line!
alias /home/punk/coding-night-live/collected_static/; # Please edit this line!
# ex) alias /home/punk/Documents/coding-night-live/collected_static/;
}

location / {
#localhost deploy -> proxy_pass http:/localhost:8000;
# 80 deploy -> proxy_pass http://0.0.0.0:8000;
proxy_pass http://localhost:8000;
proxy_pass http://0.0.0.0:8001;

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ asgi_redis
pyhaikunator
django-redis
raven
Twisted==16.0.0 # Issue #54. Not worked with Twisted==17.0.0
Twisted==16.0.0 # Issue #54. Not worked with Twisted==17.0.0
15 changes: 0 additions & 15 deletions secret_key_gen.py

This file was deleted.

38 changes: 38 additions & 0 deletions templates/505.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Server Error</title>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js" integrity="sha384-vZ2WRJMwsjRMW/8U7i6PWi6AlO1L79snBrmgiDpgIWJ82z8eA5lenwvxbMV1PAh7" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" crossorigin="anonymous">

<style>
.full{
background: white no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
-o-background-size: cover;
height:100%;
}
.box {
background: #b4dbfd;
display: block;
margin-left: auto;
margin-right: auto;
width: 70%;
max-width:400px;
}
</style>
</head>

<body class="container-fluid full">
<div class="jumbotron box" style="margin-top:30vh">
<h1>Server Error!</h1>
<small>Error 500</small>
</div>
</body>
</html>
35 changes: 33 additions & 2 deletions templates/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ <h4 class="modal-title">Are you sure to delete this room?</h4>
</div>
</div>

<!-- Account Withdraw modal -->
<div class="modal fade" id="account_withdraw_modal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Are you sure you want to withdraw from your account?</h4>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal" onclick="withdrawAccount()">Withdrawal</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>

<nav class="navbar navbar-light bg-faded" style="background-color:#0275d8; border-radius:0em;">
<a class="navbar-brand" href="#">
<img src="{% static "image/small-logo-point.png" %}" height="30vh">
Expand All @@ -61,6 +76,7 @@ <h4 class="modal-title">Are you sure to delete this room?</h4>
{% load account %}
<a class="nav-link dropdown-toggle" href="#" id="logged_dropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" style="color:white">Welcome, <b>{% user_display user %}</b></a>
<div class="dropdown-menu" aria-labelledby="logged_dropdown">
<a class="dropdown-item" href="#" onclick="withdrawAccountModal()">Withdraw</a>
<a class="dropdown-item" href="{% url 'logout' %}">Logout</a>
</div>
</li>
Expand All @@ -79,7 +95,7 @@ <h4 class="list-group-item-heading" align="center"><i class="fa fa-plus" aria-hi
<div class="list-group-item list-group-item-action">
<h4 class="list-group-item-heading"><a class="room-link-element" href="{{ room.link }}">{{ room.title }}</a></h4>
<span class="tag tag-danger tag-pill float-xs-right">
<a href="#" class="room-delete-element" onclick="deleteRoom('{% url 'delete' pk=room.label %}')">Delete</a>
<a href="#" class="room-delete-element" onclick="deleteRoomModal('{% url 'delete' pk=room.label %}')">Delete</a>
</span>
<p class="list-group-item-text">Generated at {{ room.time }}</p>
</div>
Expand All @@ -90,10 +106,25 @@ <h4 class="list-group-item-heading"><a class="room-link-element" href="{{ room.l
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js" integrity="sha384-vZ2WRJMwsjRMW/8U7i6PWi6AlO1L79snBrmgiDpgIWJ82z8eA5lenwvxbMV1PAh7" crossorigin="anonymous"></script>

<script>
function deleteRoom(link) {
function deleteRoomModal(link) {
$('#modal_delete_button').attr('onclick', 'location.href="' + link + '"');
$('#room_delete_modal').modal('show');
}

function withdrawAccountModal() {
$('#account_withdraw_modal').modal('show');
}

function withdrawAccount() {
$.ajax({
url: "{% url 'withdraw' %}",
type: "POST",
data: { csrfmiddlewaretoken: "{{ csrf_token }}", },
success: function() {
location.href="/";
}
});
}
</script>
</body>
</html>

0 comments on commit 0898cba

Please sign in to comment.