aboutsummaryrefslogtreecommitdiffstats
path: root/src/py
diff options
context:
space:
mode:
authorpzread <netfirewall@gmail.com>2013-06-26 01:49:13 +0800
committerpzread <netfirewall@gmail.com>2013-06-26 01:49:13 +0800
commit17c8c94e097018ccaf15f8a9296b03b5195cc3f7 (patch)
treef3c32a7a7a272f0ab0c2236928c4b78e36846d56 /src/py
parente0043639746fdbbf4958c67a3ef55c63c2fe51a5 (diff)
downloadtaiwan-online-judge-17c8c94e097018ccaf15f8a9296b03b5195cc3f7.tar
taiwan-online-judge-17c8c94e097018ccaf15f8a9296b03b5195cc3f7.tar.gz
taiwan-online-judge-17c8c94e097018ccaf15f8a9296b03b5195cc3f7.tar.bz2
taiwan-online-judge-17c8c94e097018ccaf15f8a9296b03b5195cc3f7.tar.lz
taiwan-online-judge-17c8c94e097018ccaf15f8a9296b03b5195cc3f7.tar.xz
taiwan-online-judge-17c8c94e097018ccaf15f8a9296b03b5195cc3f7.tar.zst
taiwan-online-judge-17c8c94e097018ccaf15f8a9296b03b5195cc3f7.zip
Add square page. Add AsyncMCD. Add sqmod manage
Diffstat (limited to 'src/py')
-rw-r--r--src/py/asyncmcd.py143
-rwxr-xr-xsrc/py/backend_server.py29
-rwxr-xr-xsrc/py/center_server.py10
-rw-r--r--src/py/mail.py33
-rw-r--r--src/py/mod.py50
-rw-r--r--src/py/notice.py16
-rw-r--r--src/py/square.py323
-rwxr-xr-xsrc/py/user.py18
8 files changed, 483 insertions, 139 deletions
diff --git a/src/py/asyncmcd.py b/src/py/asyncmcd.py
new file mode 100644
index 0000000..0a4c0e6
--- /dev/null
+++ b/src/py/asyncmcd.py
@@ -0,0 +1,143 @@
+import socket
+import struct
+import json
+
+import tornado.ioloop
+import tornado.stack_context
+import tornado.iostream
+
+import imc.async
+
+class AsyncMCD:
+ def __init__(self):
+ def _conn():
+ print('conn')
+
+ self.TYPE_INT = 0
+ self.TYPE_BYTES = 1
+ self.TYPE_STR = 2
+ self.TYPE_JSON = 3
+
+ self._ioloop = tornado.ioloop.IOLoop.instance()
+ self._opaque_count = 0
+ self._opaque_map = {}
+
+ self._stream = tornado.iostream.IOStream(socket.socket(socket.AF_INET,socket.SOCK_STREAM,0))
+ self._stream.connect(('10.8.0.6',11211),_conn)
+
+ self._recv_loop()
+
+ def get(self,key):
+ def _recv(opcode,status,opaque,cas,extra,key,value):
+ del self._opaque_map[opaque]
+
+ if status != 0:
+ imc.async.ret(retid,None)
+
+ else:
+ flag, = struct.unpack('!I',extra)
+ if flag == self.TYPE_INT:
+ ret, = struct.unpack('!Q',value)
+
+ elif flag == self.TYPE_BYTES:
+ ret = value
+
+ elif flag == self.TYPE_STR:
+ ret = value.decode('utf-8')
+
+ elif flag == self.TYPE_JSON:
+ ret = json.loads(value.decode('utf-8'))
+
+ imc.async.ret(retid,ret)
+
+ if not isinstance(key,str):
+ raise TypeError
+
+ key = bytes(key,'utf-8')
+ keylen = len(key)
+
+ opaque = self._get_opaque(_recv)
+ header = self._request_header(0x00,keylen,0,0,keylen,opaque,0)
+ data = bytes(bytearray().join([header,key]))
+
+ self._stream.write(data)
+
+ retid = imc.async.get_retid()
+ return imc.async.switch_top()
+
+ def set(self,key,value,expiration = 0):
+ def _recv(opcode,status,opaque,cas,extra,key,value):
+ del self._opaque_map[opaque]
+ imc.async.ret(retid,status)
+
+ if not isinstance(key,str):
+ raise TypeError
+
+ key = bytes(key,'utf-8')
+ keylen = len(key)
+
+ if isinstance(value,int):
+ value_type = self.TYPE_INT
+ value = struct.pack('!Q',value)
+
+ elif isinstance(value,bytes):
+ value_type = self.TYPE_BYTES
+
+ elif isinstance(value,str):
+ value_type = self.TYPE_STR
+ value = bytes(value,'utf-8')
+
+ else:
+ value_type = 2
+ value = bytes(json.dumps(value),'utf-8')
+
+ valuelen = len(value)
+
+ extra = struct.pack('!II',value_type,expiration)
+ extralen = len(extra)
+
+ opaque = self._get_opaque(_recv)
+ header = self._request_header(0x01,keylen,extralen,0,extralen + keylen + valuelen,opaque,0)
+ data = bytes(bytearray().join([header,extra,key,value]))
+
+ self._stream.write(data)
+
+ retid = imc.async.get_retid()
+ return imc.async.switch_top()
+
+ def _get_opaque(self,data):
+ self._opaque_count += 1
+ self._opaque_map[self._opaque_count] = data
+
+ return self._opaque_count
+
+ def _request_header(self,opcode,keylen,extralen,vid,totallen,opaque,cas):
+ return struct.pack('!BBHBBHIIQ',0x80,opcode,keylen,extralen,0x0,vid,totallen,opaque,cas)
+
+ def _recv_loop(self):
+ def __recv(data):
+ def ___recvdata(data):
+ extra = data[0:extralen]
+ key = data[extralen:extralen + keylen]
+ value = data[extralen + keylen:totallen]
+
+ self._opaque_map[opaque](opcode,status,opaque,cas,extra,key,value)
+ self._stream.read_bytes(24,__recv)
+
+ header = struct.unpack('!BBHBBHIIQ',data)
+ opcode = header[1]
+ keylen = header[2]
+ extralen = header[3]
+ status = header[5]
+ totallen = header[6]
+ opaque = header[7]
+ cas = header[8]
+
+ if totallen == 0:
+ self._opaque_map[opaque](opcode,status,opaque,cas,bytes(),bytes(),bytes())
+ self._stream.read_bytes(24,__recv)
+
+ else:
+ self._stream.read_bytes(totallen,___recvdata)
+
+ self._stream.read_bytes(24,__recv)
diff --git a/src/py/backend_server.py b/src/py/backend_server.py
index b9bb07b..71c3b1e 100755
--- a/src/py/backend_server.py
+++ b/src/py/backend_server.py
@@ -12,16 +12,13 @@ import tornado.tcpserver
import tornado.httpserver
import tornado.websocket
-from imc import auth
import imc.async
from imc.proxy import Proxy,Connection
+import mod
import netio
from netio import SocketStream,SocketConnection,WebSocketConnection
from tojauth import TOJAuth
-from notice import Notice
-from user import UserMg
-from mail import Mail
class BackendWorker(tornado.tcpserver.TCPServer):
def __init__(self,center_addr,ws_port):
@@ -68,7 +65,7 @@ class BackendWorker(tornado.tcpserver.TCPServer):
@imc.async.caller
def _call():
with TOJAuth.change_current_iden(self._idendesc):
- Proxy.instance.call(self.center_conn.link,'add_client',10000,link,self._link)
+ Proxy.instance.call(self.center_conn.link + 'core/','add_client',10000,link,self._link)
self._client_linkmap[link] = {}
@@ -84,7 +81,7 @@ class BackendWorker(tornado.tcpserver.TCPServer):
@imc.async.caller
def _call():
with TOJAuth.change_current_iden(self._idendesc):
- Proxy.instance.call(self.center_conn.link,'del_client',10000,link,self._link)
+ Proxy.instance.call(self.center_conn.link + 'core/','del_client',10000,link,self._link)
del self._client_linkmap[link]
@@ -111,20 +108,19 @@ class BackendWorker(tornado.tcpserver.TCPServer):
self.center_conn.add_close_callback(__retry)
Proxy.instance.add_conn(self.center_conn)
- Proxy.instance.register_call('test/','get_client_list',self._test_get_client_list)
- Proxy.instance.register_call('test/','test_dst',self._test_dst)
+ #Proxy.instance.register_call('test/','get_client_list',self._test_get_client_list)
+ #Proxy.instance.register_call('test/','test_dst',self._test_dst)
#Proxy.instance.register_filter('test/',self._test_filter)
try:
- Notice(self._idendesc,self._get_link)
- UserMg(self._idendesc,self._get_link)
- Mail(self._idendesc,self._get_link)
+ mod.load('Notice','notice',self._idendesc,self._get_link)
+ mod.load('UserMg','user',self._idendesc,self._get_link)
+ mod.load('SquareMg','square',self._idendesc,self._get_link)
+ mod.load('Mail','mail',self._idendesc,self._get_link)
+
except Exception as e:
print(e)
- #mod.load('core_user','user',self._idendesc,self._get_link)
- #mod.load('core_mail','mail',self._idendesc,self._get_link)
-
#if self._link == '/backend/2/':
# self._test_call(None)
@@ -185,10 +181,7 @@ class BackendWorker(tornado.tcpserver.TCPServer):
return None
with TOJAuth.change_current_iden(self._idendesc):
- stat,ret = Proxy.instance.call(self.center_conn.link,'lookup_link',65536,link)
-
- print(link)
- print(ret)
+ stat,ret = Proxy.instance.call(self.center_conn.link + 'core/','lookup_link',65536,link)
if stat == False or ret == None:
return None
diff --git a/src/py/center_server.py b/src/py/center_server.py
index bed117d..b4273ef 100755
--- a/src/py/center_server.py
+++ b/src/py/center_server.py
@@ -73,13 +73,13 @@ class CenterServer(tornado.tcpserver.TCPServer):
self._idendesc = TOJAuth.instance.create_iden(self._link,1,TOJAuth.ROLETYPE_TOJ)
Proxy(self._link,TOJAuth.instance,self._idendesc)
- Proxy.instance.register_call('','lookup_link',self._lookup_link)
- Proxy.instance.register_call('','create_iden',self._create_iden)
- Proxy.instance.register_call('','add_client',self._add_client)
- Proxy.instance.register_call('','del_client',self._del_client)
+ Proxy.instance.register_call('core/','lookup_link',self._lookup_link)
+ Proxy.instance.register_call('core/','create_iden',self._create_iden)
+ Proxy.instance.register_call('core/','add_client',self._add_client)
+ Proxy.instance.register_call('core/','del_client',self._del_client)
Proxy.instance.register_call('core/','get_uid_clientlink',self._get_uid_clientlink)
- Proxy.instance.register_call('test/','get_client_list',self._test_get_client_list)
+ #Proxy.instance.register_call('test/','get_client_list',self._test_get_client_list)
def handle_stream(self,stream,addr):
def _recv_worker_info(data):
diff --git a/src/py/mail.py b/src/py/mail.py
index aa01dd6..7bfa53c 100644
--- a/src/py/mail.py
+++ b/src/py/mail.py
@@ -1,7 +1,6 @@
from tojauth import TOJAuth
from asyncdb import AsyncDB
-from user import UserMg
-from notice import Notice
+import mod
from imc.proxy import Proxy
import imc.proxy
import config
@@ -21,7 +20,6 @@ class Mail:
LIST_ITEM_PER_PAGE = 20
def __init__(self, mod_idendesc, get_link_fn):
- Mail.instance = self
Mail.db = AsyncDB(config.CORE_DBNAME, config.CORE_DBUSER,
config.CORE_DBPASSWORD)
Mail._idendesc = mod_idendesc
@@ -56,11 +54,11 @@ class Mail:
elif len(content) > self.CONTENT_LEN_MAX:
return 'Econtent_too_long'
- to_uid = UserMg.instance.get_uid_by_username(to_username)
+ to_uid = mod.UserMg.get_uid_by_username(to_username)
if to_uid == None:
return 'Eto_username'
- uid = UserMg.get_current_uid()
+ uid = mod.UserMg.get_current_uid()
if uid == None:
return 'Euid'
@@ -71,8 +69,8 @@ class Mail:
self._add_mail(
uid, to_uid, self.MAIL_TYPE_SENT_BACKUP, False, title, content
)
- username = UserMg.instance.get_user_info_by_uid(uid)['username']
- Notice.instance.send_notice(
+ username = mod.UserMg.get_user_info_by_uid(uid)['username']
+ mod.Notice.send_notice(
to_uid, 'Mail From ' + username, title, None, '/mail/inbox/'
)
self.notify_client(uid)
@@ -100,7 +98,7 @@ class Mail:
):
return 'Eparameter'
- uid = UserMg.get_current_uid()
+ uid = mod.UserMg.get_current_uid()
if uid == None:
return 'Eno_uid'
@@ -137,9 +135,9 @@ class Mail:
ret['send_time'] = data[7]
ret['to_username'] = (
- UserMg.instance.get_user_info_by_uid(data[1])['username'])
+ mod.UserMg.get_user_info_by_uid(data[1])['username'])
ret['from_username'] = (
- UserMg.instance.get_user_info_by_uid(data[2])['username'])
+ mod.UserMg.get_user_info_by_uid(data[2])['username'])
return ret
@@ -159,7 +157,7 @@ class Mail:
):
return 'Eparameter'
- uid = UserMg.get_current_uid()
+ uid = mod.UserMg.get_current_uid()
if uid == None:
return 'Eno_uid'
@@ -189,7 +187,7 @@ class Mail:
item['send_time'] = data[4]
item['from_username'] = (
- UserMg.instance.get_user_info_by_uid(data[1])['username'])
+ mod.UserMg.get_user_info_by_uid(data[1])['username'])
ret.append(item)
@@ -202,7 +200,7 @@ class Mail:
):
return 'Eparameter'
- uid = UserMg.get_current_uid()
+ uid = mod.UserMg.get_current_uid()
if uid == None:
return 'Eno_uid'
@@ -227,7 +225,7 @@ class Mail:
@imc.async.caller
def get_mail_count(self, mail_type = None):
- uid = UserMg.get_current_uid()
+ uid = mod.UserMg.get_current_uid()
if uid == None:
return 'Eno_uid'
@@ -268,8 +266,5 @@ class Mail:
link + 'core/mail/', 'update_mail', 10000, None
)
-def load(mod_idendesc, get_link_fn):
- Mail(mod_idendesc, get_link_fn)
-
-def unload():
- pass
+ def unload():
+ pass
diff --git a/src/py/mod.py b/src/py/mod.py
index c41b839..e67a9e6 100644
--- a/src/py/mod.py
+++ b/src/py/mod.py
@@ -1,33 +1,35 @@
import sys
+import re;
+import importlib
from importlib import import_module
+from importlib.abc import MetaPathFinder
+import importlib.machinery
-mod_info = {}
+mod = sys.modules[__name__]
+mod_list = {}
-def load(mod_name, mod_path, *args):
- if(
- mod_name in mod_info or
- mod_path in sys.modules
- ):
- raise NameError
+class SqmodFinder(MetaPathFinder):
+ def find_module(fullname,path):
+ if not fullname.startswith('sqmod/'):
+ return None
- instance = import_module(mod_path, "")
- instance.load(*args)
- mod_info[mod_name] = [instance, args, mod_path]
- return instance
+ return importlib.machinery.SourceFileLoader(fullname,fullname + '.py')
-def reload(mod_name):
- instance, args, mod_path = mod_info[mod_name]
- instance = import_module(mod_path, "")
- instance.load(*args)
- mod_info[mod_name][0] = instance
- return instance
+def load(name,path,*args):
+ instance = import_module(path,'')
+ mod_list[name] = instance
+ setattr(mod,name,getattr(instance,name)(*args))
+def unload(name):
+ getattr(mod,name).unload()
+ delattr(mod,name)
+ del mod_list[name]
-def unload(mod_name):
- instance, args, mod_path = mod_info[mod_name]
- instance.unload()
- del sys.modules[mod_path]
- del mod_info[mod_name]
+def load_sqmod(sqmodname):
+ print(sqmodname)
-def list_mod():
- print(list(mod_info.keys()))
+ instance = import_module(''.join(['sqmod/',sqmodname,'/py/',sqmodname]))
+
+ return getattr(instance,sqmodname)
+
+sys.meta_path.append(SqmodFinder)
diff --git a/src/py/notice.py b/src/py/notice.py
index 3a0a988..de2e2d8 100644
--- a/src/py/notice.py
+++ b/src/py/notice.py
@@ -1,6 +1,6 @@
from tojauth import TOJAuth
from asyncdb import AsyncDB
-
+import mod
from imc.proxy import Proxy
import imc.proxy
import config
@@ -11,7 +11,6 @@ class Notice:
NOTICE_LIST_NUM = 10
def __init__(self, mod_idendesc, get_link_fn):
- Notice.instance = self
Notice.db = AsyncDB(config.CORE_DBNAME, config.CORE_DBUSER,
config.CORE_DBPASSWORD)
Notice._idendesc = mod_idendesc
@@ -94,7 +93,7 @@ class Notice:
):
return 'Eparameter'
- uid = UserMg.get_current_uid()
+ uid = mod.UserMg.get_current_uid()
if uid == None:
return 'Euid'
@@ -151,7 +150,7 @@ class Notice:
@imc.async.caller
def get_unseen_count(self):
- uid = UserMg.get_current_uid()
+ uid = mod.UserMg.get_current_uid()
if uid == None:
return 'Euid'
@@ -201,7 +200,8 @@ class Notice:
for link in self.get_link('client', uid = uid):
Proxy.instance.call_async(
- link + 'core/notice/', 'update_notice', 10000, None, unseen_count
+ link + 'core/notice/', 'update_notice', 10000, None,
+ unseen_count
)
def get_notice(self, noticeid):
@@ -227,10 +227,12 @@ class Notice:
if ret == None:
return None
- uid = UserMg.get_current_uid()
+ uid = mod.UserMg.get_current_uid()
if uid != ret['uid']:
TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_EXECUTE)
return ret
-from user import UserMg
+ def unload():
+ pass
+
diff --git a/src/py/square.py b/src/py/square.py
index 68f1be4..ebdf9fb 100644
--- a/src/py/square.py
+++ b/src/py/square.py
@@ -1,7 +1,10 @@
+import datetime
+
from tojauth import TOJAuth
from asyncdb import AsyncDB
-from user import UserMg
-import imc.proxy
+import mod
+import imc.async
+from imc.proxy import Proxy
import config
class SquareMg:
@@ -9,23 +12,64 @@ class SquareMg:
TITLE_LEN_MIN = 1
TITLE_LEN_MAX = 100
+ INTRO_LEN_MIN = 0
+ INTRO_LEN_MAX = 1000
+ LOGO_LEN_MIN = 0
+ LOGO_LEN_MAX = 200
SQUARE_CATE_NUM_MAX = 50
+ JOIN_REJECT = 1
+ JOIN_ACCEPT = 2
+ JOIN_PENDING = 3
+
+ STATUS_WAITING = 1
+ STATUS_RUNNING = 2
+ STATUS_ENDED = 3
+
def __init__(self, mod_idendesc, get_link_fn):
- SquareMg.instance = self
SquareMg.db = AsyncDB(config.CORE_DBNAME, config.CORE_DBUSER,
config.CORE_DBPASSWORD)
SquareMg._idendesc = mod_idendesc
self.get_link = get_link_fn
+ self._sqmod_list = {}
+
+ Proxy.instance.register_call(
+ 'core/square/', 'list_category', self.list_category)
+ Proxy.instance.register_call(
+ 'core/square/', 'list_square', self.list_square)
+ Proxy.instance.register_call(
+ 'core/square/', 'join_square', self.join_square)
+ Proxy.instance.register_call(
+ 'core/square/', 'quit_square', self.quit_square)
+
+ @TOJAuth.check_access(_accessid, TOJAuth.ACCESS_EXECUTE)
+ def load_square(self, sqid):
+ if sqid in self._sqmod_list:
+ return self._sqmod_list[sqid]
+
+ sqinfo = self.get_square_info_by_sqid(sqid)
+ sqmodname = self.get_sqmodname_by_sqmodid(sqinfo['sqmodid'])
+ sqmod = mod.load_sqmod(sqmodname)
+ self._sqmod_list[sqid] = sqmod(self._idendesc, self.get_link, sqid)
+
+ return self._sqmod_list[sqid]
+
+ @TOJAuth.check_access(_accessid, TOJAuth.ACCESS_EXECUTE)
+ def unload_square(self, sqid):
+ if sqid in self._sqmod_list:
+ self._sqmod_list[sqid].unload(True)
+ del self._sqmod_list[sqid]
@imc.async.caller
- def create_square(self, title, hidden, sqmodid, category = []):
+ def create_square(self, title, hidden, sqmodid, intro, logo, category = []):
if(
type(title) != str or
type(hidden) != bool or
type(sqmodid) != int or
- type(category) != list
+ type(category) != list or
+ type(intro) != str or
+ type(logo) != str
):
return 'Eparameter'
@@ -39,6 +83,14 @@ class SquareMg:
return 'Etitle_too_short'
elif len(title) > self.TITLE_LEN_MAX:
return 'Etitle_too_long'
+ elif len(intro) < self.INTRO_LEN_MIN:
+ return 'Eintro_too_short'
+ elif len(intro) > self.INTRO_LEN_MAX:
+ return 'Eintro_too_long'
+ elif len(logo) < self.INTRO_LEN_MIN:
+ return 'Elogo_too_short'
+ elif len(logo) > self.INTRO_LEN_MAX:
+ return 'Elogo_too_long'
if len(category) > self.SQUARE_CATE_NUM_MAX:
return 'Etoo_many_category'
@@ -49,38 +101,47 @@ class SquareMg:
if not self.does_cateid_exist(cateid):
return 'Eno_such_cateid'
- sqid = self._create_square(title, hidden, sqmodid)
+ if not self.does_sqmodid_exist(sqmodid):
+ return 'Eno_such_sqmodid'
+
+ sqid = self._create_square(title, hidden, sqmodid, intro, logo)
self._set_square_category(sqid, category)
return {'sqid': sqid}
@TOJAuth.check_access(_accessid, TOJAuth.ACCESS_CREATE)
- def _create_square(self, title, hidden, sqmodid):
+ def _create_square(self, title, hidden, sqmodid, intro, logo):
cur = self.db.cursor()
sqlstr = ('INSERT INTO "SQUARE" ("title", "hidden", "sqmodid", '
- '"accessid") VALUES (%s, %s, %s, %s) RETURNING "sqid";')
- sqlarr = (title, hidden, sqmodid, 0)
+ '"intro", "logo", "accessid") VALUES (%s, %s, %s, %s, '
+ '%s, %s) RETURNING "sqid";')
+ sqlarr = (title, hidden, sqmodid, intro, logo, 0)
cur.execute(sqlstr, sqlarr)
sqid = None
for data in cur:
sqid = data[0]
- if sqid != None:
- user_idenid = TOJAuth.get_current_iden()['idenid']
- with TOJAuth.change_current_iden(self._idendesc):
- accessid = TOJAuth.instance.create_access(user_idenid)
+ if sqid == None:
+ return None
- sqlstr = ('UPDATE "SQUARE" SET "accessid" = %s WHERE "sqid" = %s;')
- sqlarr = (accessid, sqid)
- cur.execute(sqlstr, sqlarr)
+ user_idenid = TOJAuth.get_current_iden()['idenid']
+ with TOJAuth.change_current_iden(self._idendesc):
+ accessid = TOJAuth.instance.create_access(user_idenid)
- with TOJAuth.change_current_iden(self._idendesc):
- TOJAuth.instance.set_access_list(
- accessid, TOJAuth.ROLEID_SQUARE_ADMIN_GROUP,
- TOJAuth.ACCESS_ALL
- )
+ sqlstr = ('UPDATE "SQUARE" SET "accessid" = %s WHERE "sqid" = %s;')
+ sqlarr = (accessid, sqid)
+ cur.execute(sqlstr, sqlarr)
+
+ with TOJAuth.change_current_iden(self._idendesc):
+ TOJAuth.instance.set_access_list(
+ accessid, TOJAuth.ROLEID_SQUARE_ADMIN_GROUP,
+ TOJAuth.ACCESS_ALL
+ )
+
+ sqmodname = self.get_sqmodname_by_sqmodid(sqmodid)
+ sqmod = mod.load_sqmod(sqmodname)
- # sqmod.create_square_data(sqid)
+ sqmod.create_square_data(sqid)
return sqid;
@@ -100,7 +161,14 @@ class SquareMg:
accessid = self.get_accessid_by_sqid(sqid)
TOJAuth.check_access_func(accessid, TOJAuth.ACCESS_DELETE)
- # sqmod.delete_square_data(sqid)
+ sqinfo = self.get_square_info_by_sqid(sqid)
+ sqmodname = self.get_sqmodname_by_sqmodid(sqinfo['sqmodid'])
+ sqmod = mod.load_sqmod(sqmodname)
+
+ with TOJAuth.change_current_iden(self._idendesc):
+ self.unload_square(sqid)
+
+ sqmod.delete_square_data(sqid)
with TOJAuth.change_current_iden(self._idendesc):
TOJAuth.instance.del_access(accessid)
@@ -110,6 +178,10 @@ class SquareMg:
sqlarr = (sqid, )
cur.execute()
+ sqlstr = ('DELETE FROM "SQUARE_USER" WHERE "sqid" = %s;')
+ sqlarr = (sqid, )
+ cur.execute()
+
@imc.async.caller
def list_square(self, cateid = None):
if(
@@ -118,40 +190,36 @@ class SquareMg:
return 'Eparameter'
ret = None
- if cateid == None:
- ret = self._list_square_all()
- else:
- if not self.does_cateid_exist(cateid):
- return 'Eno_such_cateid'
- ret = self._list_square_category(cateid)
+ if cateid != None and not self.does_cateid_exist(cateid):
+ return 'Eno_such_cateid'
- return ret
+ uid = mod.UserMg.get_current_uid()
- def _list_square_category(self, cateid):
- cur = self.db.cursor()
- sqlstr = ('SELECT "sqid", "title", "start_time", "end_time", "hidden",'
- ' "sqmodid" FROM "SQUARE" WHERE %s = ANY ("cateid");')
- sqlarr = (cateid, )
- cur.execute(sqlstr, sqlarr)
-
- ret = []
- for data in cur:
- obj = {}
- obj['sqid'] = data[0]
- obj['title'] = data[1]
- obj['start_time'] = data[2]
- obj['end_time'] = data[3]
- obj['hidden'] = data[4]
- obj['sqmodid'] = data[5]
- ret.append(obj)
+ ret = self._list_square_category(cateid, uid)
return ret
- def _list_square_all(self):
+ def _list_square_category(self, cateid, uid):
cur = self.db.cursor()
- sqlstr = ('SELECT "sqid", "title", "start_time", "end_time", "hidden",'
- ' "sqmodid" FROM "SQUARE";')
- cur.execute(sqlstr)
+ sqlsel = ('SELECT "SQUARE"."sqid", "title", "start_time", "end_time", '
+ '"hidden", "sqmodid", "intro", "logo"')
+ sqlfrom = (' FROM "SQUARE"')
+ sqlwhere = (' WHERE true')
+ sqlarr = []
+
+ if uid != None:
+ sqlsel = sqlsel + (', "active"')
+ sqlfrom = sqlfrom + (' LEFT JOIN "SQUARE_USER" ON "SQUARE"."sqid"'
+ ' = "SQUARE_USER"."sqid" AND "SQUARE_USER".'
+ '"uid" = %s')
+ sqlarr.append(uid)
+
+ if cateid != None:
+ sqlwhere = sqlwhere + (' AND %s = ANY ("cateid")')
+ sqlarr.append(cateid)
+
+ sqlstr = sqlsel + sqlfrom + sqlwhere + ';'
+ cur.execute(sqlstr, sqlarr)
ret = []
for data in cur:
@@ -162,6 +230,24 @@ class SquareMg:
obj['end_time'] = data[3]
obj['hidden'] = data[4]
obj['sqmodid'] = data[5]
+ obj['intro'] = data[6]
+ obj['logo'] = data[7]
+ if uid != None:
+ obj['active'] = data[8]
+
+ nowtime = datetime.datetime.now(datetime.timezone.utc)
+
+ if obj['start_time'] == None:
+ obj['status'] = self.STATUS_RUNNING
+ elif nowtime < obj['start_time']:
+ obj['status'] = self.STATUS_WAITING
+ elif obj['end_time'] == None:
+ obj['status'] = self.STATUS_RUNNING
+ elif nowtime > obj['end_time']:
+ obj['status'] = self.STATUS_ENDED
+ else:
+ obj['status'] = self.STATUS_RUNNING
+
ret.append(obj)
return ret
@@ -246,16 +332,117 @@ class SquareMg:
sq_accessid = self.get_accessid_by_sqid(sqid)
TOJAuth.check_access_func(sq_accessid, TOJAuth.ACCESS_WRITE)
+ if category == {}:
+ category = {0}
+
cur = self.db.cursor()
sqlstr = ('UPDATE "SQUARE" SET "cateid" = %s WHERE "sqid" = %s;')
sqlarr = (category, sqid)
cur.execute(sqlstr, sqlarr)
+ @imc.async.caller
+ def list_category(self):
+ cur = self.db.cursor()
+ sqlstr = ('SELECT "cateid", "catename" FROM "CATEGORY" ORDER BY '
+ '"cateid" ASC;')
+ cur.execute(sqlstr)
+
+ cate_list = []
+ for data in cur:
+ cate = {}
+ cate['cateid'] = data[0]
+ cate['catename'] = data[1]
+
+ if cate['cateid'] == 0:
+ zerocate = cate
+ else:
+ cate_list.append(cate)
+
+ cate_list.append(zerocate)
+
+ return cate_list
+
+ @imc.async.caller
+ def join_square(self, sqid):
+ if(
+ type(sqid) != int
+ ):
+ return 'Eparameter'
+
+ uid = mod.UserMg.get_current_uid()
+ if uid == None:
+ return 'Euid'
+
+ sq = self.get_square_info_by_sqid(sqid)
+ if sq == None:
+ return 'Eno_such_sqid'
+
+ with TOJAuth.change_current_iden(self._idendesc):
+ sqobj = self.load_square(sqid)
+
+ result = sqobj.join_square(uid)
+
+ if result == self.JOIN_REJECT:
+ return 'Ereject'
+ elif result == self.JOIN_PENDING:
+ with TOJAuth.change_current_iden(self._idendesc):
+ self._set_user_square_relation(uid, sqid, False)
+ return {'active': False}
+ elif result == self.JOIN_ACCEPT:
+ with TOJAuth.change_current_iden(self._idendesc):
+ self._set_user_square_relation(uid, sqid, True)
+ return {'active': True}
+ else:
+ return 'Ejoin_sq_error'
+
+ @imc.async.caller
+ def quit_square(self, sqid):
+ if(
+ type(sqid) != int
+ ):
+ return 'Eparameter'
+
+ uid = mod.UserMg.get_current_uid()
+ if uid == None:
+ return 'Euid'
+
+ sq = self.get_square_info_by_sqid(sqid)
+ if sq == None:
+ return 'Eno_such_sqid'
+
+ with TOJAuth.change_current_iden(self._idendesc):
+ sqobj = self.load_square(sqid)
+
+ sqobj.quit_square(uid)
+
+ with TOJAuth.change_current_iden(self._idendesc):
+ self._del_user_square_relation(uid, sqid)
+
+ @TOJAuth.check_access(_accessid, TOJAuth.ACCESS_WRITE)
+ def _set_user_square_relation(self, uid, sqid, active):
+ cur = self.db.cursor()
+ name = 'SQUARE_USER'
+ cond = {
+ 'sqid': sqid,
+ 'uid': uid
+ }
+ value = {
+ 'active': active
+ }
+ cur.upsert(name, cond, value)
+
+ @TOJAuth.check_access(_accessid, TOJAuth.ACCESS_WRITE)
+ def _del_user_square_relation(self, uid, sqid):
+ cur = self.db.cursor()
+ sqlstr = ('DELETE FROM "SQUARE_USER" WHERE "uid" = %s AND "sqid" = %s;')
+ sqlarr = (uid, sqid)
+ cur.execute(sqlstr, sqlarr)
+
def get_square_info_by_sqid(self, sqid):
cur = self.db.cursor()
sqlstr = ('SELECT "sqid", "title", "start_time", "end_time", '
- '"hidden", "sqmodid", "cateid" FROM "SQUARE" WHERE '
- '"sqid" = %s;')
+ '"hidden", "sqmodid", "intro", "logo", "cateid" FROM '
+ '"SQUARE" WHERE "sqid" = %s;')
sqlarr = (sqid, )
cur.execute(sqlstr, sqlarr)
@@ -268,7 +455,12 @@ class SquareMg:
ret['end_time'] = data[3]
ret['hidden'] = data[4]
ret['sqmodid'] = data[5]
- ret['cateid'] = data[6]
+ ret['intro'] = data[6]
+ ret['logo'] = data[7]
+ ret['cateid'] = data[8]
+
+ if 0 in ret['cateid']:
+ ret['cateid'] = []
return ret
@@ -304,8 +496,29 @@ class SquareMg:
catename = self.get_catename_by_cateid(cateid)
return catename != None
+ def get_sqmodname_by_sqmodid(self, sqmodid):
+ cur = self.db.cursor()
+ sqlstr = ('SELECT "sqmodname" FROM "SQMOD" WHERE "sqmodid" = %s;')
+ sqlarr = (sqmodid, )
+ cur.execute(sqlstr, sqlarr)
+
+ sqmodname = None
+ for data in cur:
+ sqmodname = data[0]
+
+ return sqmodname
+
+ def does_sqmodid_exist(self, sqmodid):
+ sqmodname = self.get_sqmodname_by_sqmodid(sqmodid)
+ return sqmodname != None
+
+ def unload():
+ pass
+
class Square:
- pass
+ def unload(self):
+ pass
class Group:
pass
+
diff --git a/src/py/user.py b/src/py/user.py
index 33c2c6c..43605e0 100755
--- a/src/py/user.py
+++ b/src/py/user.py
@@ -7,6 +7,8 @@ import imc.async
from imc.proxy import Proxy
import config
+import mod
+
class UserMg:
_accessid = 2
@@ -26,7 +28,6 @@ class UserMg:
COVER_LEN_MAX = 200
def __init__(self, mod_idendesc, get_link_fn):
- UserMg.instance = self
UserMg.db = AsyncDB(config.CORE_DBNAME, config.CORE_DBUSER,
config.CORE_DBPASSWORD)
UserMg._idendesc = mod_idendesc
@@ -120,7 +121,7 @@ class UserMg:
uid = data[0]
with TOJAuth.change_current_iden(self._idendesc):
- Notice.instance.create_unseen_count(uid)
+ mod.Notice.create_unseen_count(uid)
return uid
@@ -153,7 +154,7 @@ class UserMg:
client_link = TOJAuth.get_current_iden()['link']
with TOJAuth.change_current_iden(self._idendesc):
- stat,data = Proxy.instance.call(self.get_link('center'),
+ stat,data = Proxy.instance.call(self.get_link('center') + 'core/',
'create_iden',
10000,
client_link,
@@ -200,7 +201,7 @@ class UserMg:
client_link = TOJAuth.get_current_iden()['link']
with TOJAuth.change_current_iden(self._idendesc):
- stat,data = Proxy.instance.call(self.get_link('center'),
+ stat,data = Proxy.instance.call(self.get_link('center') + 'core/',
'create_iden',
10000,
client_link,
@@ -398,11 +399,6 @@ class UserMg:
return None
return uid
-def load(mod_idendesc, get_link_fn):
- UserMg(mod_idendesc, get_link_fn)
-
-def unload():
- pass
-
-from notice import Notice
+ def unload():
+ pass