Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added another category support in creating entity.

  • Loading branch information...
commit 7dd84cf52b22a3e419b8a5fd010bd94554021c12 1 parent a8c1130
leonyuan authored
22 admin/entity.py
... ... @@ -1,7 +1,7 @@
1 1 #encoding=utf-8
2 2 import web
3 3 from admin.util import render, admin_login_required
4   -from basis.dbutil import get_category, get_entities as get_base_entities, get_entity as get_base_entity
  4 +from basis.dbutil import get_category, category_tree, get_entities as get_base_entities, get_entity as get_base_entity
5 5 from models.dbutil import get_models, get_model_by_name, get_entities, new_entity, get_entity, save_entity, del_entity
6 6 from admin.form import base_entity_form, entity_form
7 7 from datetime import datetime
@@ -31,6 +31,7 @@ def GET(self, mname):
31 31 model = get_model_by_name(mname)
32 32 base_form = base_entity_form()
33 33 form = entity_form(mname)
  34 + categories = category_tree()
34 35 req = web.ctx.req
35 36 req.update({
36 37 'base_form': base_form,
@@ -38,6 +39,7 @@ def GET(self, mname):
38 39 'mname': mname,
39 40 'mtitle': model.title,
40 41 'model': model,
  42 + 'categories': categories,
41 43 })
42 44 return render.entity_edit(**req)
43 45
@@ -49,15 +51,22 @@ def POST(self, mname):
49 51 bv = base_form.validates()
50 52 v = form.validates()
51 53 if not bv or not v:
  54 + categories = category_tree()
  55 + data = web.input(cids=[])
  56 + base_form.cids.set_value(data.cids)
52 57 req = web.ctx.req
53 58 req.update({
54 59 'base_form': base_form,
55 60 'form': form,
56 61 'mname': mname,
57 62 'mtitle': model.title,
  63 + 'categories': categories,
58 64 })
59 65 return render.entity_edit(**req)
  66 +
  67 + data = web.input(cids=[])
60 68 base_form_data = base_form.d
  69 + base_form_data.cids = data.cids
61 70 base_form_data.user_id = web.ctx.session._userid
62 71 form_data = form.d
63 72 new_entity(model, base_form_data, form_data)
@@ -67,12 +76,16 @@ class edit:
67 76 @admin_login_required
68 77 def GET(self, id):
69 78 base_entity = get_base_entity(id)
  79 + cids = [cate.id for cate in base_entity.categories]
70 80 model = base_entity.model
71 81 base_form = base_entity_form()
  82 + base_form.cids.set_value(cids)
72 83 form = entity_form(model.name)
73 84 entity = getattr(base_entity, model.name)
74 85 base_form.fill(base_entity)
  86 + base_form.cids.value = cids
75 87 form.fill(entity)
  88 + categories = category_tree()
76 89 data = web.input()
77 90 req = web.ctx.req
78 91 req.update({
@@ -80,6 +93,7 @@ def GET(self, id):
80 93 'form': form,
81 94 'mname': model.name,
82 95 'mtitle': model.title,
  96 + 'categories': categories,
83 97 })
84 98 return render.entity_edit(**req)
85 99
@@ -93,15 +107,21 @@ def POST(self, id):
93 107 bv = base_form.validates()
94 108 v = form.validates()
95 109 if not bv or not v:
  110 + categories = category_tree()
  111 + data = web.input(cids=[])
  112 + base_form.cids.set_value(data.cids)
96 113 req = web.ctx.req
97 114 req.update({
98 115 'base_form': base_form,
99 116 'form': form,
100 117 'mname': model.name,
101 118 'mtitle': model.title,
  119 + 'categories': categories,
102 120 })
103 121 return render.entity_edit(**req)
  122 + data = web.input(cids=[])
104 123 base_form_data = base_form.d
  124 + base_form_data.cids = data.cids
105 125 base_form_data.updated_time = datetime.now()
106 126 form_data = form.d
107 127 save_entity(model, int(id), base_form_data, form_data)
1  admin/form.py
@@ -93,6 +93,7 @@ def render(self):
93 93 )
94 94
95 95 base_entity_form = web.form.Form(
  96 + MyTextbox('cids', vnotnull, required=True, size=40, description=u"栏目"),
96 97 MyTextbox('title', vnotnull, required=True, size=40, description=u"标题"),
97 98 MyTextbox('slug', size=20, description=u"英文缩写"),
98 99 )
12 basis/dbutil.py
@@ -121,14 +121,14 @@ def get_entitys_category_ancestors(entity):
121 121 def get_entities():
122 122 return web.ctx.orm.query(Entity).all()
123 123
124   -def get_entities2(mname=None, cid=None, limit=None):
  124 +def get_entities2(cid=None, limit=None):
125 125 if cid is None:
126   - return web.ctx.orm.query(Entity).filter(Entity.model.has(name=mname)).order_by(Entity.id.desc()).all()
  126 + return web.ctx.orm.query(Entity).order_by(Entity.id.desc()).all()
127 127 else:
128 128 if limit is None:
129   - return web.ctx.orm.query(Entity).filter(Entity.model.has(name=mname)).filter(Entity.categories.any(id=cid)).order_by(Entity.id.desc()).all()
  129 + return web.ctx.orm.query(Entity).filter(Entity.categories.any(id=cid)).order_by(Entity.id.desc()).all()
130 130 else:
131   - return web.ctx.orm.query(Entity).filter(Entity.model.has(name=mname)).filter(Entity.categories.any(id=cid)).order_by(Entity.id.desc()).limit(limit)
  131 + return web.ctx.orm.query(Entity).filter(Entity.categories.any(id=cid)).order_by(Entity.id.desc()).limit(limit)
132 132
133 133 def get_entity(id):
134 134 return web.ctx.orm.query(Entity).get(id)
@@ -150,5 +150,5 @@ def del_entity(id):
150 150 entity = get_entity(id)
151 151 web.ctx.orm.delete(entity)
152 152
153   -def get_latest_entities(mname, cid, count):
154   - return get_entities2(mname, cid, count);
  153 +def get_latest_entities(cid, count):
  154 + return get_entities2(cid, count);
1  common/widget.py
@@ -24,7 +24,6 @@ def validate(self, value):
24 24 def render(self):
25 25 html = super(MyInput, self).render()
26 26 if self.note:
27   -
28 27 #html += '<span class="input-notification error png_bg">%s</span>' % (self.note,)
29 28 html += '<div class="input-notification error png_bg">%s</div>' % (self.note,)
30 29 return html
4 mod_wsgi.conf
@@ -14,9 +14,9 @@ WSGILazyInitialization On
14 14 AddType text/html .wsgi
15 15
16 16 WSGIPythonPath /media/king/code/web/pycms
17   -WSGIScriptAlias /pycms /media/king/code/web/pycms/app.wsgi
  17 +WSGIScriptAlias /app /media/king/code/web/pycms/app.wsgi
18 18
19   -Alias /pycms/static /media/king/code/web/pycms/static
  19 +Alias /static /media/king/code/web/pycms/static
20 20
21 21 <Directory /media/king/code/web/pycms/>
22 22 Order deny,allow
8 models/dbutil.py
@@ -136,9 +136,11 @@ def save_entity(model, id, base_data, data):
136 136 populate(base_entity, base_data, Entity)
137 137 populate(entity, data, model_cls)
138 138
139   - cid = 1 #data.cid
140   - category = get_category(cid)
141   - base_entity.categories.append(category)
  139 + for i in range(len(base_entity.categories)-1,-1,-1):
  140 + del base_entity.categories[i]
  141 + for cid in base_data.cids:
  142 + if cid:
  143 + base_entity.categories.append(get_category(int(cid)))
142 144
143 145 if id == -1:
144 146 base_entity.model = model
BIN  static/images/admin_img/icon_addlink.gif
BIN  static/images/admin_img/icon_deletelink.gif
4 templates/admin/category_edit.html
@@ -32,7 +32,7 @@
32 32 <td>
33 33 <span id="normal_add"><input type="text" value="${_s(form.d.name)}" class="input-text" id="name" name="name">
34 34 % if form.name.note:
35   - <div id="catnameTip" class="onShow">${form.name.note}</div>
  35 + <div id="catnameTip" class="input-notification error png_bg">${form.name.note}</div>
36 36 % endif
37 37 </span>
38 38 </td>
@@ -41,7 +41,7 @@
41 41 <th>${form.slug.description}</th>
42 42 <td><input type="text" value="${_s(form.d.slug)}" class="input-text" id="slug" name="slug">
43 43 % if form.slug.note:
44   - <div id="catdirTip" class="onShow">${form.slug.note}</div>
  44 + <div id="catdirTip" class="input-notification error png_bg">${form.slug.note}</div>
45 45 % endif
46 46 </td>
47 47 </tr>
55 templates/admin/entity_edit.html
@@ -5,6 +5,22 @@
5 5 <script language="javascript" type="text/javascript" src="${static_url}/js/colorpicker.js"></script>
6 6 <script language="javascript" type="text/javascript" src="${static_url}/js/hotkeys.js"></script>
7 7 <script language="javascript" type="text/javascript" src="${static_url}/js/cookie.js"></script>
  8 +<script>
  9 + $(document).ready(function(){
  10 + $("#add_cate_btn").click(function(){
  11 + template = $("#cidblk_0");
  12 + newcidblk = template.clone();
  13 + newcidblk.append("<a href=\"javascript:void(0)\" title=\"删除栏目\"><img src=\"${static_url}/images/admin_img/icon_deletelink.gif\"></a>&nbsp;&nbsp;&nbsp;");
  14 + rmbtn = newcidblk.find("a")
  15 + rmbtn.click(function(obj){
  16 + return function() {
  17 + obj.remove();
  18 + }
  19 + }(newcidblk));
  20 + newcidblk.insertBefore($(this));
  21 + });
  22 + });
  23 +</script>
8 24
9 25 <form name="myform" id="myform" action="" method="post" enctype="multipart/form-data">
10 26 <table width="100%" cellspacing="0" class="table_form">
@@ -23,15 +39,44 @@
23 39 </tfoot>
24 40
25 41 <tbody>
26   - <tr>
27   - <th width="80"> <font color="red">*</font>栏目</th>
28   - </tr>
29   -
30 42 % for input in base_form.inputs:
31 43 <tr>
32 44 <th width="80">${'<font color="red">*</font>' if input.required else ''}${input.description}</th>
33 45 <td>
34   - ${input.render()}
  46 + % if input.name == 'cids':
  47 + % if base_form.d.cids:
  48 + % for i, cid in enumerate(base_form.d.cids):
  49 + <span id="cidblk_${i}">
  50 + <select id="cid${i}" name="cids">
  51 + <option value="">≡ 选择栏目≡</option>
  52 + % for cate in categories:
  53 + <option value="${cate.id}" ${'selected="selected"' if str(cid) == str(cate.id) else ''}>${cate.prefix+cate.name}</option>
  54 + % endfor
  55 + </select>
  56 + % if i != 0:
  57 + <a href="javascript:void(0)" title="删除栏目" onclick="javascript:$(this).parent().remove()" ><img src="${static_url}/images/admin_img/icon_deletelink.gif"></a>&nbsp;
  58 + % endif
  59 + </span>
  60 + % endfor
  61 + &nbsp;&nbsp;<a id="add_cate_btn" href="javascript:void(0)" title="添加另一个栏目"><img src="${static_url}/images/admin_img/icon_addlink.gif"></a>
  62 + % else:
  63 + <span id="cidblk_0">
  64 + <select id="cid0" name="cids">
  65 + <option value="">≡ 选择栏目 ≡</option>
  66 + % for cate in categories:
  67 + <option value="${cate.id}" >${cate.prefix+cate.name}</option>
  68 + % endfor
  69 + </select>
  70 + </span>
  71 + &nbsp;&nbsp;<a id="add_cate_btn" href="javascript:void(0)" title="添加另一个栏目"><img src="${static_url}/images/admin_img/icon_addlink.gif"></a>
  72 + % endif
  73 + % if base_form.cids.note:
  74 + <div class="input-notification error png_bg">${base_form.cids.note}</div>
  75 + % endif
  76 +
  77 + % else:
  78 + ${input.render()}
  79 + % endif
35 80 </td>
36 81 </tr>
37 82 % endfor
6 templates/admin/model_edit.html
@@ -20,7 +20,7 @@
20 20 <td>
21 21 <span id="normal_add"><input type="text" value="${_s(form.d.title)}" class="input-text" id="name" name="title">
22 22 % if form.title.note:
23   - <div id="catnameTip" class="onShow">${form.title.note}</div>
  23 + <div class="input-notification error png_bg">${form.title.note}</div>
24 24 % endif
25 25 </span>
26 26 </td>
@@ -30,7 +30,7 @@
30 30 <td>
31 31 <span id="normal_add"><input type="text" value="${_s(form.d.name)}" class="input-text" id="name" name="name">
32 32 % if form.name.note:
33   - <div id="catnameTip" class="onShow">${form.name.note}</div>
  33 + <div class="input-notification error png_bgon">${form.name.note}</div>
34 34 % endif
35 35 </span>
36 36 </td>
@@ -45,7 +45,7 @@
45 45 % endfor
46 46 </select>
47 47 % if form.template_id.note:
48   - <div id="modelidTip" class="onShow">${form.template_id.note}</div>
  48 + <div class="input-notification error png_bgon">${form.template_id.note}</div>
49 49 % endif
50 50 </td>
51 51 </tr>
6 templates/index.html
@@ -22,7 +22,7 @@ <h5 class="title-1">新闻<a href=
22 22 "more">更多&gt;&gt;</a></h5>
23 23 <div class="content">
24 24 <ul class="list lh24 f14">
25   - ${latest_entities('news', 1, 5)}
  25 + ${latest_entities(1, 5)}
26 26 </ul>
27 27 </div>
28 28 </div>
@@ -71,7 +71,7 @@ <h5 class="title-1">医药猎头职位<a href=
71 71 "more">更多&gt;&gt;</a></h5>
72 72 <div class="content">
73 73 <ul class="list lh24 f14">
74   - ${latest_entities('job', 1, 5)}
  74 + ${latest_entities(2, 5)}
75 75 </ul>
76 76 </div>
77 77 </div>
@@ -120,7 +120,7 @@ <h5 class="title-1">医疗猎头职位<a href=
120 120 "more">更多&gt;&gt;</a></h5>
121 121 <div class="content">
122 122 <ul class="list lh24 f14">
123   - ${latest_entities('elite', 1, 5)}
  123 + ${latest_entities(4, 5)}
124 124 </ul>
125 125 </div>
126 126 </div>
6 templates/lib/entity.html
... ... @@ -1,6 +1,6 @@
1 1
2   -<%def name="latest_entities(mname, catid, count)">
3   - % for entity in get_latest_entities(mname, catid, count):
4   - <li>·<a style="" title="${entity.title}" target="_blank" href="${mname}/${entity.created_time.year}/${entity.created_time.month}/${entity.created_time.day}/${entity.id}.html">${entity.title}</a></li>
  2 +<%def name="latest_entities(catid, count)">
  3 + % for entity in get_latest_entities(catid, count):
  4 + <li>·<a style="" title="${entity.title}" target="_blank" href="${entity.model.name}/${entity.created_time.year}/${entity.created_time.month}/${entity.created_time.day}/${entity.id}.html">${entity.title}</a></li>
5 5 % endfor
6 6 </%def>

0 comments on commit 7dd84cf

Please sign in to comment.
Something went wrong with that request. Please try again.