Ejemplo n.º 1
0
class TweetView(ModelView):
    column_list = ('name', 'user_name', 'text')
    column_sortable_list = ('name', 'text')

    column_filters = (filters.FilterEqual('name', 'Name'),
                      filters.FilterNotEqual('name', 'Name'),
                      filters.FilterLike('name', 'Name'),
                      filters.FilterNotLike('name', 'Name'),
                      filters.BooleanEqualFilter('testie', 'Testie'))

    column_searchable_list = ('name', 'text')

    form = TweetForm

    def get_list(self, *args, **kwargs):
        count, data = super(TweetView, self).get_list(*args, **kwargs)

        # Grab user names
        query = {'_id': {'$in': [x['user_id'] for x in data]}}
        users = db.user.find(query, fields=('name', ))

        # Contribute user names to the models
        users_map = dict((x['_id'], x['name']) for x in users)

        for item in data:
            item['user_name'] = users_map.get(item['user_id'])

        return count, data

    # Contribute list of user choices to the forms
    def _feed_user_choices(self, form):
        users = db.user.find(fields=('name', ))
        form.user_id.choices = [(str(x['_id']), x['name']) for x in users]
        return form

    def create_form(self):
        form = super(TweetView, self).create_form()
        return self._feed_user_choices(form)

    def edit_form(self, obj):
        form = super(TweetView, self).edit_form(obj)
        return self._feed_user_choices(form)

    # Correct user_id reference before saving
    def on_model_change(self, form, model):
        user_id = model.get('user_id')
        model['user_id'] = ObjectId(user_id)

        return model
Ejemplo n.º 2
0
class TweetView(ModelView):
    column_list = ('name', 'user_name', 'text')
    column_sortable_list = ('name', 'text')

    column_filters = (filters.FilterEqual('name', 'Name'),
                      filters.FilterNotEqual('name', 'Name'),
                      filters.FilterLike('name', 'Name'),
                      filters.FilterNotLike('name', 'Name'),
                      filters.BooleanEqualFilter('testie', 'Testie'))

    # column_searchable_list = ('name', 'text')

    form = TweetForm

    def get_list(self, *args, **kwargs):
        # not necessary but kept as example
        count, data = super(TweetView, self).get_list(*args, **kwargs)

        # Contribute user_name to the models
        for item in data:
            user = collection_users.find_one({'_id': item['user_id']})
            if user:
                item['user_name'] = user['_id']

        return count, data

    # Contribute list of user choices to the forms
    def _feed_user_choices(self, form):
        users = collection_users.find(fields=('_id', ))
        form.user_id.choices = [(str(x['_id']), x['_id']) for x in users]
        return form

    def create_form(self):
        form = super(TweetView, self).create_form()
        return self._feed_user_choices(form)

    def edit_form(self, obj):
        form = super(TweetView, self).edit_form(obj)
        return self._feed_user_choices(form)
Ejemplo n.º 3
0
class SchoolView(ModelView):
    def is_accessible(self):
       
        
        return current_user.is_authenticated()
    
    
    
    column_list = ('name', 'branchName')
    column_sortable_list = ('name')

    column_filters = (filters.FilterEqual('name', 'name'),filters.FilterNotEqual('name', 'name'),filters.FilterLike('name', 'name'),filters.FilterNotLike('name', 'name'))
                     

    column_searchable_list = ('name', 'name')

    form = SchoolForm
    
 
    def get_list(self, *args, **kwargs):
        count, data = super(SchoolView, self).get_list(*args, **kwargs)

        # Grab user names
        #query = {'name': {'$in': [x['user_id'] for x in data]}}
       
        for d in data:
            bn=[]
            for b in d["Branch"]:
                bn.append(b["branch"])
            d["branchName"]=bn
                
           

        

        return count, data
Ejemplo n.º 4
0
class LogView(MongoView):

    collection = log_log_coll

    column_list = (
        'meta.project',
        'meta.job',
        'meta.page',
        'level',
        'meta.worker',  # 'meta.task_id',
        'message',
        'created')

    column_filters = (filters.FilterEqual('project', u'关联项目'),
                      filters.FilterEqual('page', u'关联页面'),
                      filters.FilterEqual('task_id', u'关联任务'),
                      filters.FilterEqual('level', u'日志级别'))

    column_labels = {
        'meta.project': u'关联项目',
        'meta.job': u'关联任务',
        'meta.page': u'关联页面',
        'meta.action': u'关联动作',
        'meta.task_id': u'相关任务',
        'level': u'日志级别',
        # 'worker': u'日志级别',
        'message': u'信息',
        'created': u'记录时间',
    }

    column_formatters = {
        'message': message_format,
        # 'status': map_format({START: u'启用', PAUSE: u'暂停', STOP: u'停用'}),
        # 'category': map_format({'ds': u'电商', 'sb': u'社保', 'dx': u'电信运营商'}),
        # 'type': map_format({0: u'通用', 1: u'存储', 2: u'告警'}),
        'created': date_format,
    }

    column_display_actions = True

    column_extra_row_actions = [
        EndpointLinkRowAction('fa fa-tasks',
                              'log.get_task_info',
                              id_field='task_id'),
        EndpointLinkRowAction('fa fa-file-code-o',
                              'log.get_page_info',
                              id_field='task_id')
    ]

    # def initialize(self):
    #     if self.coll_name == 'crawl':
    #         self.column_list = ('_project', '_page', '_task_id', 'level', '_worker', 'message', '_created')
    #     else:
    #         self.column_list = ('_code', '_action', '_task_id', 'level', '_worker', 'message', '_created')

    def _get_default_order(self):
        return ('created', 1)

    # def get_list(self, page, sort_column, sort_desc, search, filters,
    #              execute=True, page_size=None):
    #     task_infos = self.db.task_infos
    #     task_logs = self.db.task_logs

    # def get_list(self, page, sort_column, sort_desc, search, filters,
    #              execute=True, page_size=None):
    #     """
    #         Get list of objects from MongoEngine
    #
    #         :param page:
    #             Page number
    #         :param sort_column:
    #             Sort column
    #         :param sort_desc:
    #             Sort descending
    #         :param search:
    #             Search criteria
    #         :param filters:
    #             List of applied fiters
    #         :param execute:
    #             Run query immediately or not
    #         :param page_size:
    #             Number of results. Defaults to ModelView's page_size. Can be
    #             overriden to change the page_size limit. Removing the page_size
    #             limit requires setting page_size to 0 or False.
    #     """
    #     # db.hotel.aggregate([
    #     #   { $sort: { url: -1 } },
    #     #   { $group: {_id: "$url"} },
    #     #   { $skip: 40 },
    #     #   { $limit: 30 }
    #     # ])
    #     # db.collection.distinct(field, query, options)                   <================
    #
    #     query = {}
    #
    #     # Filters
    #     if self._filters:
    #         data = []
    #
    #         for flt, flt_name, value in filters:
    #             f = self._filters[flt]
    #             data = f.apply(data, value)
    #
    #         if data:
    #             if len(data) == 1:
    #                 query = data[0]
    #             else:
    #                 query['$and'] = data
    #
    #     # Search
    #     if self._search_supported and search:
    #         query = self._search(query, search)
    #
    #     # Get count
    #     count = self.coll.find(query).count() if not self.simple_list_pager else None
    #
    #     # Sorting
    #     sort_by = None
    #
    #     if sort_column:
    #         sort_by = [(sort_column, pymongo.DESCENDING if sort_desc else pymongo.ASCENDING)]
    #     else:
    #         order = self._get_default_order()
    #
    #         if order:
    #             sort_by = [(order[0], pymongo.DESCENDING if order[1] else pymongo.ASCENDING)]
    #
    #     # Pagination
    #     if page_size is None:
    #         page_size = self.page_size
    #
    #     skip = 0
    #
    #     if page and page_size:
    #         skip = page * page_size
    #     projection = {"_id": 1, "meta": 1}
    #     results = task_infos.find(query, projection=projection, sort=sort_by, skip=skip, limit=page_size)
    #
    #     if execute:
    #         rows = []
    #         for row in results:
    #             rows.append(row)
    #             row["logs"] = list(task_logs.find({"meta.task_id": row["_id"]}))
    #         # results = list(results)
    #
    #     return count, results

    @expose('/task-info/')
    def get_task_info(self):
        task_id = get_mdict_item_or_list(request.args, 'id')
        print('task_id ==> ', task_id)
        task = log_task_coll.find_one({'task_id': self._get_valid_id(task_id)})
        tmpl = 'admin/model/log_task_info.html'
        return self.render(
            tmpl,
            # get_value=self.get_list_value,
            get_value=lambda m, c: m[c],
            get_type=type,
            model=task)

    @expose('/page-info/')
    def get_page_info(self):
        return_url = get_redirect_target() or self.get_url('.index_view')
        task_id = get_mdict_item_or_list(request.args, 'id')
        page = log_page_coll.find({'task_id': task_id})

        if page.count() < 1:
            flash('没找到相关任务的页面Response信息', 'error')
            return redirect(return_url)

        tmpl = 'admin/model/log_page_info.html'
        return self.render(
            tmpl,
            # get_value=self.get_list_value,
            get_value=lambda m, c: m[c],
            get_type=type,
            model=page)
Ejemplo n.º 5
0
class StudentView(ModelView):
    def is_accessible(self):

        return current_user.is_authenticated()

    def user_link_formatter(view, context, model, name):

        id = model["_id"]
        url = "/admin/grades?sid=" + str(id)
        name = model["fname"]
        return Markup('<a href="{}">{}</a>').format(url, name)

    column_details_list = ('fname', 'mname', 'lname', 'schoolName', 'dob',
                           'className')

    def user_link_formatter(view, context, model, name):

        id = model["_id"]
        url = "/admin/grades?sid=" + str(id)
        name = model["fname"]
        return Markup('<a href="{}">{}</a>').format(url, name)

    column_details_list = ('fname', 'mname', 'lname', 'schoolName', 'dob',
                           'className')

    def user_link_formatter(view, context, model, name):

        id = model["_id"]
        url = "/admin/grades?sid=" + str(id)
        name = model["fname"]
        return Markup('<a href="{}">{}</a>').format(url, name)

    def action_link_formatter(view, context, model, name):

        id = model["_id"]
        url = "/admin/grades?sid=" + str(id)
        name = model["fname"]
        return Markup('<a href="{}">{}</a>').format(url, "Enter Grades")

    column_details_list = ('fname', 'mname', 'lname', 'schoolName', 'dob',
                           'className')
    column_list = ('fname', 'mname', 'lname', 'schoolName', 'dob', 'className',
                   'Action')
    column_formatters = {
        "fname": user_link_formatter,
        "Action": action_link_formatter
    }

    column_formatters = {"fname": user_link_formatter}

    column_formatters = {"fname": user_link_formatter}

    column_sortable_list = ('fname')
    column_searchable_list = ('fname', 'className')
    column_filters = (filters.FilterEqual('fname', 'fname'),
                      filters.FilterNotEqual('fname', 'fname'),
                      filters.FilterLike('fname', 'fname'),
                      filters.FilterEqual('className', 'className'),
                      filters.FilterLike('schoolName', 'schoolName'))
    form = StudentForm

    def get_list(self, *args, **kwargs):
        count, data = super(StudentView, self).get_list(*args, **kwargs)

        for d in data:
            sid = d["school"]
            school = db.School.find_one({"_id": ObjectId(sid)})
            cid = d["classAssigned"]
            classAssigned = db.Classes.find_one({"_id": ObjectId(cid)})
            #print(school)

            d["schoolName"] = school["name"]
            d["className"] = classAssigned["name"]

        return count, data

        # Contribute list of user choices to the forms
    def _feed_school_choices(self, form):
        school = db.School.find(fields=('name', ))
        form.school.choices = [(str(x['_id']), x['name']) for x in school]
        return form

    def create_form(self):
        form = super(StudentView, self).create_form()
        return self._feed_school_choices(form)

    def edit_form(self, obj):
        form = super(StudentView, self).edit_form(obj)
        return self._feed_school_choices(form)