forked from JoneXiong/PyRedisAdmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_view.py
146 lines (136 loc) · 7.52 KB
/
data_view.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# coding=utf-8
'''
数据相关视图
'''
from config import media_prefix
def title_html(fullkey,sid, db):
out = '''
<h2>%(fullkey)s
<a class="btn" href="/rename?s=%(sid)s&db=%(db)s&key=%(fullkey)s" onclick="renameKey(this.href,'%(fullkey)s');return false;" title="Rename"><i class="icon-edit"></i></a>
<a class="btn" href="/delete?s=%(sid)s&db=%(db)s&key=%(fullkey)s" title="Delete"><i class="icon-remove"></i></a>
<a class="btn" href="/export?s=%(sid)s&db=%(db)s&key=%(fullkey)s" title="Export"><i class="icon-download-alt"></i></a>
</h2>'''%({'sid':sid, 'db':db, 'fullkey':fullkey,'media_prefix':media_prefix})
return out
def general_html(fullkey, sid, db, client):
cl = client
m_type = cl.type(fullkey)
m_ttl = cl.ttl(fullkey)
m_ttl = m_ttl and m_ttl or ''
redis_version = cl.info()['redis_version']
if redis_version>='2.2.3':
m_encoding = cl.object('encoding', fullkey)
else:
m_encoding = ''
m_len = 0
m_detail = ''
m_other = '''<p>
<a href="/add?s=%(sid)s&db=%(db)s&type=%(type)s&key=%(fullkey)s" onclick="add(this.href,'%(type)s');return false;" class="add">Add another value</a>
</p>'''%({'sid':sid, 'db':db, 'type':m_type, 'fullkey':fullkey})
if m_type=='string':
val = cl.get(fullkey)
m_len = len(val)
m_detail = string_html(fullkey, sid, db, client)
m_other = ''
if m_type=='hash':
m_len = cl.hlen(fullkey)
m_detail = hash_html(fullkey, sid, db, client)
elif m_type=='list':
m_len = cl.llen(fullkey)
m_detail = list_html(fullkey, sid, db, client)
elif m_type=='set':
m_len = len(cl.smembers(fullkey))
m_detail = set_html(fullkey, sid, db, client)
elif m_type=='zset':
m_len = len(cl.zrange(fullkey,0,-1))
m_detail = zset_html(fullkey, sid, db, client)
out = '''
<table class="table table-bordered table-striped" style="max-width: 400px">
<tr><td><div>Type:</div></td><td><div>%(type)s</div></td></tr>
<tr><td><div><abbr title="Time To Live">TTL</abbr>:</div></td><td><div>%(ttl)s <a class="btn" href="/ttl?s=%(sid)s&db=%(db)s&key=%(fullkey)s" onclick="changeTTL(this.href,'%(ttl)s');return false;" title="Edit TTL"><i class="icon-edit"></i></a></div></td></tr>
<tr><td><div>Encoding:</div></td><td><div>%(encoding)s</div></td></tr>
<tr><td><div>Size:</div></td><td><div>%(size)s</div></td></tr>
</table>'''%({'type': m_type, 'ttl':m_ttl, 'sid':sid, 'db':db, 'fullkey':fullkey, 'encoding':m_encoding, 'size': m_len, 'media_prefix':media_prefix})
return out + m_detail + m_other
def string_html(fullkey,sid, db, client):
m_value = client.get(fullkey)
out = '''
<table class="table table-bordered table-striped">
<tr><td><div>%(value)s</div></td><td><div>
<a class="btn" href="/edit?s=%(sid)s&db=%(db)s&type=string&key=%(fullkey)s" onclick="edit(this.href,'%(value)s');return false;" title="Edit"><i class="icon-edit"></i></a>
</div></td></tr>
</table>
'''%({'value':m_value, 'sid':sid, 'db':db, 'fullkey':fullkey, 'media_prefix':media_prefix})
return out
def hash_html(fullkey,sid, db, client):
out = '''
<table class="table table-bordered table-striped">
<tr><th><div>Key</div></th><th><div>Value</div></th><th><div> </div></th><th><div> </div></th></tr>'''
m_values = client.hgetall(fullkey)
alt = False
for key,value in m_values.items():
if len(value)>200:
value = 'data(len:%s)'%len(value)
alt_str = alt and 'class="alt"' or ''
out +='''<tr %(alt_str)s><td><div>%(key)s</div></td><td><div>%(value)s</div></td><td><div>
<a class="btn" href="/edit?s=%(sid)s&db=%(db)s&type=hash&key=%(fullkey)s&value=%(key)s" onclick="edit(this.href,'%(value)s');return false;" title="Edit"><i class="icon-edit"></i></a>
</div></td><td><div>
<a class="btn" href="/delete?s=%(sid)s&db=%(db)s&type=hash&key=%(fullkey)s&value=%(key)s" class="delval" title="Delete"><i class="icon-remove"></i></a>
</div></td></tr>
'''%({'value':value, 'key':key, 'sid':sid, 'db':db, 'fullkey':fullkey, 'alt_str':alt_str, 'media_prefix':media_prefix})
alt = not alt
out +='</table>'
return out
def list_html(fullkey,sid, db, client):
out = '''
<table class="table table-bordered table-striped">
<tr><th><div>Index</div></th><th><div>Value</div></th><th><div> </div></th><th><div> </div></th></tr>'''
m_values = client.lrange(fullkey,0,-1)
alt = False
index = 0
for value in m_values:
alt_str = alt and 'class="alt"' or ''
out +='''<tr %(alt_str)s><td><div>%(index)s</div></td><td><div>%(value)s</div></td><td><div>
<a class="btn" href="/edit?s=%(sid)s&db=%(db)s&type=list&key=%(fullkey)s&value=%(index)s" onclick="edit(this.href,'%(value)s');return false;" title="Edit"><i class="icon-edit"></i></a>
</div></td><td><div>
<a class="btn" href="/delete?s=%(sid)s&db=%(db)s&type=list&key=%(fullkey)s&value=%(index)s" class="delval" title="Delete"><i class="icon-remove"></i></a>
</div></td></tr>
'''%({'value':value.replace('"','\\\''), 'index':index, 'sid':sid, 'db':db, 'fullkey':fullkey, 'alt_str':alt_str, 'media_prefix':media_prefix})
alt = not alt
index +=1
out +='</table>'
return out
def set_html(fullkey,sid, db, client):
out = '''
<table class="table table-bordered table-striped">
<tr><th><div>Value</div></th><th><div> </div></th><th><div> </div></th></tr>'''
m_values = client.smembers(fullkey)
alt = False
for value in m_values:
alt_str = alt and 'class="alt"' or ''
out +='''<tr %(alt_str)s><td><div>%(value)s</div></td><td><div>
<a class="btn" href="/edit?s=%(sid)s&db=%(db)s&type=set&key=%(fullkey)s&value=%(value)s" onclick="edit(this.href,'%(value)s');return false;" title="Edit"><i class="icon-edit"></i></a>
</div></td><td><div>
<a class="btn" href="/delete?s=%(sid)s&db=%(db)s&type=set&key=%(fullkey)s&value=%(value)s" class="delval" title="Delete"><i class="icon-remove"></i></a>
</div></td></tr>
'''%({'value':value, 'sid':sid, 'db':db, 'fullkey':fullkey, 'alt_str':alt_str, 'media_prefix':media_prefix})
alt = not alt
out +='</table>'
return out
def zset_html(fullkey,sid, db, client):
out = '''
<table class="table table-bordered table-striped">
<tr><th><div>Score</div></th><th><div>Value</div></th><th><div> </div></th><th><div> </div></th></tr>'''
m_values = client.zrange(fullkey,0,-1)
alt = False
for value in m_values:
score = client.zscore(fullkey,value)
alt_str = alt and 'class="alt"' or ''
out +='''<tr %(alt_str)s><td><div>%(score)s</div></td><td><div>%(value)s</div></td><td><div>
<a class="btn" href="/edit?s=%(sid)s&db=%(db)s&type=zset&key=%(fullkey)s&value=%(value)s;score=%(score)s" onclick="edit(this.href,'%(value)s');return false;" title="Edit"><i class="icon-edit"></i></a>
</div></td><td><div>
<a class="btn" href="/delete?s=%(sid)s&db=%(db)s&type=zset&key=%(fullkey)s&value=%(value)s" class="delval" title="Delete"><i class="icon-remove"></i></a>
</div></td></tr>
'''%({'value':value, 'score':score, 'sid':sid, 'db':db, 'fullkey':fullkey, 'alt_str':alt_str, 'media_prefix':media_prefix})
alt = not alt
out +='</table>'
return out