Ejemplo n.º 1
0
def delete_record(request):
    page_id = request.GET['page_id']
    record_id = request.GET['record_id']
    pagestore = pagestore_manager.get_pagestore('mongo')
    pagestore.remove_record(record_id, page_id)
    if not hasattr(request, 'is_from_app'):
        return HttpResponseRedirect(request.META['HTTP_REFERER'])
Ejemplo n.º 2
0
def update_record_display_index(request):
    pagestore = pagestore_manager.get_pagestore('mongo')
    page_id = request.POST.get('page_id', None)
    if not page_id and ('app' in request.POST):
        app = request.POST['app']
        module = request.POST['module']
        resource = request.POST['resource']
        page_id = 'apps:{}:{}:{}'.format(app, module, resource)

    src_record_id = request.POST.get('src_id', None)
    dst_record_id = request.POST.get('dst_id', None)
    if dst_record_id == u'0':
        #置顶操作
        order = request.POST['sort_attr']
        if order[0] == '-':
            order = 'desc'
        else:
            order = 'asc'
        pagestore.set_record_to_top(src_record_id, order, page_id)
    else:
        src_record = pagestore.get_record(src_record_id, page_id)
        dst_record = pagestore.get_record(dst_record_id, page_id)
        pagestore.update_record_display_index(src_record_id,
                                              dst_record['display_index'],
                                              page_id)
        pagestore.update_record_display_index(dst_record_id,
                                              src_record['display_index'],
                                              page_id)

    response = create_response(200)
    return response.get_response()
Ejemplo n.º 3
0
def get_article_list(request):
    pagestore = pagestore_manager.get_pagestore(request)
    project_id = request.display_info['project_id']
    project = request.display_info['project']
    datasource_project_id = request.display_info['datasource_project_id']

    #获得category
    category_record = request.display_info['datasource_record']
    category_name = category_record['model']['name']

    #获得article
    article_records = pagestore.get_records(datasource_project_id,
                                            article_page_id)
    articles = []
    for article_record in article_records:
        article_model = article_record['model']
        print article_model['category'], ' ', category_name
        if article_model['category'] == category_name:
            articles.append({
                'title':
                article_model['title'],
                'summary':
                article_model['content'][:20],
                'link':
                './?type=viper&rid=%s&project_id=%d' %
                (article_record['id'], request.project.id)
            })

    data = {'article_list': {'articles': articles}}
    return data
Ejemplo n.º 4
0
def show_viper_production_page(request):
    pagestore = pagestore_manager.get_pagestore(request)

    project_id = request.GET['project_id']
    page_id = request.GET.get('page_id', None)
    page = pagestore.get_page(project_id, page_id)

    return HttpResponseRedirect(
        '/workbench/viper/page/?project_id=%s&page_id=%s' %
        (page['project_id'], page['page_id']))
Ejemplo n.º 5
0
def example_api(request):
	pagestore = pagestore_manager.get_pagestore(request)

	data = {
		'product_list': {
			'data_groups': [{
				'datas': []
			}]
		},
		'product_image': {'image': ''},
	}
	return data
Ejemplo n.º 6
0
def get_all_records(request, app_settings):
    __update_page_id(request)
    pagestore = pagestore_manager.get_pagestore('mongo')
    page_id = request.GET['page_id']
    if hasattr(request, 'webapp_owner_id'):
        #处理从webapp来的请求
        user_id = request.webapp_owner_id
    else:
        #处理从后台系统来的请求
        user_id = request.user.id
    total_count, records = pagestore.get_records(user_id, '', page_id, {})
    result = []
    for record in records:
        data = record['model']
        data['id'] = record['id']
        result.append(data)
    return result
Ejemplo n.º 7
0
def get_record(request, app_settings):
    __update_page_id(request)
    pagestore = pagestore_manager.get_pagestore('mongo')
    record_id = request.GET['record_id']
    page_id = request.GET['page_id']
    if getattr(app_settings, 'STORE_ENGINE', '') == 'mysql':
        record = mysql_model_util.get_record(request)
    else:
        record = pagestore.get_record(record_id, page_id)
        record['model']['id'] = record['id']
        record = record['model']

    __get_record_hook = getattr(request, '__get_record_hook', None)
    if __get_record_hook:
        __get_record_hook(request, record)

    return record
Ejemplo n.º 8
0
def create_page_by_id(request, return_html_snippet=False, get_record=False):
    project_id = request.GET['project_id']
    page_id = request.GET['page_id']

    pagestore = pagestore_manager.get_pagestore(request)
    project = Project.objects.get(id=project_id)
    page = pagestore.get_page(project_id, page_id)

    request.page = page
    html = pagerender.create_mobile_page_html_content(request, page,
                                                      page['component'],
                                                      project)

    if return_html_snippet:
        return html
    else:
        response = create_response(200)
        response.data = {
            'html': html,
            'title': page['component']['model']['navName']
        }
        return response.get_response()
Ejemplo n.º 9
0
def create_record(request):
    #获取project
    project_id = request.GET['project_id']

    page_id = request.GET.get('page_id', None)

    pagestore = pagestore_manager.get_pagestore('mongo')
    if request.POST:
        page_id = request.POST.get('__page_id')
        record = __get_fields_to_be_save(request)
        __save_record_hook = getattr(request, '__save_record_hook', None)
        if __save_record_hook:
            __save_record_hook(request, record)
        pagestore.save_record(request.user.id, project_id, page_id, record)

        redirect_to_page_id = request.GET.get('submit_redirect_to', page_id)
        if not hasattr(request, 'is_from_app'):
            return HttpResponseRedirect(
                '/workbench/viper/page/?project_id=%s&page_id=%s' %
                (project_id, redirect_to_page_id))
    else:
        if not hasattr(request, 'is_from_app'):
            return show_free_page(request)
Ejemplo n.º 10
0
def show_free_page(request):
    project_id = request.GET['project_id']
    page_id = request.GET['page_id']

    pagestore = pagestore_manager.get_pagestore(request)

    #获取左侧导航信息
    webapp_editor_nav = __get_webapp_navs(request, project_id, pagestore)

    #获取page对象
    project = Project.objects.get(id=project_id)
    if project.source_project_id == 0:
        page = pagestore.get_page(project_id, page_id)
    else:
        page = pagestore.get_page(str(project.source_project_id), page_id)
    #获取二级导航信息
    page_model = page['component']['model']
    page_type = page_model['type']
    if page_type == 'top_level_page':
        second_nav_name = page['page_id']
    else:
        second_nav_name = request.COOKIES.get('top_level_page_id', '')

    #获取html页面
    if hasattr(request, 'should_get_record') and request.should_get_record:
        html = viper_api_views.create_page(request,
                                           return_html_snippet=True,
                                           get_record=True)
    else:
        html = viper_api_views.create_page(request, return_html_snippet=True)

    jsons = []
    if 'record' in request.page:
        jsons.append({
            "name": "record",
            "content": json.dumps(request.page['record']['model'])
        })

    #渲染最终页面
    c = RequestContext(
        request, {
            'project_id': project_id,
            'jsons': jsons,
            'page_html_content': html,
            'second_navs': webapp_editor_nav,
            'first_nav_name': FIRST_NAV_NAME,
            'second_nav_name': second_nav_name
        })
    response = render_to_response('workbench/viper_record_page.html', c)

    #设置cookie,保持导航信息跟踪
    if page_type == 'top_level_page':
        response.set_cookie('top_level_page_id',
                            page['page_id'],
                            max_age=3600 * 24)
        response.set_cookie('top_level_page_nav_name',
                            page_model['navName'].encode('utf8'),
                            max_age=3600 * 24)
    else:
        pass

    return response
Ejemplo n.º 11
0
def list_records_bak(request):
    from workbench.templatetags import workbench_filter

    pagestore = pagestore_manager.get_pagestore(request)
    #获得project
    project_id = request.GET['project_id']
    project = Project.objects.get(id=project_id)
    #获得左侧导航
    webapp_editor_nav = __get_webapp_navs(request, project_id, pagestore)
    #获得page
    page_id = request.GET.get('page_id', None)
    if project.source_project_id == 0:
        page = pagestore.get_page(project_id, page_id)
    else:
        page = pagestore.get_page(str(project.source_project_id), page_id)

    page_model = page['component']['model']
    is_enable_paginate = (page_model['is_enable_paginate'] == 'yes')

    #获取page list页面的columns
    #TODO: 将columns的存储从json str变为json obj
    page['columns'] = []
    for column in json.loads(page_model['columns']):
        if column['is_checked']:
            if not column['name']:
                column['name'] = 'component_%d' % column['id']
            page['columns'].append(column)
    page['columns'].sort(lambda x, y: cmp(x['index'], y['index']))

    #填充filter
    '''
	page.filter_component = None
	cid2component = {}
	for sub_component in page.component['components']:
		cid2component[sub_component['cid']] = sub_component
	for column in page.columns:
		if 'checkbox_group' in column['type']:
			cid = column['id']
			sub_component = cid2component[cid]
			if sub_component['model']['is_filter_in_list_page'] == 'yes':
				page.filter_component = sub_component
	filters = None
	current_filter_name = request.GET.get('filter', 'all')
	if page.filter_component:
		filter_component = page.filter_component
		filter_page_id = filter_component['model']['datasource_page'].split('-')[1]
		filter_page_field = filter_component['model']['datasource_field']

		filters = [{'name':'all', 'text':u'全部'}]
		if current_filter_name == 'all':
			page.current_filter = filters[0]

		for filter_record in Record.objects.filter(page_id=filter_page_id):
			filter_record_model = json.loads(filter_record.json_content)
			filter_name = '%s$%s_%d' % (filter_page_field, filter_page_id, filter_record.id)
			filter = {
				'name': filter_name,
				'text': filter_record_model[filter_page_field]
			}
			filters.append(filter)
			if filter_name == current_filter_name:
				page.current_filter = filter
	'''
    current_filter_name = 'all'
    page['filters'] = []

    #获取record集合
    records = pagestore.get_records(project_id, page['page_id'])
    if not current_filter_name == 'all':
        filtered_records = []
        for record in records:
            if current_filter_name in record.json_content:
                filtered_records.append(record)
        records = filtered_records

    #分页
    pageinfo_json_str = None
    if is_enable_paginate:
        count_per_page = int(page_model['count_per_page'])
        cur_page = int(request.GET.get('page', '1'))
        pageinfo, records = paginator.paginate(
            records,
            cur_page,
            count_per_page,
            query_string=request.META['QUERY_STRING'])
        pageinfo_json_str = json.dumps(paginator.to_dict(pageinfo))

    #渲染页面
    c = RequestContext(
        request, {
            'project_id': project_id,
            'project': project,
            'page': page,
            'records': records,
            'second_navs': webapp_editor_nav,
            'first_nav_name': FIRST_NAV_NAME,
            'second_nav_name': page['page_id'],
            'pageinfo': pageinfo_json_str
        })
    return render_to_response('workbench/viper_records_page.html', c)
Ejemplo n.º 12
0
def __preprocess_page(request, get_record):
    pagestore = pagestore_manager.get_pagestore('mongo')
    if hasattr(request, 'page'):
        #生成app时,request中会携带page信息
        project_id = request.GET['project_id']
        project = Project.objects.get(id=project_id)
        page = request.page
    else:
        if request.POST:
            project_id = request.POST['project_id']
            project = Project.objects.get(id=project_id)
            page_component = json.loads(request.POST['page'])
            page_id = page_component['cid']
            if project.source_project_id == 0:
                page = pagestore.get_page(project_id, page_id)
            else:
                page = pagestore.get_page(str(project.source_project_id),
                                          page_id)
            page['component'] = page_component
        else:
            project_id = request.GET['project_id']
            project = Project.objects.get(id=project_id)
            page_id = request.GET.get('page_id', None)
            if project.source_project_id == 0:
                page = pagestore.get_page(project_id, page_id)
            else:
                page = pagestore.get_page(str(project.source_project_id),
                                          page_id)

            if get_record:
                record_id = request.GET['record_id']
                page['record'] = pagestore.get_record(record_id)

    page_component = page['component']

    if not 'components' in page_component:
        page_component['components'] = []
    if not 'global_content_components' in page_component:
        page_component['global_content_components'] = []

    extra_context = {}
    for component in page_component['components']:
        if component['type'] == 'viper.simulator':
            extra_context[
                'simulator'] = pagerender.create_mobile_page_html_content(
                    request, page, component, project)
            extra_context['simulator_component'] = component

    request.extra_page_context = extra_context
    if 'simulator' in extra_context:
        page_component['components'].remove(
            extra_context['simulator_component'])
        if len(page_component['components']) == 0:
            page_component['components'].append({
                'type': 'empty',
                'model': {
                    'index': 1
                }
            })

    target_page = page

    id2page = dict([(page['page_id'], page)
                    for page in pagestore.get_pages(project_id)])
    workbench_filter.RENDER_CONTEXT['id2page'] = id2page

    return project, target_page
Ejemplo n.º 13
0
def __get_records_from_mysql(request):
    from workbench.templatetags import workbench_filter

    models_path = 'apps.customerized_apps.'

    pagestore = pagestore_manager.get_pagestore('mongo')
    #获得project
    project_id = request.GET['project_id']
    '''
	处理page_id
	'''
    is_query_from_app = False
    if (not 'page_id' in request.GET) and ('app' in request.GET):
        is_query_from_app = True
        app = request.GET['app']
        module = request.GET['module']
        resource = request.GET['resource']
        page_id = 'apps:{}:{}:{}'.format(app, module, resource)
    else:
        page_id = request.GET.get('page_id', None)
        if page_id and page_id[0] == 'p':
            page_id = page_id.split('-')[1]

    is_enable_paginate = (request.GET.get('enable_paginate', '0') == '1')

    #TODO: 进行缓存
    mysql_models_module_path = 'apps.customerized_apps.{}.mysql_models'.format(
        request.GET['app'])
    mysql_models_module = __import__(mysql_models_module_path, {}, {}, [
        '*',
    ])
    model_class_name = request.GET['model_class']
    model_class = getattr(mysql_models_module, model_class_name)
    '''
	获取record集合
	'''
    params = {"owner": request.user}
    #处理sort_attr参数
    sort_attr = request.GET.get('sort_attr', 'display_index')
    sort_attr = sort_attr.replace('display_index', 'id')
    #获取filter参数
    filter_attr = request.GET.get('filter_attr', None)
    filter_value = request.GET.get('filter_value', None)
    if filter_attr:
        params[filter_attr] = filter_value
    #获取search参数
    search = request.GET.get('query', None)
    if search:
        search_attr = 'model.{}'.format(request.GET.get('query_attr', ''))
        params['%s_contains' % search_attr] = search

    records = model_class.objects.filter(**params).order_by(sort_attr)

    #分页
    pageinfo = None
    if is_enable_paginate:
        count_per_page = int(request.GET.get('count_per_page', '15'))
        cur_page = int(request.GET.get('page', '1'))
        pageinfo, records = paginator.paginate(
            records,
            cur_page,
            count_per_page,
            query_string=request.META['QUERY_STRING'])

    items = []

    #解析target_fields
    target_fields = request.GET.get('target_fields', '')
    if len(target_fields) > 0:
        target_fields = target_fields.split(',')
    else:
        target_fields = ()
    buf = []
    for target_field in target_fields:
        field, field_type = target_field.split(':')
        buf.append({'field': field, 'type': field_type})
    target_fields = buf

    #从records中抽取item
    submit_redirect_to_page_id = request.GET.get('submit_redirect_to', '')
    for record in records:
        meta_data = {"id": record.id}
        result = []
        for target_field in target_fields:
            if target_field['type'] == 'link':
                if hasattr(request, 'is_from_app'):
                    link = '#haha'
                elif is_query_from_app:
                    _, app_name, page_module, page_name = page_id.split(':')
                    link = '<a class="btn btn-mini" href="/apps/%s/?module=%s&resource=%s&action=update&project_id=%s&record_id=%s&submit_redirect_to=%s"><i class="icon icon-pencil"></i></a>' % (
                        app_name, page_module, page_name, project_id,
                        record.id, submit_redirect_to_page_id)
                else:
                    link = '<a class="btn btn-mini" href="/workbench/viper/record/update/?project_id=%s&page_id=%s&record_id=%s&submit_redirect_to=%s"><i class="icon icon-pencil"></i></a>' % (
                        project_id, page_id, record.id,
                        submit_redirect_to_page_id)
                result.append(link)
                meta_data["has_operation_link"] = True
            else:
                field_data = getattr(record, target_field['field'], '')
                if type(field_data) == datetime:
                    field_data = str(field_data)
                elif 'json:' in field_data:
                    #json格式存储的数据,比如select component存储的数据为
                    #json:{"type":"select","text":"刮刮卡","value":"guaguaka"}这样的格式
                    #需要解析出其中的value
                    field_data = json.loads(
                        field_data.split('json:')[1])['text']
                result.append(field_data)
        items.append({"meta_data": meta_data, "data": result})

    return pageinfo, items
Ejemplo n.º 14
0
def __get_records_from_pagestore(request):
    from workbench.templatetags import workbench_filter

    pagestore = pagestore_manager.get_pagestore('mongo')
    #获得project
    project_id = request.GET['project_id']
    '''
	处理page_id
	'''
    is_query_from_app = False
    if (not 'page_id' in request.GET) and ('app' in request.GET):
        is_query_from_app = True
        app = request.GET['app']
        module = request.GET['module']
        resource = request.GET['resource']
        page_id = 'apps:{}:{}:{}'.format(app, module, resource)
    else:
        page_id = request.GET.get('page_id', None)
        if page_id and page_id[0] == 'p':
            page_id = page_id.split('-')[1]

    is_enable_paginate = (request.GET.get('enable_paginate', '0') == '1')
    '''
	获取record集合
	'''
    options = {}
    #处理sort_attr参数
    sort_attr = request.GET.get('sort_attr', 'display_index')
    if sort_attr[0] == '-':
        sort_attr = sort_attr[1:]
        direction = pymongo.DESCENDING
    else:
        direction = pymongo.ASCENDING
    if sort_attr != 'display_index':
        sort_attr = 'model.{}'.format(sort_attr)
    options['sort'] = [(sort_attr, direction)]
    #获取filter参数
    filter_attr = request.GET.get('filter_attr', None)
    filter_value = request.GET.get('filter_value', None)
    if filter_value != u'-1' and filter_value != None:
        filter_attr = 'model.raw_{}'.format(filter_attr)
        options['filter'] = {filter_attr: filter_value}
    #获取search参数
    search = request.GET.get('query', None)
    if search:
        search_attr = 'model.{}'.format(request.GET.get('query_attr', ''))
        options['search'] = {search_attr: search}
    #获取pagination参数
    if is_enable_paginate:
        cur_page = int(request.GET.get('page', 1))
        count_per_page = int(request.GET.get('count_per_page', 20))
        options['pagination'] = {
            'cur_page': cur_page - 1,
            'count_per_page': count_per_page
        }

    if hasattr(request, 'webapp_owner_id'):
        #处理从webapp来的请求
        user_id = request.webapp_owner_id
    else:
        #处理从后台系统来的请求
        user_id = request.user.id
    total_count, records = pagestore.get_records(user_id, project_id, page_id,
                                                 options)

    #分页
    pageinfo = None
    if is_enable_paginate:
        count_per_page = int(request.GET.get('count_per_page', '15'))
        cur_page = int(request.GET.get('page', '1'))
        #TODO: 当total_count很大时,temp_records的构造会带来性能问题
        temp_records = [1] * total_count  #构造临时的数据,获取pageinfo信息
        pageinfo, _ = paginator.paginate(
            temp_records,
            cur_page,
            count_per_page,
            query_string=request.META['QUERY_STRING'])

    items = []

    #解析target_fields
    target_fields = request.GET.get('target_fields', '')
    if len(target_fields) > 0:
        target_fields = target_fields.split(',')
    else:
        target_fields = ()
    buf = []
    for target_field in target_fields:
        field, field_type = target_field.split(':')
        buf.append({'field': field, 'type': field_type})
    target_fields = buf

    #从records中抽取item
    submit_redirect_to_page_id = request.GET.get('submit_redirect_to', '')
    for record in records:
        model = record['model']
        meta_data = {"id": record['id']}
        result = []
        for target_field in target_fields:
            if target_field['type'] == 'link':
                if hasattr(request, 'is_from_app'):
                    link = '#haha'
                elif is_query_from_app:
                    _, app_name, page_module, page_name = page_id.split(':')
                    link = '<a class="btn btn-mini" href="/apps/%s/?module=%s&resource=%s&action=update&project_id=%s&record_id=%s&submit_redirect_to=%s"><i class="icon icon-pencil"></i></a>' % (
                        app_name, page_module, page_name, project_id,
                        record['id'], submit_redirect_to_page_id)
                else:
                    link = '<a class="btn btn-mini" href="/workbench/viper/record/update/?project_id=%s&page_id=%s&record_id=%s&submit_redirect_to=%s"><i class="icon icon-pencil"></i></a>' % (
                        project_id, page_id, record['id'],
                        submit_redirect_to_page_id)
                result.append(link)
                meta_data["has_operation_link"] = True
            else:
                field_data = model.get(target_field['field'], '')
                if 'json:' in field_data:
                    #json格式存储的数据,比如select component存储的数据为
                    #json:{"type":"select","text":"刮刮卡","value":"guaguaka"}这样的格式
                    #需要解析出其中的value
                    field_data = json.loads(
                        field_data.split('json:')[1])['text']
                result.append(field_data)
        items.append({"meta_data": meta_data, "data": result})

    return pageinfo, items