Skip to content

Commit

Permalink
Merge pull request #337 from SWLBot/dev
Browse files Browse the repository at this point in the history
New release merge from dev
  • Loading branch information
Billy4195 authored Jan 28, 2018
2 parents d7834d0 + 24dd626 commit 3fbeb3f
Show file tree
Hide file tree
Showing 11 changed files with 483 additions and 391 deletions.
327 changes: 166 additions & 161 deletions arrange_schedule.py

Large diffs are not rendered by default.

91 changes: 47 additions & 44 deletions broadcast_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,56 @@
import os.path
import json

def getDisplayContent(sche_target_id,return_msg):
#get type from target id prefix
targetIdPrefix = sche_target_id[:4]
if targetIdPrefix == "imge":
with ImageDao() as imageDao:
file_info = imageDao.getIdSysName(Id=sche_target_id)
return_msg["file_type"] = "image"
elif targetIdPrefix == "text":
with TextDao() as textDao:
file_info = textDao.getIdSysName(Id=sche_target_id)
return_msg["file_type"] = "text"
else :
return_msg["error"] = "target id type error {}".format(targetIdPrefix)
return return_msg

try:
type_id = file_info['typeId']
system_file_name = file_info['systemName']
return_msg["like_count"] = file_info['likeCount']
except:
return_msg["error"] = "no file record"
return return_msg

with DataTypeDao() as dataTypeDao:
type_dir = dataTypeDao.getTypeDir(typeId=type_id)
type_name = dataTypeDao.getTypeName(typeId=type_id)
if type_dir == None or type_name == None:
return_msg["error"] = "No such type id {}".format(type_id)
return return_msg

targetFile = os.path.join("static", type_dir, system_file_name)
return_msg["file"] = os.path.join(type_dir, system_file_name)
return_msg["type_name"] = type_name

#if text read file
if return_msg["file_type"] == "text":
if not os.path.isfile(targetFile) :
return_msg["error"] = "no file"
return return_msg
else :
with open(targetFile,"r") as fp:
file_content = json.load(fp)
return_msg["file_text"] = file_content
return return_msg

#The API load schedule.txt and find out the first image which has not print and the time limit still allow
def load_schedule():
try:
return_msg = {}
return_msg["result"] = "fail"
schedule_dir = ""
sche_target_id = ""
type_id = ""
system_file_name = ""

#find next schedule
with ScheduleDao() as scheduleDao:
Expand All @@ -27,47 +68,9 @@ def load_schedule():
sche_target_id = next_schedule['sche_target_id']
return_msg["display_time"] = int(next_schedule['display_time'])

#find the file
if sche_target_id[0:4]=="imge":
with ImageDao() as imageDao:
file_info = imageDao.getIdSysName(Id=sche_target_id)
return_msg["file_type"] = "image"
elif sche_target_id[0:4]=="text":
with TextDao() as textDao:
file_info = textDao.getIdSysName(Id=sche_target_id)
return_msg["file_type"] = "text"
else :
return_msg["error"] = "target id type error"
return_msg = getDisplayContent(sche_target_id,return_msg)
if "error" in return_msg:
return return_msg
try:
type_id = int(file_info['typeId'])
system_file_name = file_info['systemName']
return_msg["like_count"] = int(file_info['likeCount'])
except:
return_msg["error"] = "no file record"
return return_msg

#find type dir
with DataTypeDao() as dataTypeDao:
type_dir = dataTypeDao.getTypeDir(typeId=type_id)
type_name = dataTypeDao.getTypeName(typeId=type_id)
try:
schedule_dir = os.path.join(schedule_dir, "static", type_dir, system_file_name)
return_msg["file"] = os.path.join(type_dir, system_file_name)
return_msg["type_name"] = str(type_name)
except:
return_msg["error"] = "no type record"
return return_msg

#if text read file
if return_msg["file_type"] == "text":
if not os.path.isfile(schedule_dir) :
return_msg["error"] = "no file"
return return_msg
else :
with open(schedule_dir,"r") as fp:
file_content = json.load(fp)
return_msg["file_text"] = file_content

#update display count
if return_msg["file_type"] == "image":
Expand Down
5 changes: 4 additions & 1 deletion display_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
from datetime import timedelta
from dataAccessObjects import *
import os.path
#

def get_user_id(user_name):
"""
Get user id from database by user name
"""
try:
return_msg = {}
result = ""
Expand Down
14 changes: 14 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "update.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#

FROM ubuntu:16.04

RUN apt-get update; \
apt-get install -y tmux python3 python3-pip mysql-client-core-5.7 git;

ENV LC_ALL C.UTF-8

CMD ["python3"]
4 changes: 4 additions & 0 deletions docker/clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

sudo docker rm EB -f
sudo docker rm EB-mysql -f
3 changes: 3 additions & 0 deletions docker/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sudo docker run -d --name=EB-mysql --env=MYSQL_ROOT_PASSWORD=mypassword mysql:5.7
echo -e "EB-mysql\nroot\nmypassword\nblackboard" > ../mysql_auth.txt
sudo docker run --name=EB -it --link EB-mysql:mysql -v "$PWD":/home/EB -w /home/EB -p 3000:3000 -p 4000:4000 billy4195/electronic-blackboard bash
4 changes: 4 additions & 0 deletions docker/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

pip3 install -r requirements.txt
pytest test/
15 changes: 7 additions & 8 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,22 @@ def get(self):
def post(self):
send_msg = {}
receive_msg = {}
upload_path = os.path.join(os.path.dirname(__file__),"static/img")
thumbnail_path = os.path.join(os.path.dirname(__file__),"static/thumbnail")
upload_path = os.path.join(os.path.dirname(__file__),"static","img")
thumbnail_filepath = os.path.join(os.path.dirname(__file__),"static","thumbnail")
send_msg = get_upload_meta_data(self)
send_msg["file_dir"] = upload_path
if self.get_argument('type') == 'image':
try:
file_metas=self.request.files['file']
for meta in file_metas:
filename=meta['filename']
filepath=os.path.join(upload_path,filename)
send_msg["file_dir"] = filepath
filepath = os.path.join(upload_path,meta['filename'])
send_msg["filepath"] = filepath
store_image(filepath,meta['body'])
receive_msg = upload_image_insert_db(send_msg)

filepath = receive_msg["img_system_dir"]
thumbnail_path=os.path.join(thumbnail_path,receive_msg["img_thumbnail_name"])
store_thumbnail_image(filepath,thumbnail_path)
filepath = receive_msg["img_system_filepath"]
thumbnail_filepath=os.path.join(thumbnail_filepath,receive_msg["img_thumbnail_name"])
store_thumbnail_image(filepath,thumbnail_filepath)

data_types = display_data_types()
self.render("upload.html",flash="Upload finish!",data_types=data_types)
Expand Down
73 changes: 38 additions & 35 deletions server_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,29 @@
from dataAccessObjects import *

class ArgumentUtil():
"""Provide the interface to get argument from handler
This class is the base class defines the basic method for getting
the argument(s).
"""
def __init__(self,requestHandler):
"""
Store request handler as the class variable.
"""
self.handler = requestHandler

def getArgument(self,name):
"""
Get one argument from request handler and transform some
character for security reason.
"""
rawArg = self.handler.get_argument(name)
return xhtml_escape(rawArg)

def getArguments(self):
"""
This method should be implement in derived class.
"""
raise NotImplementedError("The getArgument() is not implemented.")

class UserArgumentsUtil(ArgumentUtil):
Expand Down Expand Up @@ -120,31 +135,19 @@ def add_now_like_count():
return return_msg
def register_preference(data):
try:
inside_type = str(display_data_type(type_name='inside')[0])
techOrange_type = str(display_data_type(type_name='techOrange')[0])
medium_type = str(display_data_type(type_name='medium')[0])
pttBeauty_type = str(display_data_type(type_name='pttBeauty')[0])
pttjoke_type = str(display_data_type(type_name='pttjoke')[0])
pttStupidClown_type = str(display_data_type(type_name='pttStupidClown')[0])
data_types = dict()
data_types['inside'] = str(display_data_type(type_name='inside')[0])
data_types['techOrange'] = str(display_data_type(type_name='techOrange')[0])
data_types['medium'] = str(display_data_type(type_name='medium')[0])
data_types['pttBeauty'] = str(display_data_type(type_name='pttBeauty')[0])
data_types['pttjoke'] = str(display_data_type(type_name='pttjoke')[0])
data_types['pttStupidClown'] = str(display_data_type(type_name='pttStupidClown')[0])

pref_str = ""
if "all" in data["user_preference"]:
pref_str = inside_type+" "+medium_type+" "+pttBeauty_type+" "+pttStupidClown_type+" "+pttjoke_type+" "+techOrange_type
pref_str = " ".join(data_types.values())
else:
if "inside" in data["user_preference"]:
pref_str = pref_str + inside_type + " "
if "techOrange" in data["user_preference"]:
pref_str = pref_str + techOrange_type + " "
if "medium" in data["user_preference"]:
pref_str = pref_str + medium_type + " "
if "pttBeauty" in data["user_preference"]:
pref_str = pref_str + pttBeauty_type + " "
if "pttjoke" in data["user_preference"]:
pref_str = pref_str + pttjoke_type + " "
if "pttStupidClown" in data["user_preference"]:
pref_str = pref_str + pttStupidClown_type + " "
if len(pref_str)>0:
pref_str = pref_str[:-1]
pref_str = " ".join([data_types[key] for key in data["user_preference"]])

#generate new id
try:
Expand Down Expand Up @@ -603,7 +606,7 @@ def upload_image_insert_db(json_obj):
try:
server_dir = json_obj["server_dir"]
type_id = json_obj["file_type"]
img_file_dir = json_obj["file_dir"]
img_filepath = json_obj["filepath"]
img_start_date = json_obj["start_date"]
img_end_date = json_obj["end_date"]
img_start_time = json_obj["start_time"]
Expand All @@ -615,7 +618,7 @@ def upload_image_insert_db(json_obj):
return return_msg

img_id = ""
img_file_name = os.path.split(img_file_dir)[1]
img_file_name = os.path.split(img_filepath)[1]
user_level_low_bound = 100
#default
if len(img_start_time)==0:
Expand All @@ -639,15 +642,15 @@ def upload_image_insert_db(json_obj):

img_system_name = img_id + os.path.splitext(img_file_name)[1]
img_thumbnail_name = "thumbnail_" + img_system_name
img_system_dir = os.path.join(img_type_dir, img_system_name)
img_system_filepath = os.path.join(img_type_dir, img_system_name)
try:
copyfile(img_file_dir, img_system_dir)
if os.path.isfile(img_file_dir) and os.path.isfile(img_system_dir):
os.remove(img_file_dir)
copyfile(img_filepath, img_system_filepath)
if os.path.isfile(img_filepath) and os.path.isfile(img_system_filepath):
os.remove(img_filepath)
except:
try:
if os.path.isfile(img_file_dir) and os.path.isfile(img_system_dir):
os.remove(img_system_dir)
if os.path.isfile(img_filepath) and os.path.isfile(img_system_filepath):
os.remove(img_system_filepath)
except:
"DO NOTHING"
return_msg["error"] = "copy or remove file error"
Expand All @@ -670,18 +673,18 @@ def upload_image_insert_db(json_obj):
with ImageDao() as imageDao:
imageDao.insertData(data=img_data)
except DB_Exception as e:
return_msg["error"] = "insert mysql error please check file system " + img_system_dir
return_msg["error"] = "insert mysql error ({filename}) {msg}".format(filename=img_filepath,msg=str(e))
try:
copyfile(img_system_dir, img_file_dir)
if os.path.isfile(img_file_dir) and os.path.isfile(img_system_dir):
os.remove(img_system_dir)
return_msg["error"] = "insert mysql error please check file system " + img_file_dir
copyfile(img_system_filepath, img_filepath)
if os.path.isfile(img_filepath) and os.path.isfile(img_system_filepath):
os.remove(img_system_filepath)
return_msg["error"] = "insert mysql error ({filename}) {msg}".format(filename=img_filepath,msg=str(e))
except:
"DO NOTHING"
return return_msg

return_msg["img_id"] = img_id
return_msg["img_system_dir"] = img_system_dir
return_msg["img_system_filepath"] = img_system_filepath
return_msg["img_thumbnail_name"] = img_thumbnail_name
return_msg["result"] = "success"
return return_msg
Expand Down
Loading

0 comments on commit 3fbeb3f

Please sign in to comment.