def ack(sakuya_db, id): def error(msg): return {'status': 'error', 'msg': msg} def log(chart, category): with open(get_config('webapp')['chart_ack_log'], 'a') as f: l = u'[%s] user: %s, chart: %d-%s, category: %d-%s\n' %\ (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), auth.get_user()['username'], chart.id, chart.name, category.id, category.name) f.write(l.encode('utf-8')) chart = sakuya_db.query(Charts).get(id) if chart is None: return error('图表不存在') if haopan.is_haopan(chart.cate_id): return error('栏目不正确') if 'cl2' in request.params: # create category and move into it cl2 = sakuya_db.query(Categories).get(request.params['cl2']) if cl2 is None: return error('参数不正确') cl3_name = request.params.get('cl3') if not cl3_name: return error('栏目名称不能为空') cl3_exists = sakuya_db.query(Categories).\ filter_by(parent_cate_id=cl2.id, name=cl3_name).\ count() > 0 if cl3_exists: return error('栏目已存在') cl3 = Categories() cl3.name = cl3_name cl3.parent_cate_id = cl2.id cl3.is_parent = False sakuya_db.add(cl3) sakuya_db.commit() chart.cate_id = cl3.id chart.owner = auth.get_user()['username'] sakuya_db.commit() else: # move to category cl3 = sakuya_db.query(Categories).get(request.params.get('cl3')) if cl3 is None: return error('栏目不存在') chart.cate_id = cl3.id chart.owner = auth.get_user()['username'] sakuya_db.commit() log(chart, cl3) return {'status': 'ok'}
def add_category(sakuya_db, pid): if not request.forms.name: return abort(400) parent = sakuya_db.query(Categories).get(pid) if parent is None or not parent.is_parent: return abort(404) row = Categories() row.name = request.forms.name row.parent_cate_id = pid row.is_parent = request.forms.isp == '1' sakuya_db.add(row) sakuya_db.commit() return redirect('/admin/category/%d' % pid)
except Exception, e: return util.output('error', msg='Invalid parameters.') pid = libchart.SUITE_PID for i in xrange(len(category)): cate = sakuya_db.\ query(Categories).\ filter_by(parent_cate_id=pid, name=category[i]).\ first() if cate is None: cate = Categories() cate.name = category[i] cate.parent_cate_id = pid cate.is_parent = i < 2 sakuya_db.add(cate) sakuya_db.commit() pid = cate.id # after loop the pid will be the lowest level category dt_date = dt.strftime('%Y%m%d') Chartdata = ChartdataTmpl.gen(dt_date) Logstuff = LogstuffTmpl.gen(dt_date) for k, v in data.iteritems(): title_segs = list(category) title_segs.append(str(k)) title = '::'.join(title_segs) chart = sakuya_db.\