Exemplo n.º 1
0
class RelHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mapp = MApp()
        self.mpost = MPost()
        self.rel_post2app = MRelPost2App()
        self.rel_app2post = MRelApp2Post()

    def get(self, url_str=''):
        if len(url_str) > 0:
            ip_arr = url_str.split('/')
        else:
            return False
        if len(ip_arr) == 2:
            self.add_relation(ip_arr)

    def check_app(self, tt, uid):

        if tt == 'post':
            if False == self.mpost.get_by_id(uid):
                return False
        if tt == 'app':
            if False == self.mapp.get_by_uid(uid):
                return False
        return True

    def add_relation(self, url_arr):
        if self.check_app(url_arr[0], url_arr[1]):
            pass
        else:
            return False

        last_post_id = self.get_secure_cookie('last_post_uid')
        if last_post_id:
            last_post_id = last_post_id.decode('utf-8')

        last_app_id = self.get_secure_cookie('use_app_uid')
        if last_app_id:
            last_app_id = last_app_id.decode('utf-8')

        if url_arr[0] == 'info':
            if last_post_id:
                self.rel_post2app.add_relation(last_post_id, url_arr[1], 2)
                self.rel_app2post.add_relation(url_arr[1], last_post_id, 1)

        if url_arr[0] == 'post':
            if last_app_id:
                self.rel_app2post.add_relation(last_app_id, url_arr[1], 2)
                self.rel_post2app.add_relation(url_arr[1], last_app_id, 1)
Exemplo n.º 2
0
class InfoHandler(BaseHandler):
    def initialize(self, hinfo=''):
        self.init()
        self.mevaluation = MEvaluation()
        self.mapp2catalog = MApp2Catalog()
        self.mapp2tag = MApp2Label()
        self.minfo = MApp()
        self.musage = MUsage()
        self.mcat = MInforCatalog()
        self.mrel = MAppRel()
        self.mreply = MApp2Reply()

    def get(self, url_str=''):
        url_arr = self.parse_url(url_str)
        if len(url_arr) == 1 and len(url_str) == 4:
            self.view_info(url_str)
        else:
            kwd = {
                'title': '',
                'info': '',
            }
            self.render('html/404.html',
                        kwd=kwd,
                        userinfo=self.userinfo, )

    def post(self, url_str=''):
        url_arr = self.parse_url(url_str)
        if url_arr[0] == 'rel':
            if self.get_current_user():
                self.add_relation(url_arr[1], url_arr[2])
            else:
                self.redirect('/user/login')
        elif url_arr[0] == 'comment_add':
            self.add_comment(url_arr[1])
        else:
            return False

    @tornado.web.authenticated
    def add_comment(self, id_post):
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        post_data['user_id'] = self.userinfo.uid
        post_data['user_name'] = self.userinfo.user_name
        comment_uid = self.mreply.insert_data(post_data, id_post)
        if comment_uid:
            output = {
                'pinglun': comment_uid,
            }
        else:
            output = {
                'pinglun': 0,
            }
        return json.dump(output, self)

    def view_info(self, info_id):
        '''
        Render the info
        :param info_id:
        :return: Nonthing.
        '''

        rec = self.minfo.get_by_uid(info_id)

        if rec:
            pass
        else:
            kwd = {
                'info': '您要找的信息不存在。',
            }
            self.render('html/404.html',
                        kwd=kwd,
                        userinfo=self.userinfo, )
            return False

        replys = self.mreply.get_by_id(info_id)
        rel_recs = self.mrel.get_app_relations(rec.uid, 4)
        rand_recs = self.minfo.query_random(4 - rel_recs.count() + 2)
        self.chuli_cookie_relation(info_id)
        cookie_str = tools.get_uuid()

        if 'def_cat_uid' in rec.extinfo:
            catid = rec.extinfo['def_cat_uid']
        else:
            catid = ''

        parent_name = self.mcat.get_by_id(catid[:2] + '00').name if catid != '' else ''
        if catid != '':
            cat_rec = self.mcat.get_by_uid(catid)
            priv_mask_idx = cat_rec.priv_mask.index('1')
            cat_name = cat_rec.name
        else:
            priv_mask_idx = 0
            cat_name = ''

        parentname = '<a href="/list/{0}">{1}</a>'.format(catid[:2] + '00', parent_name)

        catname = '<a href="/list/{0}">{1}</a>'.format(catid, cat_name)

        kwd = {
            'pager': '',
            'url': self.request.uri,
            'cookie_str': cookie_str,
            'daohangstr': '',
            'signature': info_id,
            'tdesc': '',
            'eval_0': self.mevaluation.app_evaluation_count(info_id, 0),
            'eval_1': self.mevaluation.app_evaluation_count(info_id, 1),
            'site_url': config.site_url,
            'login': 1 if self.get_current_user() else 0,
            'has_image': 0,
            'parentlist': self.mcat.get_parent_list(),
            'parentname': parentname,
            'catname': catname,
        }
        self.minfo.view_count_increase(info_id)
        if self.get_current_user():
            self.musage.add_or_update(self.userinfo.uid, info_id)
        self.set_cookie('user_pass', cookie_str)
        tmpl = self.ext_tmpl_name(rec) if self.ext_tmpl_name(rec) else self.get_tmpl_name(rec)
        catid = rec.extinfo['def_cat_uid'] if 'def_cat_uid' in rec.extinfo else None
        print(rec.extinfo)
        self.render(tmpl,
                    kwd=dict(kwd, **self.extra_kwd(rec)),
                    calc_info=rec,
                    userinfo=self.userinfo,
                    relations=rel_recs,
                    rand_recs=rand_recs,
                    unescape=tornado.escape.xhtml_unescape,
                    ad_switch=random.randint(1, 18),
                    tag_info=self.mapp2tag.get_by_id(info_id),
                    recent_apps=self.musage.query_recent(self.get_current_user(), 6)[1:],
                    post_info=rec,
                    replys=replys,
                    cat_enum=self.mcat.get_qian2(catid[:2]) if catid else [],
                    priv_mask_idx=priv_mask_idx,
                    )

    def extra_kwd(self, info_rec):
        '''
        The additional information.
        :param info_rec:
        :return: directory.
        '''
        return {}

    def chuli_cookie_relation(self, app_id):
        '''
        The current Info and the Info viewed last should have some relation.
        And the last viewed Info could be found from cookie.
        :param app_id: the current app
        :return: None
        '''
        last_app_uid = self.get_secure_cookie('use_app_uid')
        if last_app_uid:
            last_app_uid = last_app_uid.decode('utf-8')
        self.set_secure_cookie('use_app_uid', app_id)
        if last_app_uid and self.minfo.get_by_uid(last_app_uid):
            self.add_relation(last_app_uid, app_id)

    def ext_tmpl_name(self, rec):
        return None

    def get_tmpl_name(self, rec):
        '''
        According to the application, each info of it's classification could has different temaplate.
        :param rec: the App record.
        :return: the temaplte path.
        '''
        if 'def_cat_uid' in rec.extinfo and rec.extinfo['def_cat_uid'] != '':
            cat_id = rec.extinfo['def_cat_uid']
        else:
            cat_id = False
        if cat_id:
            tmpl = 'autogen/view/view_{0}.html'.format(cat_id)
        else:
            tmpl = 'infor/app/show_map.html'
        return tmpl

    def add_relation(self, f_uid, t_uid):
        '''
        Add the relation. And the from and to, should have different weight.
        :param f_uid:
        :param t_uid:
        :return: return True if the relation has been succesfully added.
        '''
        if self.minfo.get_by_uid(t_uid):
            pass
        else:
            return False
        if f_uid == t_uid:
            return False
        self.mrel.add_relation(f_uid, t_uid, 2)
        self.mrel.add_relation(t_uid, f_uid, 1)
        return True
Exemplo n.º 3
0
class InfoHandler(BaseHandler):
    def initialize(self, hinfo=''):
        self.init()
        self.mevaluation = MEvaluation()
        self.mapp2catalog = MApp2Catalog()
        self.mapp2tag = MApp2Label()
        self.minfo = MApp()
        self.musage = MUsage()
        self.mcat = MAppCatalog()
        self.mrel = MAppRel()
        self.mreply = MApp2Reply()


    def get(self, url_str=''):
        url_arr = self.parse_url(url_str)

        if len(url_arr) == 1 and len(url_str) == 4:
            self.view_info(url_str)

        else:
            kwd = {
                'title': '',
                'info': '',
            }
            self.render('html/404.html', kwd=kwd,
                        userinfo=self.userinfo, )

    def post(self, url_str=''):

        url_arr = self.parse_url(url_str)

        if url_arr[0] == 'rel':
            if self.get_current_user():
                self.add_relation(url_arr[1])
            else:
                self.redirect('/user/login')
        elif url_arr[0] == 'comment_add':
            self.add_comment(url_arr[1])

        else:
            return False

    @tornado.web.authenticated
    def add_comment(self, id_post):
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        post_data['user_id'] = self.userinfo.uid
        post_data['user_name'] = self.userinfo.user_name
        comment_uid = self.mreply.insert_data(post_data, id_post)
        if comment_uid:
            output = {
                'pinglun': comment_uid,
            }
        else:
            output = {
                'pinglun': 0,
            }
        return json.dump(output, self)

    def view_info(self, info_id):
        '''
        Render the info
        :param info_id:
        :return: Nonthing.
        '''

        rec = self.minfo.get_by_uid(info_id)

        if rec:
            pass
        else:
            kwd = {
                'info': '您要找的信息不存在。',
            }
            self.render('html/404.html',
                        kwd=kwd,
                        userinfo=self.userinfo, )
            return False

        replys = self.mreply.get_by_id(info_id)
        rel_recs = self.mrel.get_app_relations(rec.uid, 4)
        rand_recs = self.minfo.query_random(4 - rel_recs.count() + 2)
        self.chuli_cookie_relation(info_id)
        cookie_str = tools.get_uuid()

        kwd = {
            'pager': '',
            'url': self.request.uri,
            'cookie_str': cookie_str,

            'signature': info_id,
            'tdesc': '',
            'eval_0': self.mevaluation.app_evaluation_count(info_id, 0),
            'eval_1': self.mevaluation.app_evaluation_count(info_id, 1),
            'site_url': config.site_url,
            'login': 1 if self.get_current_user() else 0,
            'has_image': 0,
            'parentlist': self.mcat.get_parent_list(),
        }
        self.minfo.view_count_increase(info_id)
        if self.get_current_user():
            self.musage.add_or_update(self.userinfo.uid, info_id)
        self.set_cookie('user_pass', cookie_str)
        tmpl = self.ext_tmpl_name(rec) if self.ext_tmpl_name(rec) else self.get_tmpl_name(rec)
        catid = rec.extinfo['def_cat_uid'] if 'def_cat_uid' in rec.extinfo else None
        self.render(tmpl,
                    kwd=dict(kwd, **self.extra_kwd(rec)),
                    calc_info=rec,
                    userinfo=self.userinfo,
                    relations=rel_recs,
                    rand_recs=rand_recs,
                    unescape=tornado.escape.xhtml_unescape,
                    ad_switch=random.randint(1, 18),
                    tag_info=self.mapp2tag.get_by_id(info_id),
                    recent_apps=self.musage.query_recent(self.get_current_user(), 6)[1:],
                    post_info=rec,
                    replys=replys,
                    cat_enum = self.mcat.get_qian2(catid[:2]) if catid else [],
                    )

    def extra_kwd(self, info_rec):
        '''
        The additional information.
        :param info_rec:
        :return: directory.
        '''
        return {}

    def chuli_cookie_relation(self, app_id):
        '''
        The current Info and the Info viewed last should have some relation.
        And the last viewed Info could be found from cookie.
        :param app_id: the current app
        :return: None
        '''
        last_map_id = self.get_secure_cookie('use_app_uid')
        if last_map_id:
            last_map_id = last_map_id.decode('utf-8')
        self.set_secure_cookie('use_app_uid', app_id)
        if last_map_id and self.minfo.get_by_uid(last_map_id):
            self.add_relation(last_map_id, app_id)

    def ext_tmpl_name(self, rec):
        return None

    def get_tmpl_name(self, rec):
        '''
        According to the application, each info of it's classification could has different temaplate.
        :param rec: the App record.
        :return: the temaplte path.
        '''
        if 'def_cat_uid' in rec.extinfo and rec.extinfo['def_cat_uid'] != '':
            cat_id = rec.extinfo['def_cat_uid']
        else:
            cat_id = False
        if cat_id:
            tmpl = 'autogen/view/view_{0}.html'.format(cat_id)
        else:
            tmpl = 'tmpl_applite/app/show_map.html'
        return tmpl

    def add_relation(self, f_uid, t_uid):
        '''
        Add the relation. And the from and to, should have different weight.
        :param f_uid:
        :param t_uid:
        :return: return True if the relation has been succesfully added.
        '''
        if self.minfo.get_by_uid(t_uid):
            pass
        else:
            return False
        if f_uid == t_uid:
            return False
        self.mrel.add_relation(f_uid, t_uid, 2)
        self.mrel.add_relation(t_uid, f_uid, 1)
        return True
Exemplo n.º 4
0
class MetaHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mappcat = MInforCatalog()
        self.mevaluation = MEvaluation()
        self.mapp2catalog = MApp2Catalog()
        self.mapp2tag = MApp2Label()
        self.mapp = MApp()
        self.musage = MUsage()
        self.mtag = MInforCatalog()
        self.mrel = MAppRel()
        self.mreply = MApp2Reply()
        if 'app_url_name' in cfg:
            self.app_url_name = cfg['app_url_name']
        else:
            self.app_url_name = 'info'

    def get(self, url_str=''):

        url_arr = self.parse_url(url_str)

        if url_arr[0] == 'cat_add':
            self.user_to_add(url_arr[1])
        elif url_arr[0] == 'catalog':
            self.catalog()
        elif len(url_arr) == 1 and len(url_str) == 4:
            self.redirect('/{0}/{1}'.format(self.app_url_name, url_arr[0]))
        elif len(url_arr) == 2:
            if url_arr[0] == 'edit':
                self.to_edit_app(url_arr[1])
            elif url_arr[0] == 'add':
                self.to_add_app(url_arr[1])
            elif url_arr[0] == 'delete':
                self.to_del_app(url_arr[1])


            else:
                '''
                从相关计算中过来的。
                '''
                self.mrel.update_relation(url_arr[1], url_arr[0])
                self.redirect('/{0}/{1}'.format(self.app_url_name, url_arr[0]))
        else:
            kwd = {
                'title': '',
                'info': '',
            }
            self.render('html/404.html', kwd=kwd,
                        userinfo=self.userinfo, )

    def post(self, url_str=''):

        url_arr = self.parse_url(url_str)

        if url_arr[0] == 'to_add':
            self.add()
        elif url_arr[0] == 'rel':
            if self.get_current_user():
                self.add_relation(url_arr[1])
            else:
                self.redirect('/user/login')
        elif url_arr[0] == 'comment_add':
            self.add_comment(url_arr[1])
        elif url_arr[0] == 'edit':
            self.update(url_arr[1])
        elif url_arr[0] == 'add':
            self.add(url_arr[1])
        else:
            return False

    def catalog(self):
        self.render('infor/app/catalog.html',
                    userinfo=self.userinfo,
                    kwd={'uid': '',}
                    )

    @tornado.web.authenticated
    def user_to_add(self, catid):

        uid = tools.get_uu4d()
        while self.mapp.get_by_uid(uid):
            uid = tools.get_uu4d()
        kwd = {
            'uid': uid,
            'userid': self.userinfo.user_name,
            'def_cat_uid': catid,
            'parentname': self.mtag.get_by_id(catid[:2] + '00').name,
            'catname': self.mtag.get_by_id(catid).name,
        }
        self.render('autogen/add/add_{0}.html'.format(catid),
                    userinfo=self.userinfo,
                    kwd=kwd)

    def check_priv(self, userinfo, cat_id):
        cat_rec = self.mappcat.get_by_uid(cat_id)
        priv_mask_idx = cat_rec.priv_mask.index('1')
        priv_dic = {'ADD': False, 'EDIT': False, 'DELETE': False, 'ADMIN': False}
        if userinfo.privilege[priv_mask_idx] >= '1':
            priv_dic['ADD'] = True
        if userinfo.privilege[priv_mask_idx] >= '2':
            priv_dic['EDIT'] = True
        if userinfo.privilege[priv_mask_idx] >= '4':
            priv_dic['DELETE'] = True
        if userinfo.privilege[priv_mask_idx] >= '8':
            priv_dic['ADMIN'] = True
        return priv_dic

    @tornado.web.authenticated
    def to_add_app(self, uid):
        if self.mapp.get_by_uid(uid):
            self.redirect('/{0}/edit/{1}'.format(self.app_url_name, uid))
        else:
            self.render('infor/app/add.html',
                        tag_infos=self.mtag.query_all(),
                        userinfo=self.userinfo,
                        kwd={'uid': uid,}
                        )

    @tornado.web.authenticated
    def to_del_app(self, uid):
        current_infor = self.mapp.get_by_uid(uid)
        if self.check_priv(self.userinfo, current_infor.extinfo['def_cat_uid'])['DELETE']:
            pass
        else:
            return False

        if self.mapp.delete(uid):
            self.redirect('/list/{0}'.format(current_infor.extinfo['def_cat_uid']))
        else:
            self.redirect('/info/{0}'.format(uid))

    @tornado.web.authenticated
    def to_edit_app(self, infoid):

        rec_info = self.mapp.get_by_uid(infoid)

        if rec_info:
            pass
        else:
            self.render('html/404.html')
            return
        if 'def_cat_uid' in rec_info.extinfo:
            catid = rec_info.extinfo['def_cat_uid']
        else:
            catid = ''

        kwd = {
            'def_cat_uid': catid,
            'parentname': self.mtag.get_by_id(catid[:2] + '00').name if catid != '' else '',
            'catname': self.mtag.get_by_id(catid).name if catid != '' else '',
            'parentlist': self.mtag.get_parent_list(),
            'userip': self.request.remote_ip
        }

        if catid:
            tmpl = 'autogen/edit/edit_{0}.html'.format(catid)
        else:
            tmpl = 'infor/app/edit.html'

        self.render(tmpl,
                    kwd=kwd,
                    calc_info=rec_info,
                    post_info=rec_info,
                    userinfo=self.userinfo,
                    app_info=rec_info,
                    unescape=tornado.escape.xhtml_unescape,
                    cat_enum=self.mappcat.get_qian2(catid[:2]),
                    tag_infos=self.mappcat.query_all(by_order=True),
                    app2tag_info=self.mapp2catalog.query_by_entry_uid(infoid),
                    app2label_info=self.mapp2tag.get_by_id(infoid), )

    @tornado.web.authenticated
    def update(self, uid):
        post_data = {}
        ext_dic = {}
        for key in self.request.arguments:
            if key.startswith('ext_') or key.startswith('tag_'):
                ext_dic[key] = self.get_argument(key)
            else:
                post_data[key] = self.get_arguments(key)

        post_data['user_name'] = self.userinfo.user_name

        current_info = self.mapp.get_by_uid(uid)

        if current_info.user_name == self.userinfo.user_name:
            pass
        elif self.userinfo.privilege[4] >= '1':
            pass
        elif 'def_cat_uid' in post_data and self.check_priv(self.userinfo, post_data['def_cat_uid'][0])['EDIT']:
            pass
        else:
            return False

        ext_dic['def_uid'] = str(uid)
        if 'def_cat_uid' in post_data:
            ext_dic['def_cat_uid'] = post_data['def_cat_uid'][0]
            ext_dic['def_cat_pid'] = '{0}00'.format(post_data['def_cat_uid'][0][:2])

        ext_dic['def_tag_arr'] = [x.strip() for x in post_data['tags'][0].strip().strip(',').split(',')]
        ext_dic = self.extra_data(ext_dic, post_data)
        self.mapp.modify_meta(uid,
                              post_data,
                              extinfo=ext_dic)
        self.update_catalog(uid)
        self.update_tag(uid)
        self.redirect('/{0}/{1}'.format(self.app_url_name, uid))

    @tornado.web.authenticated
    def add(self, uid=''):

        ext_dic = {}
        post_data = {}
        for key in self.request.arguments:
            if key.startswith('ext_') or key.startswith('tag_'):
                ext_dic[key] = self.get_argument(key)
            else:
                post_data[key] = self.get_arguments(key)

        if self.check_priv(self.userinfo, post_data['def_cat_uid'][0])['ADD']:
            pass
        else:
            return False

        if uid == '':
            uid = tools.get_uu4d()
            while self.mapp.get_by_uid(uid):
                uid = tools.get_uu4d()
            post_data['uid'][0] = uid

        post_data['user_name'] = self.userinfo.user_name

        ext_dic['def_uid'] = str(uid)
        if 'def_cat_uid' in post_data:
            ext_dic['def_cat_pid'] = '{0}00'.format(post_data['def_cat_uid'][0][:2])
            ext_dic['def_cat_uid'] = post_data['def_cat_uid'][0]

        ext_dic['def_tag_arr'] = [x.strip() for x in post_data['tags'][0].strip().strip(',').split(',')]
        ext_dic = self.extra_data(ext_dic, post_data)
        print(post_data)
        self.mapp.modify_meta(ext_dic['def_uid'],
                              post_data,
                              extinfo=ext_dic)
        self.update_catalog(ext_dic['def_uid'])
        self.update_tag(ext_dic['def_uid'])

        self.redirect('/list/{0}'.format(ext_dic['def_cat_uid']))

    @tornado.web.authenticated
    def extra_data(self, ext_dic, post_data):
        '''
        The additional information.
        :param post_data:
        :return: directory.
        '''
        return ext_dic

    @tornado.web.authenticated
    def update_tag(self, signature):

        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        current_tag_infos = self.mapp2tag.get_by_id(signature)
        tags_arr = [x.strip() for x in post_data['tags'][0].split(',')]
        for tag_name in tags_arr:
            if tag_name == '':
                pass
            else:
                self.mapp2tag.add_record(signature, tag_name, 1)

        for cur_info in current_tag_infos:
            if cur_info.tag.name in tags_arr:
                pass
            else:
                self.mapp2tag.remove_relation(signature, cur_info.tag)

    @tornado.web.authenticated
    def update_catalog(self, signature):
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        current_catalog_infos = self.mapp2catalog.query_by_entry_uid(signature)

        new_tag_arr = []
        for idx, key in enumerate(['cat_1', 'cat_2', 'cat_3', 'cat_4', 'cat_5', 'def_cat_uid']):
            if key in post_data:
                vv = post_data[key][0]
                if vv == '':
                    pass
                else:
                    new_tag_arr.append(vv)
                    self.mapp2catalog.add_record(signature, vv, idx)
            else:
                pass

        for cur_info in current_catalog_infos:
            if str(cur_info.catalog.uid).strip() in new_tag_arr:
                pass
            else:
                self.mapp2catalog.remove_relation(signature, cur_info.catalog)

    @tornado.web.authenticated
    def add_comment(self, id_post):
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        post_data['user_id'] = self.userinfo.uid
        post_data['user_name'] = self.userinfo.user_name
        comment_uid = self.mreply.insert_data(post_data, id_post)
        if comment_uid:
            output = {
                'pinglun': comment_uid,
            }
        else:
            output = {
                'pinglun': 0,
            }
        return json.dump(output, self)

    def add_relation(self, f_uid, t_uid):
        if False == self.mapp.get_by_uid(t_uid):
            return False
        if f_uid == t_uid:
            '''
            关联其本身
            '''
            return False
        self.mrel.add_relation(f_uid, t_uid, 2)
        self.mrel.add_relation(t_uid, f_uid, 1)
        return True
Exemplo n.º 5
0
class MetaHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mappcat = MInforCatalog()
        self.mevaluation = MEvaluation()
        self.mapp2catalog = MApp2Catalog()
        self.mapp2tag = MApp2Label()
        self.mapp = MApp()
        self.musage = MUsage()
        self.mtag = MInforCatalog()
        self.mrel = MAppRel()
        self.mreply = MApp2Reply()
        if 'app_url_name' in cfg:
            self.app_url_name = cfg['app_url_name']
        else:
            self.app_url_name = 'info'

    def get(self, url_str=''):

        url_arr = self.parse_url(url_str)

        if url_arr[0] == 'cat_add':
            self.user_to_add(url_arr[1])
        elif url_arr[0] == 'catalog':
            self.catalog()
        elif len(url_arr) == 1 and len(url_str) == 4:
            self.redirect('/{0}/{1}'.format(self.app_url_name, url_arr[0]))
        elif len(url_arr) == 2:
            if url_arr[0] == 'edit':
                self.to_edit_app(url_arr[1])
            elif url_arr[0] == 'add':
                self.to_add_app(url_arr[1])
            elif url_arr[0] == 'delete':
                self.to_del_app(url_arr[1])

            else:
                '''
                从相关计算中过来的。
                '''
                self.mrel.update_relation(url_arr[1], url_arr[0])
                self.redirect('/{0}/{1}'.format(self.app_url_name, url_arr[0]))
        else:
            kwd = {
                'title': '',
                'info': '',
            }
            self.render(
                'html/404.html',
                kwd=kwd,
                userinfo=self.userinfo,
            )

    def post(self, url_str=''):

        url_arr = self.parse_url(url_str)

        if url_arr[0] == 'to_add':
            self.add()
        elif url_arr[0] == 'rel':
            if self.get_current_user():
                self.add_relation(url_arr[1])
            else:
                self.redirect('/user/login')
        elif url_arr[0] == 'comment_add':
            self.add_comment(url_arr[1])
        elif url_arr[0] == 'edit':
            self.update(url_arr[1])
        elif url_arr[0] == 'add':
            self.add(url_arr[1])
        else:
            return False

    def catalog(self):
        self.render('infor/app/catalog.html',
                    userinfo=self.userinfo,
                    kwd={
                        'uid': '',
                    })

    @tornado.web.authenticated
    def user_to_add(self, catid):

        uid = tools.get_uu4d()
        while self.mapp.get_by_uid(uid):
            uid = tools.get_uu4d()
        kwd = {
            'uid': uid,
            'userid': self.userinfo.user_name,
            'def_cat_uid': catid,
            'parentname': self.mtag.get_by_id(catid[:2] + '00').name,
            'catname': self.mtag.get_by_id(catid).name,
        }
        self.render('autogen/add/add_{0}.html'.format(catid),
                    userinfo=self.userinfo,
                    kwd=kwd)

    def check_priv(self, userinfo, cat_id):
        cat_rec = self.mappcat.get_by_uid(cat_id)
        priv_mask_idx = cat_rec.priv_mask.index('1')
        priv_dic = {
            'ADD': False,
            'EDIT': False,
            'DELETE': False,
            'ADMIN': False
        }
        if userinfo.privilege[priv_mask_idx] >= '1':
            priv_dic['ADD'] = True
        if userinfo.privilege[priv_mask_idx] >= '2':
            priv_dic['EDIT'] = True
        if userinfo.privilege[priv_mask_idx] >= '4':
            priv_dic['DELETE'] = True
        if userinfo.privilege[priv_mask_idx] >= '8':
            priv_dic['ADMIN'] = True
        return priv_dic

    @tornado.web.authenticated
    def to_add_app(self, uid):
        if self.mapp.get_by_uid(uid):
            self.redirect('/{0}/edit/{1}'.format(self.app_url_name, uid))
        else:
            self.render('infor/app/add.html',
                        tag_infos=self.mtag.query_all(),
                        userinfo=self.userinfo,
                        kwd={
                            'uid': uid,
                        })

    @tornado.web.authenticated
    def to_del_app(self, uid):
        current_infor = self.mapp.get_by_uid(uid)
        if self.check_priv(self.userinfo,
                           current_infor.extinfo['def_cat_uid'])['DELETE']:
            pass
        else:
            return False

        if self.mapp.delete(uid):
            self.redirect('/list/{0}'.format(
                current_infor.extinfo['def_cat_uid']))
        else:
            self.redirect('/info/{0}'.format(uid))

    @tornado.web.authenticated
    def to_edit_app(self, infoid):

        rec_info = self.mapp.get_by_uid(infoid)

        if rec_info:
            pass
        else:
            self.render('html/404.html')
            return
        if 'def_cat_uid' in rec_info.extinfo:
            catid = rec_info.extinfo['def_cat_uid']
        else:
            catid = ''

        kwd = {
            'def_cat_uid':
            catid,
            'parentname':
            self.mtag.get_by_id(catid[:2] + '00').name if catid != '' else '',
            'catname':
            self.mtag.get_by_id(catid).name if catid != '' else '',
            'parentlist':
            self.mtag.get_parent_list(),
            'userip':
            self.request.remote_ip
        }

        if catid:
            tmpl = 'autogen/edit/edit_{0}.html'.format(catid)
        else:
            tmpl = 'infor/app/edit.html'

        self.render(
            tmpl,
            kwd=kwd,
            calc_info=rec_info,
            post_info=rec_info,
            userinfo=self.userinfo,
            app_info=rec_info,
            unescape=tornado.escape.xhtml_unescape,
            cat_enum=self.mappcat.get_qian2(catid[:2]),
            tag_infos=self.mappcat.query_all(by_order=True),
            app2tag_info=self.mapp2catalog.query_by_entry_uid(infoid),
            app2label_info=self.mapp2tag.get_by_id(infoid),
        )

    @tornado.web.authenticated
    def update(self, uid):
        post_data = {}
        ext_dic = {}
        for key in self.request.arguments:
            if key.startswith('ext_') or key.startswith('tag_'):
                ext_dic[key] = self.get_argument(key)
            else:
                post_data[key] = self.get_arguments(key)

        post_data['user_name'] = self.userinfo.user_name

        current_info = self.mapp.get_by_uid(uid)

        if current_info.user_name == self.userinfo.user_name:
            pass
        elif self.userinfo.privilege[4] >= '1':
            pass
        elif 'def_cat_uid' in post_data and self.check_priv(
                self.userinfo, post_data['def_cat_uid'][0])['EDIT']:
            pass
        else:
            return False

        ext_dic['def_uid'] = str(uid)
        if 'def_cat_uid' in post_data:
            ext_dic['def_cat_uid'] = post_data['def_cat_uid'][0]
            ext_dic['def_cat_pid'] = '{0}00'.format(
                post_data['def_cat_uid'][0][:2])

        ext_dic['def_tag_arr'] = [
            x.strip()
            for x in post_data['tags'][0].strip().strip(',').split(',')
        ]
        ext_dic = self.extra_data(ext_dic, post_data)
        self.mapp.modify_meta(uid, post_data, extinfo=ext_dic)
        self.update_catalog(uid)
        self.update_tag(uid)
        self.redirect('/{0}/{1}'.format(self.app_url_name, uid))

    @tornado.web.authenticated
    def add(self, uid=''):

        ext_dic = {}
        post_data = {}
        for key in self.request.arguments:
            if key.startswith('ext_') or key.startswith('tag_'):
                ext_dic[key] = self.get_argument(key)
            else:
                post_data[key] = self.get_arguments(key)

        if self.check_priv(self.userinfo, post_data['def_cat_uid'][0])['ADD']:
            pass
        else:
            return False

        if uid == '':
            uid = tools.get_uu4d()
            while self.mapp.get_by_uid(uid):
                uid = tools.get_uu4d()
            post_data['uid'][0] = uid

        post_data['user_name'] = self.userinfo.user_name

        ext_dic['def_uid'] = str(uid)
        if 'def_cat_uid' in post_data:
            ext_dic['def_cat_pid'] = '{0}00'.format(
                post_data['def_cat_uid'][0][:2])
            ext_dic['def_cat_uid'] = post_data['def_cat_uid'][0]

        ext_dic['def_tag_arr'] = [
            x.strip()
            for x in post_data['tags'][0].strip().strip(',').split(',')
        ]
        ext_dic = self.extra_data(ext_dic, post_data)
        print(post_data)
        self.mapp.modify_meta(ext_dic['def_uid'], post_data, extinfo=ext_dic)
        self.update_catalog(ext_dic['def_uid'])
        self.update_tag(ext_dic['def_uid'])

        self.redirect('/list/{0}'.format(ext_dic['def_cat_uid']))

    @tornado.web.authenticated
    def extra_data(self, ext_dic, post_data):
        '''
        The additional information.
        :param post_data:
        :return: directory.
        '''
        return ext_dic

    @tornado.web.authenticated
    def update_tag(self, signature):

        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        current_tag_infos = self.mapp2tag.get_by_id(signature)
        tags_arr = [x.strip() for x in post_data['tags'][0].split(',')]
        for tag_name in tags_arr:
            if tag_name == '':
                pass
            else:
                self.mapp2tag.add_record(signature, tag_name, 1)

        for cur_info in current_tag_infos:
            if cur_info.tag.name in tags_arr:
                pass
            else:
                self.mapp2tag.remove_relation(signature, cur_info.tag)

    @tornado.web.authenticated
    def update_catalog(self, signature):
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        current_catalog_infos = self.mapp2catalog.query_by_entry_uid(signature)

        new_tag_arr = []
        for idx, key in enumerate(
            ['cat_1', 'cat_2', 'cat_3', 'cat_4', 'cat_5', 'def_cat_uid']):
            if key in post_data:
                vv = post_data[key][0]
                if vv == '':
                    pass
                else:
                    new_tag_arr.append(vv)
                    self.mapp2catalog.add_record(signature, vv, idx)
            else:
                pass

        for cur_info in current_catalog_infos:
            if str(cur_info.catalog.uid).strip() in new_tag_arr:
                pass
            else:
                self.mapp2catalog.remove_relation(signature, cur_info.catalog)

    @tornado.web.authenticated
    def add_comment(self, id_post):
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        post_data['user_id'] = self.userinfo.uid
        post_data['user_name'] = self.userinfo.user_name
        comment_uid = self.mreply.insert_data(post_data, id_post)
        if comment_uid:
            output = {
                'pinglun': comment_uid,
            }
        else:
            output = {
                'pinglun': 0,
            }
        return json.dump(output, self)

    def add_relation(self, f_uid, t_uid):
        if False == self.mapp.get_by_uid(t_uid):
            return False
        if f_uid == t_uid:
            '''
            关联其本身
            '''
            return False
        self.mrel.add_relation(f_uid, t_uid, 2)
        self.mrel.add_relation(t_uid, f_uid, 1)
        return True
Exemplo n.º 6
0
class MetaHandler(BaseHandler):
    def initialize(self):
        self.init()
        self.mevaluation = MEvaluation()
        self.mapp2catalog = MApp2Catalog()
        self.mapp2tag = MApp2Label()
        self.mapp = MApp()
        self.musage = MUsage()
        self.mtag = MAppCatalog()
        self.mrel = MAppRel()
        self.mreply = MApp2Reply()

    def get(self, url_str=''):

        url_arr = self.parse_url(url_str)

        if url_arr[0] == 'cat_add':
            self.user_to_add(url_arr[1])
        elif url_arr[0] == 'catalog':
            self.catalog()
        elif len(url_arr) == 1 and len(url_str) == 4:
            self.redirect('/info/{0}'.format(url_arr[0]))
        elif len(url_arr) == 2:
            if url_arr[0] == 'edit':
                self.to_edit_app(url_arr[1])
            elif url_arr[0] == 'add':
                self.to_add_app(url_arr[1])
            else:
                '''
                从相关计算中过来的。
                '''
                self.mrel.update_relation(url_arr[1], url_arr[0])
                self.redirect('/{0}/{1}'.format(config.app_url_name, url_arr[0]))
        else:
            kwd = {
                'title': '',
                'info': '',
            }
            self.render('html/404.html', kwd=kwd,
                        userinfo=self.userinfo, )

    def post(self, url_str=''):

        url_arr = self.parse_url(url_str)

        if url_arr[0] == 'to_add':
            self.add()
        elif url_arr[0] == 'rel':
            if self.get_current_user():
                self.add_relation(url_arr[1])
            else:
                self.redirect('/user/login')
        elif url_arr[0] == 'comment_add':
            self.add_comment(url_arr[1])
        elif url_arr[0] == 'edit':
            self.update(url_arr[1])
        elif url_arr[0] == 'add':
            self.add(url_arr[1])
        else:
            return False

    def catalog(self):
        self.render('tmpl_applite/app/catalog.html',
                    userinfo=self.userinfo,
                    kwd={'uid': '',}
                    )

    @tornado.web.authenticated
    def user_to_add(self, catid):
        if self.is_admin():
            pass
        else:
            return

        uid = tools.get_uu4d()
        while self.mapp.get_by_uid(uid):
            uid = tools.get_uu4d()
        kwd = {
            'uid': uid,
            'userid': self.userinfo.user_name,
            'def_cat_uid': catid,
            'parentname': self.mtag.get_by_id(catid[:2] + '00').name,
            'catname': self.mtag.get_by_id(catid).name,
        }
        self.render('autogen/add/add_{0}.html'.format(catid), kwd=kwd)

    @tornado.web.authenticated
    def to_add_app(self, uid):
        if self.mapp.get_by_uid(uid):
            self.redirect('/map/edit/{0}'.format(uid))
        else:
            self.render('tmpl_applite/app/add.html',
                        tag_infos=self.mtag.query_all(),
                        userinfo=self.userinfo,
                        kwd={'uid': uid,}
                        )

    @tornado.web.authenticated
    def to_edit_app(self, infoid):
        if self.is_admin():
            pass
        else:
            return False

        rec_info = self.mapp.get_by_uid(infoid)

        if rec_info:
            pass
        else:
            self.render('html/404.html')
            return

        catid = rec_info.extinfo['def_cat_uid']

        kwd = {
            'def_cat_uid': catid,
            'parentname': self.mtag.get_by_id(catid[:2] + '00').name,
            'catname': self.mtag.get_by_id(catid).name,
            'parentlist': self.mtag.get_parent_list(),
            'userip': self.request.remote_ip

        }
        self.render('autogen/edit/edit_{0}.html'.format(catid),
                    kwd=kwd,
                    post_info=rec_info,
                    userinfo=self.userinfo,
                    app_info=rec_info,
                    unescape=tornado.escape.xhtml_unescape,
                    tag_infos=[],
                    app2label_info=self.mapp2tag.get_by_id(infoid), )

    # @tornado.web.authenticated
    # def to_edit_app(self, app_id):
    #     if self.userinfo.privilege[4] == '1':
    #         info = self.mapp.get_by_uid(app_id)
    #         self.render('tmpl_applite/app/edit.html',
    #                     userinfo = self.userinfo,
    #                     app_info=info,
    #                     unescape=tornado.escape.xhtml_escape,
    #                     tag_infos=self.mtag.query_all(),
    #                     app2tag_info=self.mapp2catalog.query_by_app_uid(app_id),
    #                     app2label_info=self.mapp2tag.get_by_id(app_id),
    #                     )
    #     else:
    #         return False

    @tornado.web.authenticated
    def update(self, uid):
        if self.is_admin():
            pass
        else:
            return

        post_data = {}
        ext_dic = {}
        for key in self.request.arguments:
            if key.startswith('ext_') or key.startswith('tag_'):
                ext_dic[key] = self.get_argument(key)
            else:
                post_data[key] = self.get_arguments(key)

        ext_dic['def_uid'] = str(uid)
        ext_dic['def_cat_uid'] = post_data['def_cat_uid'][0]

        self.mapp.modify_meta(uid, post_data, extinfo=ext_dic)
        self.update_catalog(uid)
        self.update_tag(uid)
        self.redirect('/info/{0}'.format(uid))

    @tornado.web.authenticated
    def add(self, uid=''):
        if self.userinfo.privilege[4] == '1':
            pass
        else:
            return False
        ext_dic = {}
        post_data = {}
        for key in self.request.arguments:
            if key.startswith('ext_') or key.startswith('tag_'):
                ext_dic[key] = self.get_argument(key)
            else:
                post_data[key] = self.get_arguments(key)

        if uid == '':
            uid = tools.get_uu4d()
            while self.mapp.get_by_uid(uid):
                uid = tools.get_uu4d()
            post_data['uid'][0] = uid

        ext_dic['def_uid'] = str(uid)
        ext_dic['def_cat_uid'] = post_data['def_cat_uid'][0]
        self.mapp.modify_meta(uid, post_data, extinfo=ext_dic)
        self.update_catalog(uid)
        self.update_tag(uid)

        self.redirect('/list/{0}'.format(ext_dic['def_cat_uid']))

    @tornado.web.authenticated
    def update_tag(self, signature):
        if self.userinfo.privilege[4] == '1':
            pass
        else:
            return False
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        current_tag_infos = self.mapp2tag.get_by_id(signature)

        tags_arr = [x.strip() for x in post_data['tags'][0].split(',')]
        for tag_name in tags_arr:
            if tag_name == '':
                pass
            else:
                self.mapp2tag.add_record(signature, tag_name, 1)

        for cur_info in current_tag_infos:
            if cur_info.tag.name in tags_arr:
                pass
            else:
                self.mapp2tag.remove_relation(signature, cur_info.tag)

    @tornado.web.authenticated
    def update_catalog(self, signature):
        if self.userinfo.privilege[4] == '1':
            pass
        else:
            return False
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        current_catalog_infos = self.mapp2catalog.query_by_app_uid(signature)

        new_tag_arr = []
        for key in ['cat_1', 'cat_2', 'cat_3', 'cat_4', 'cat_5']:
            if key in post_data:
                if post_data[key][0] == '':
                    pass
                else:
                    new_tag_arr.append(post_data[key][0])
                    self.mapp2catalog.add_record(signature, post_data[key][0], int(key[-1]))
            else:
                pass
        for cur_info in current_catalog_infos:
            if str(cur_info.catalog.uid).strip() in new_tag_arr:
                pass
            else:
                self.mapp2catalog.remove_relation(signature, cur_info.catalog)

    @tornado.web.authenticated
    def add_comment(self, id_post):
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)
        post_data['user_id'] = self.userinfo.uid
        post_data['user_name'] = self.userinfo.user_name
        comment_uid = self.mreply.insert_data(post_data, id_post)
        if comment_uid:
            output = {
                'pinglun': comment_uid,
            }
        else:
            output = {
                'pinglun': 0,
            }
        return json.dump(output, self)

    # def show_app(self, app_id):
    #     qian = self.get_secure_cookie('map_hist')
    #
    #     post_data = {}
    #     for key in self.request.arguments:
    #         post_data[key] = self.get_arguments(key)
    #
    #     if qian:
    #         qian = qian.decode('utf-8')
    #     else:
    #         qian = ''
    #     self.set_secure_cookie('map_hist', (app_id + qian)[:20])
    #     replys = self.mreply.get_by_id(app_id)
    #     rec = self.mapp.get_by_uid(app_id)
    #
    #     if rec == False:
    #         kwd = {
    #             'info': '您要找的云算应用不存在。',
    #         }
    #         self.render('html/404.html', kwd=kwd,
    #                     userinfo = self.userinfo,)
    #         return False
    #
    #     if 'zoom' in post_data:
    #         rec.zoom_current = post_data['zoom'][0]
    #     if 'lat' in post_data:
    #         rec.lat = post_data['lat'][0]
    #     if 'lon' in post_data:
    #         rec.lon = post_data['lon'][0]
    #
    #     if 'lng' in post_data:
    #         rec.lon = post_data['lng'][0]
    #
    #     last_map_id = self.get_secure_cookie('use_app_uid')
    #
    #     if last_map_id:
    #         last_map_id = last_map_id.decode('utf-8')
    #
    #     self.set_secure_cookie('use_app_uid', app_id)
    #
    #     if last_map_id and self.mapp.get_by_uid(last_map_id):
    #         self.add_relation(last_map_id, app_id)
    #
    #     cookie_str = tools.get_uuid()
    #     kwd = {
    #         'pager': '',
    #         'url': self.request.uri,
    #         'cookie_str': cookie_str,
    #         'marker': 1 if 'marker' in post_data  else 0,
    #         'geojson': post_data['gson'][0] if 'gson' in post_data else '',
    #         'signature': app_id,
    #         'tdesc': '',
    #         'eval_0': self.mevaluation.app_evaluation_count(app_id, 0),
    #         'eval_1': self.mevaluation.app_evaluation_count(app_id, 1),
    #         'site_url': config.site_url,
    #         'login': 1 if self.get_current_user() else 0,
    #
    #     }
    #
    #     self.mapp.view_count_increase(app_id)
    #
    #     if self.get_current_user():
    #         self.musage.add_or_update(self.userinfo.uid, app_id)
    #
    #         # json_recs = self.mjson.query_by_app(app_id, self.userinfo.uid)
    #         # layout_recs = self.mlayout.query_by_app(app_id, self.userinfo.uid)
    #         #
    #         # layout_links = []
    #         #
    #         # for layout_rec in layout_recs:
    #         #     out_link = '{0}?zoom={1}&lat={2}&lon={3}'.format(layout_rec.app.uid, layout_rec.zoom, layout_rec.lat,
    #         #                                                      layout_rec.lon)
    #         #     if layout_rec.marker != 0:
    #         #         out_link = out_link + '&marker=1'
    #         #     if layout_rec.json != '':
    #         #         out_link = out_link + '&gson={0}'.format(layout_rec.json)
    #         #     layout_links.append({'uid': layout_rec.uid, 'link': out_link})
    #
    #
    #     self.set_cookie('user_pass', cookie_str)
    #
    #     map_hist = []
    #     if self.get_secure_cookie('map_hist'):
    #         for xx in range(0, len(self.get_secure_cookie('map_hist').decode('utf-8')), 4):
    #             map_hist.append(self.get_secure_cookie('map_hist').decode('utf-8')[xx: xx + 4])
    #
    #     if 'fullscreen' in self.request.arguments:
    #         tmpl = 'tmpl_applite/app/full_screen.html'
    #         # if self.userinfo.privilege[4] == '1':
    #         #     tmpl = 'tmpl_applite/app/full_vip.html'
    #         # else:
    #         #     tmpl = 'tmpl_applite/app/full_screen.html'
    #     else:
    #
    #         tmpl = 'tmpl_applite/app/show_map.html'
    #
    #     rel_recs = self.mrel.get_app_relations(rec.uid, 4)
    #
    #     rand_recs = self.mapp.query_random(4 - rel_recs.count() + 2)
    #
    #     self.render(tmpl,
    #                 kwd=kwd,
    #                 calc_info=rec,
    #                 userinfo=self.userinfo,
    #                 relations=rel_recs,
    #                 rand_recs=rand_recs,
    #                 unescape=tornado.escape.xhtml_unescape,
    #                 ad_switch=random.randint(1, 18),
    #                 tag_info=self.mapp2tag.get_by_id(app_id),
    #                 recent_apps=self.musage.query_recent(self.get_current_user(), 6)[1:],
    #                 map_hist=map_hist,
    #                 # json_recs=json_recs,
    #                 # layout_links=layout_links,
    #                 replys=replys,
    #                 )

    def add_relation(self, f_uid, t_uid):
        if False == self.mapp.get_by_uid(t_uid):
            return False
        if f_uid == t_uid:
            '''
            关联其本身
            '''
            return False
        self.mrel.add_relation(f_uid, t_uid, 2)
        self.mrel.add_relation(t_uid, f_uid, 1)
        return True