Skip to content

Commit

Permalink
Merge pull request #3 from intoj/吱声
Browse files Browse the repository at this point in the history
添加功能: 吱声
  • Loading branch information
interestingLSY authored May 20, 2019
2 parents 184572b + 0b50211 commit db81956
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 84 deletions.
19 changes: 19 additions & 0 deletions document/intoj 的 吱声.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# intoj 的 吱声.md

---

数据库:

- id
- username
- message
- send_time

```sql
CREATE TABLE zisheng(
id INT NOT NULL PRIMARY KEY auto_increment,
username VARCHAR(20),
message TEXT,
send_time VARCHAR(50)
);
```
4 changes: 3 additions & 1 deletion web/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
app.add_template_global(min,'min')
app.add_template_global(max,'max')
app.add_template_global(sum,'sum')
app.add_template_global(len,'len')
app.add_template_global(random.randint,'randint')

app.add_template_global(sites.modules.Argstring,'Argstring')
app.add_template_global(sites.modules.Score_Color,'Score_Color')
Expand Down Expand Up @@ -48,7 +50,7 @@ def Error_404(e):
def Blank():
return render_template('base.html')

@app.route('/')
@app.route('/',methods=['GET','POST'])
def Home():
return sites.home.Run()

Expand Down
6 changes: 5 additions & 1 deletion web/config-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
"port": 6379
},
"site": {
"use_resource": "online"
"use_resource": "online",
"zisheng": {
"enable": true,
"max_length": "400"
}
},
"enable_lang": [
{
Expand Down
30 changes: 21 additions & 9 deletions web/sites/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import modules,config

def Generate_Limitation(limitation,allowed=[]):
if limitation == None: return '',[]
answer = ''
arg = []
for key,value in limitation.items():
Expand Down Expand Up @@ -51,13 +52,9 @@ def Read_Submissions(limitation=None,order="id DESC"):
'contest_id': ('eq','contest_id')
}
db,cur = Connect()
if limitation != None:
lim,arg = Generate_Limitation(limitation,allowed)
cmd = "SELECT * FROM records %s ORDER BY %s" % (lim,modules.Raw(order))
cur.execute(cmd,arg)
else:
cur.execute("SELECT * FROM records ORDER BY %s"%order)

lim,arg = Generate_Limitation(limitation,allowed)
cmd = "SELECT * FROM records %s ORDER BY %s" % (lim,modules.Raw(order))
cur.execute(cmd,arg)
submissions = cur.fetchall()
End_Connect(db,cur)
return submissions
Expand Down Expand Up @@ -85,9 +82,9 @@ def Read_Contest_Ranklist(contest_id):
def Read_Record(id):
db,cur = Connect()
cur.execute("SELECT * FROM records WHERE id=%s;",id)
urecord = cur.fetchone()
record = cur.fetchone()
End_Connect(db,cur)
return urecord
return record

def Read_Userlist():
db,cur = Connect()
Expand Down Expand Up @@ -130,3 +127,18 @@ def User_Privilege(username,privilege_name):
if user == None: return 0
if user['is_admin']: return 1
return user[privilege_name]

def Read_Zisheng(limitation=None,top=10):
allowed = {
'id': ('eq','id'),
'username': ('eq','username')
}
db,cur = Connect()

lim,arg = Generate_Limitation(limitation,allowed)
cmd = "SELECT * FROM zisheng %s ORDER BY id DESC LIMIT %d" % (lim,top)
cur.execute(cmd,arg)
zisheng = cur.fetchall()

End_Connect(db,cur)
return zisheng
48 changes: 44 additions & 4 deletions web/sites/home.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,50 @@
#coding:utf-8
from flask import *
import db,modules
import db,modules,config
import datetime

def Run():
contests = db.Read_Contestlist()
zisheng_delay_time = 3

def GET():
contests = db.Read_Contestlist()
users = list(db.Read_Userlist())
users.sort( key = lambda x: (x['total_ac'],-x['total_submit']) , reverse=True )
return render_template('home.html',contests=contests,users=users[:10])
zishengs = db.Read_Zisheng()
return render_template('home.html',contests=contests,users=users[:10],zishengs=zishengs)

def Send_Zisheng():
if not modules.Is_Loggedin():
flash('请先登录','error')
return False
if not config.config['site']['zisheng']['enable']:
flash('管理员已关闭「吱声」','error')
return False

message = request.form['message']
max_length = config.config['site']['zisheng']['max_length']
if len(message) > max_length:
flash('超过最长长度: %d'%max_length,'error')
return False

username = request.cookies['username']
last_zisheng_time = modules.Get_Session('last_zisheng_time',username)
now_time = datetime.datetime.now()
if last_zisheng_time != None:
seconds = (now_time-last_zisheng_time).seconds
if seconds < zisheng_delay_time:
flash('请在 %ss 后提交'%(zisheng_delay_time-seconds),'error')
return False
modules.Set_Session('last_zisheng_time',username,now_time)

nowtime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
db.Execute("INSERT INTO zisheng VALUES(NULL,%s,%s,%s);",(username,request.form['message'],nowtime))

flash('发送成功','ok')
return True

def Run():
if request.method == 'GET':
return GET()
else:
is_success = Send_Zisheng()
return redirect('/')
3 changes: 2 additions & 1 deletion web/sites/userhome.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ def Run(username):
return modules.Page_Back()

status_cnt = User_Statistic(username)
return render_template('userhome.html',user=user,status_cnt=status_cnt)
zishengs = db.Read_Zisheng({'username':username})
return render_template('userhome.html',user=user,status_cnt=status_cnt,zishengs=zishengs,top=1000)
11 changes: 0 additions & 11 deletions web/static/css/ui.css
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,6 @@ div.block-mini-inner{
border-radius: 6px;
}

/**************** footer ****************/
#footer{
background: #ffffff88;
width: 100%;
height: 22px;
margin-top: 30px;
padding-top: 5px;
font-size: 14px;
text-align: center;
}

/**************** tags ****************/
.tag{
background-color: #cfcfcf;
Expand Down
35 changes: 35 additions & 0 deletions web/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,41 @@
</div>
</div>
</div>
<div class="ui" style="padding: 13px 12px; border-radius: 3px; margin-top: 12px;">
<div class="block-mini-inner">
<div class="contitle"> 吱声 </div>
{% if config['site']['zisheng']['enable'] %}
<div style="">
<form method="POST">
<textarea class="markdowninput" name="message"></textarea>
<button type="submit" class="button button-action button-small button-rounded" style="margin: 10px 0px 0px 10px"> 发射! </button>
</form>
</div>
<hr style="color: #dddddd" />
{% for zisheng in zishengs %}
<div style="margin: 20px 10px; border: 1px dashed #66ccff; border-radius: 5px; box-shadow: 1px 1px 2px #ddd;">
<div style="height: 35px; background-color: #e3e3e3; padding: 1px 15px; display: flex; align-items: center;">
<span>
<span style="color: #666;">
@ <span style="font-family: consolas"><a href="/user/{{- zisheng['username'] -}}">{{zisheng['username']}}</a></span>
</span>
<span style="color: #aaa; font-family: consolas; font-size: 12px; margin-left: 10px;">
{{zisheng['send_time']}}
</span>
</span>
</div>
<div style="padding: 5px 15px; overflow: hidden; max-height: 600px;" class="markdown">
{{- zisheng['message'] -}}
</div>
</div>
{% endfor %}
{% else %}
<div style="height: 50px; display: flex; justify-content: center; align-items: center; color: #999; font-size: 20px;">
<span> <i class="icon-ban-circle"> </i> 该功能已被管理员关闭 </span> </span>
</div>
{% endif %}
</div>
</div>
</div>
<div class="block-mini" style="width: 35%;">
<div class="ui" style="padding: 15px 12px; border-radius: 3px;">
Expand Down
144 changes: 87 additions & 57 deletions web/templates/userhome.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
{% extends 'base.html' %}
{% block title %} {{user['username']}} {% endblock %}
{% block body %}
{% set called = "我" if Is_Loggedin() and user['username'] == request.cookies['username'] else "她" if user['sex'] == 1 else "他" if user['sex'] == 0 else "Ta" %}
<div class="ui main">
{% set called = "我" if Is_Loggedin() and user['username'] == request.cookies['username'] else "她" if user['sex'] == 1 else "他" if user['sex'] == 0 else "Ta " %}
<div class="main">
<div class="block-mini-box">
<div style="width: 30%;">
<div class="block-mini-inner" style="padding: 0px 0px 5px 0px; width: 100%; border-radius: 0;">
<div class="block-mini-inner ui" style="padding: 0px 0px 5px 0px; width: 100%; border-radius: 0;">
<img src="https://cdn.v2ex.com/gravatar/{{Email_Hash(user['email'])}}.jpg?s=1000" style="width:100%" />
<div class="contitle"> U{{user['id']}} - {{user['username']}} </div>
</div>
<div class="block-mini-inner">
<div class="block-mini ui">
<div class="block-mini-inner" style="display: flex; justify-content: space-around; padding-top: 20px; padding-bottom: 20px;">
<div style="display: flex; align-items: center; flex-direction: column;">
<span style="font-size: 60px;"> {{user['total_ac']}} </span> 通过
</div>
<div style="display: flex; align-items: center; flex-direction: column;">
<span style="font-size: 60px;"> {{user['total_submit']}} </span> 提交
</div>
</div>
<div class="block-mini-inner">
<div class="contitle"> 统计 <a class="button button-primary button-small button-rounded" style="float: right; margin-top: 5px;" href="/status?username={{user['username']}}"> {{called}}的提交 </a> </div>
<div style="width: 100%; height: 100%;">
<canvas id="submit_cnt" width="100" height="100"></canvas>
</div>
</div>
</div>
<div class="block-mini-inner ui">
{% if user['ac_list'] != None %}
<div class="contitle" onclick="Click('ac_list')"> AC 的题目 <span style="font-size: 14px"> (点击查看) </span> </div>
<div class="content dropdown-ac_list" style="font-size: 18px;" hidden>
Expand All @@ -19,66 +35,80 @@
</div>
{% endif %}
</div>
</div>
<div class="block-mini" style="width: 33%">
<div class="block-mini-inner">
<div class="contitle"> 个性签名 </div>
<div class="content markdown">
{{user['signature']}}
</div>
</div>
<div class="block-mini-inner">
<div class="contitle"> 邮箱地址 </div>
<div class="content markdown">
{{user['email']}}
</div>
<div class="contitle"> 性别 </div>
<div class="content markdown">
{% if user['sex'] == 0 %} 男
{% elif user['sex'] == 1 %} 女
{% else %} Unknown
{% endif %}
<div class="block-mini ui">
<div class="block-mini-inner">
<div class="contitle"> 邮箱地址 </div>
<div class="content markdown">
{{-user['email']-}}
</div>
<div class="contitle"> 性别 </div>
<div class="content markdown">
{%- if user['sex'] == 0 -%} 男
{%- elif user['sex'] == 1 -%} 女
{%- else -%} Unknown
{% endif %}
</div>
{% if user['group'] != None %}
<div class="contitle"> 小组 </div>
<div class="content markdown">
{{-user['group']-}}
</div>
{% endif %}
{% if Current_User_Privilege('is_user_manager') %}
<a href="/user/{{user['username']}}/edit" class="uncolored">
<button class="button button-primary button-rounded button-small">
编辑{{called}}的资料
</button>
</a>
{% endif %}
</div>
{% if user['group'] != None %}
<div class="contitle"> 小组 </div>
<div class="content markdown">
{{user['group']}}
{% if User_Privilege(user['username'],'is_user_manager') or User_Privilege(user['username'],'is_problem_manager') or User_Privilege(user['username'],'is_contest_manager') or User_Privilege(user['username'],'is_tag_manager') %}
<div class="block-mini-inner">
<div class="contitle"> 权限 </div>
<div class="content">
{% if User_Privilege(user['username'],'is_admin') %} <b> 全站管理员 </b> <br /> {% endif %}
{% if User_Privilege(user['username'],'is_user_manager') %} 用户管理员 <br /> {% endif %}
{% if User_Privilege(user['username'],'is_problem_manager') %} 题目管理员 <br /> {% endif %}
{% if User_Privilege(user['username'],'is_contest_manager') %} 比赛管理员 <br /> {% endif %}
{% if User_Privilege(user['username'],'is_tag_manager') %} 题目标签管理员 <br /> {% endif %}
</div>
</div>
{% endif %}
{% if Current_User_Privilege('is_user_manager') %}
<a href="/user/{{user['username']}}/edit" class="uncolored">
<button class="button button-primary button-rounded button-small">
编辑{{called}}的资料
</button>
</a>
{% endif %}
</div>
{% if User_Privilege(user['username'],'is_user_manager') or User_Privilege(user['username'],'is_problem_manager') or User_Privilege(user['username'],'is_contest_manager') or User_Privilege(user['username'],'is_tag_manager') %}
<div class="block-mini-inner">
<div class="contitle"> 权限 </div>
<div class="content">
{% if User_Privilege(user['username'],'is_admin') %} <b> 全站管理员 </b> <br /> {% endif %}
{% if User_Privilege(user['username'],'is_user_manager') %} 用户管理员 <br /> {% endif %}
{% if User_Privilege(user['username'],'is_problem_manager') %} 题目管理员 <br /> {% endif %}
{% if User_Privilege(user['username'],'is_contest_manager') %} 比赛管理员 <br /> {% endif %}
{% if User_Privilege(user['username'],'is_tag_manager') %} 题目标签管理员 <br /> {% endif %}
</div>
</div>
{% endif %}
</div>
<div class="block-mini" style="width: 33%">
<div class="block-mini-inner" style="display: flex; justify-content: space-around; padding-top: 20px; padding-bottom: 20px;">
<div style="display: flex; align-items: center; flex-direction: column;">
<span style="font-size: 60px;"> {{user['total_ac']}} </span> 通过
</div>
<div style="display: flex; align-items: center; flex-direction: column;">
<span style="font-size: 60px;"> {{user['total_submit']}} </span> 提交
<div style="width: 69%">
<div class="block-mini-inner ui">
<div class="contitle"> 个性签名 </div>
<div class="content markdown">
{{-user['signature']-}}
</div>
</div>
<div class="block-mini-inner">
<div class="contitle"> 统计 <a class="button button-primary button-small button-rounded" style="float: right; margin-top: 5px;" href="/status?username={{user['username']}}"> {{called}}的提交 </a> </div>
<div style="width: 100%; height: 100%;">
<canvas id="submit_cnt" width="100" height="100"></canvas>
<div class="block-mini-inner ui">
<div class="contitle"> {{called}}的「吱声」</div>
<div class="content">
{% if len(zishengs) > 0 %}
{% for zisheng in zishengs %}
<div style="margin: 20px 10px; border: 1px dashed #66ccff; border-radius: 5px; box-shadow: 1px 1px 2px #ddd;">
<div style="height: 35px; background-color: #e3e3e3; padding: 1px 15px; display: flex; align-items: center;">
<span>
<span style="color: #666;">
@ <span style="font-family: consolas"><a href="/user/{{- zisheng['username'] -}}">{{zisheng['username']}}</a></span>
</span>
<span style="color: #aaa; font-family: consolas; font-size: 12px; margin-left: 10px;">
{{zisheng['send_time']}}
</span>
</span>
</div>
<div style="padding: 5px 15px; overflow: hidden; max-height: 600px;" class="markdown">
{{-zisheng['message']-}}
</div>
</div>
{% endfor %}
{% else %}
<div style="height: 50px; display: flex; justify-content: center; align-items: center; color: #999; font-size: 20px;">
<span> <i class="icon-ban-circle"> </i> 这人太懒, 没吱过声. </span> </span>
</div>
{% endif %}
</div>
</div>
</div>
Expand Down

0 comments on commit db81956

Please sign in to comment.