def search(request): data = [] db = DepartmentBase() objs = [] name = request.REQUEST.get('name') city_name = request.REQUEST.get('city_name') order = request.REQUEST.get('order', '0') page_index = int(request.REQUEST.get('page_index')) objs, all_count = db.search_departments_for_admin(name, city_name, order) page_objs = page.Cpt(objs, count=10, page=page_index).info # 格式化json num = 10 * (page_index - 1) data = format_department(page_objs[0], num) return HttpResponse(json.dumps({ 'data': data, 'page_count': page_objs[4], 'total_count': page_objs[5], 'all_count': all_count }), mimetype='application/json')
def get_department_by_id(request): department_id = request.REQUEST.get('department_id') data = format_department( [DepartmentBase().get_department_by_id(department_id)], 1)[0] return HttpResponse(json.dumps(data), mimetype='application/json')
def random_department(context): """ @note: 随机出现营业部 """ from common import utils from www.kaihu.interface import DepartmentBase, CityBase city_abbr = utils.get_sub_domain_from_http_host(context['request'].META.get('HTTP_HOST', '')) city = CityBase().get_city_by_pinyin_abbr(city_abbr) departments = DepartmentBase().get_departments_by_random(city.id)[:10] return render_to_response('kaihu/_random_department.html', locals(), context_instance=context).content
def get_departments_by_name(request): ''' 根据名字查询营业部 ''' department_name = request.REQUEST.get('department_name') result = [] departments = DepartmentBase().get_departments_by_name(department_name) if departments: for x in departments[:10]: result.append([x.id, x.name, None, x.name]) return HttpResponse(json.dumps(result), mimetype='application/json')
def add_article(request): title = request.REQUEST.get('title') content = request.REQUEST.get('content') city_id = request.REQUEST.get('belong_city') department_id = request.REQUEST.get('belong_department') sort_num = request.REQUEST.get('sort') # 如果设置了营业部,则使用营业部所属城市 if department_id: city_id = DepartmentBase().get_department_by_id(department_id).city.id code, obj = ArticleBase().add_article(title, content, city_id, department_id, sort_num) return code, obj.id if code == 0 else ''
def modify_article(request): article_id = request.REQUEST.get('article_id') title = request.REQUEST.get('title') content = request.REQUEST.get('content') city_id = request.REQUEST.get('belong_city') department_id = request.REQUEST.get('belong_department') sort_num = request.REQUEST.get('sort') # 如果设置了营业部,则使用营业部所属城市 if department_id: city_id = DepartmentBase().get_department_by_id(department_id).city.id return ArticleBase().modify_article(article_id, title=title, content=content, city_id=city_id, department_id=department_id, sort_num=sort_num)
def modify_department(request): department_id = request.REQUEST.get('department_id') name = request.REQUEST.get('name') sort_num = request.REQUEST.get('sort') tel = request.REQUEST.get('tel') city_id = request.REQUEST.get('belong_city') # company_id = request.REQUEST.get('belong_company') addr = request.REQUEST.get('addr') des = request.REQUEST.get('des') district_id = request.REQUEST.get('district_id') district_id = district_id if district_id else None return DepartmentBase().modify_department(department_id, name=name, sort_num=sort_num, tel=tel, addr=addr, des=des, city_id=city_id, district_id=district_id)
def add_department(request): name = request.REQUEST.get('name') sort_num = request.REQUEST.get('sort') tel = request.REQUEST.get('tel') city_id = request.REQUEST.get('belong_city') company_id = request.REQUEST.get('belong_company') addr = request.REQUEST.get('addr') des = request.REQUEST.get('des') district_id = request.REQUEST.get('district_id') district_id = district_id if district_id else None code, obj = DepartmentBase().add_department( name=name, sort_num=sort_num, tel=tel, addr=addr, des=des, city_id=city_id, district_id=district_id, company_id=company_id, ) return code, obj.id if code == 0 else obj
def format_article(objs, num): data = [] for x in objs: num += 1 city = CityBase().get_city_by_id(x.city_id) if x.city_id else None data.append({ 'num': num, 'article_id': x.id, 'title': x.title, 'content': x.content, 'city_id': city.id if city else '', 'city_name': city.city if city else '', 'city_pinyin_abbr': city.pinyin_abbr if city else '', 'department_id': x.department_id if x.department_id else '', 'department_name': DepartmentBase().get_department_by_id(x.department_id).name if x.department_id else '', 'sort_num': x.sort_num, 'state': x.state, 'create_time': str(x.create_time) }) return data