Ejemplo n.º 1
0
 def render(self, limit):
     self.mcat = MAppCatalog()
     all_cats = self.mcat.query_field_count(limit)
     kwd = {
         'cats': all_cats,
     }
     return self.render_string('app_theme/modules/app_menu.html', kwd=kwd)
Ejemplo n.º 2
0
    def initialize(self):
        self.muser = MUser()

        self.mequa = MApp()
        self.mtag = MAppCatalog()
        self.mapp2tag = MApp2Catalog()
        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None
Ejemplo n.º 3
0
    def initialize(self):
        self.mevaluation = MEvaluation()
        self.mapp2catalog = MApp2Catalog()
        self.mapp2tag = MApp2Label()
        self.mapp = MApp()
        self.musage = MUsage()
        self.mtag = MAppCatalog()
        self.muser = MUser()
        self.mrel = MAppRel()
        self.mreply = MApp2Reply()

        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None
Ejemplo n.º 4
0
 def render(self, limit):
     self.mcat = MAppCatalog()
     all_cats = self.mcat.query_field_count(limit)
     kwd = {
         'cats': all_cats,
     }
     return self.render_string('app_theme/modules/app_menu.html', kwd=kwd)
Ejemplo n.º 5
0
    def initialize(self):
        self.muser = MUser()

        self.mequa = MApp()
        self.mtag = MAppCatalog()
        self.mapp2tag = MApp2Catalog()
        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None
Ejemplo n.º 6
0
    def initialize(self):
        self.mevaluation = MEvaluation()
        self.mapp2catalog = MApp2Catalog()
        self.mapp2tag = MApp2Label()
        self.mapp = MApp()
        self.musage = MUsage()
        self.mtag = MAppCatalog()
        self.muser = MUser()
        self.mrel = MAppRel()
        self.mreply = MApp2Reply()

        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None
Ejemplo n.º 7
0
class TagHandler(BaseHandler):
    def initialize(self):
        self.muser = MUser()

        self.mequa = MApp()
        self.mtag = MAppCatalog()
        self.mapp2tag = MApp2Catalog()
        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

    def get(self, url_str=''):
        if len(url_str) > 0:
            url_arr = url_str.split(r'/')

        if len(url_arr) == 1:
            self.list(url_str)
        elif len(url_arr) == 2:
            self.list(url_arr[0], url_arr[1])

    def post(self, url_str=''):
        if len(url_str) > 0:
            url_arr = url_str.split('/')
        if url_arr[0] == 'edit':
            self.edit(url_arr[1])

    def list(self, tag_slug, cur_p=''):
        '''
        根据 cat_handler.py 中的 def view_cat_new(self, cat_slug, cur_p = '')
        :param tag_slug:
        :return:
        '''
        if cur_p == '' or cur_p == '-1':
            current_page_number = 1
        else:
            current_page_number = int(cur_p)
        taginfo = self.mtag.get_by_slug(tag_slug)
        num_of_tag = self.mapp2tag.catalog_record_number(taginfo.uid)
        page_num = int(num_of_tag / config.page_num) + 1
        tag_name = taginfo.name

        kwd = {
            'tag_name': tag_name,
            'tag_slug': tag_slug,

            'title': tag_name,
        }

        # infos = self.mapp2tag.query_by_slug(tag_slug)
        self.render('app_theme/tag/list.html',
                    tag_name=tag_name,
                    infos=self.mapp2tag.query_pager_by_slug(tag_slug, current_page_number),
                    unescape=tornado.escape.xhtml_unescape,
                    kwd=kwd,
                    pager=self.gen_pager_bootstrap(tag_slug, page_num, current_page_number),
                    userinfo=self.userinfo,
                    )

    def gen_pager_bootstrap(self, cat_slug, page_num, current):
        # cat_slug 分类
        # page_num 页面总数
        # current 当前页面
        if page_num == 1:
            return ''

        pager_shouye = '''
        <li class=" {0}">
        <a  href="/tag/{1}">&lt;&lt; 首页</a>
                    </li>'''.format('hidden' if current <= 1 else '', cat_slug)

        pager_pre = '''
                    <li class=" {0}">
                    <a  href="/tag/{1}/{2}">&lt; 前页</a>
                    </li>
                    '''.format('hidden' if current <= 1 else '', cat_slug, current - 1)
        pager_mid = ''
        for ind in range(0, page_num):
            tmp_mid = '''
                    <li class="{0}">
                    <a  href="/tag/{1}/{2}">{2}</a></li>
                    '''.format('active' if ind + 1 == current else '', cat_slug, ind + 1)
            pager_mid += tmp_mid
        pager_next = '''
                    <li class=" {0}">
                    <a  href="/tag/{1}/{2}">后页 &gt;</a>
                    </li>
                    '''.format('hidden' if current >= page_num else '', cat_slug, current + 1)
        pager_last = '''
                    <li class=" {0}">
                    <a  href="/tag/{1}/{2}">末页
                        &gt;&gt;</a>
                    </li>
                    '''.format('hidden' if current >= page_num else '', cat_slug, page_num)
        pager = pager_shouye + pager_pre + pager_mid + pager_next + pager_last
        return (pager)

    def gen_pager(self, cat_slug, page_num, current):
        # cat_slug 分类
        # page_num 页面总数
        # current 当前页面
        if page_num == 1:
            return ''

        pager_shouye = '''
        <li class="pure-menu-item first {0}">
        <a class="pure-menu-link" href="/tag/{1}">&lt;&lt; 首页</a>
                    </li>'''.format('hidden' if current <= 1 else '', cat_slug)

        pager_pre = '''
                    <li class="pure-menu-item previous {0}">
                    <a class="pure-menu-link" href="/tag/{1}/{2}">&lt; 前页</a>
                    </li>
                    '''.format('hidden' if current <= 1 else '', cat_slug, current - 1)
        pager_mid = ''
        for ind in range(0, page_num):
            tmp_mid = '''
                    <li class="pure-menu-item page {0}">
                    <a class="pure-menu-link" href="/tag/{1}/{2}">{2}</a></li>
                    '''.format('selected' if ind + 1 == current else '', cat_slug, ind + 1)
            pager_mid += tmp_mid
        pager_next = '''
                    <li class="pure-menu-item next {0}">
                    <a class="pure-menu-link" href="/tag/{1}/{2}">后页 &gt;</a>
                    </li>
                    '''.format('hidden' if current >= page_num else '', cat_slug, current + 1)
        pager_last = '''
                    <li class="pure-menu-item last {0}">
                    <a class="pure-menu-link" href="/tag/{1}/{2}">末页
                        &gt;&gt;</a>
                    </li>
                    '''.format('hidden' if current >= page_num else '', cat_slug, page_num)
        pager = pager_shouye + pager_pre + pager_mid + pager_next + pager_last
        return (pager)
Ejemplo n.º 8
0
class AppHandler(BaseHandler):
    def initialize(self):
        self.mevaluation = MEvaluation()
        self.mapp2catalog = MApp2Catalog()
        self.mapp2tag = MApp2Label()
        self.mapp = MApp()
        self.musage = MUsage()
        self.mtag = MAppCatalog()
        self.muser = MUser()
        self.mrel = MAppRel()
        self.mreply = MApp2Reply()

        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

    def get(self, url_str=''):
        if len(url_str) > 0:
            url_arr = url_str.split('/')
        else:
            return False
        if len(url_arr) == 1 and len(url_str) == 4:
            self.show_app(url_str)
        elif len(url_arr) == 2:
            if url_arr[0] == 'edit':
                self.to_edit_description(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)

    def post(self, url_str=''):

        if len(url_str) > 0:
            ip_arr = url_str.split('/')
        else:
            return False
        if ip_arr[0] == 'rel':
            if self.get_current_user():
                self.add_relation(ip_arr[1])
            else:
                self.redirect('/user/login')
        elif ip_arr[0] == 'comment_add':

            self.add_comment(ip_arr[1])
        elif ip_arr[0] == 'edit':
            self.update()

    @tornado.web.authenticated
    def to_edit_description(self, app_id):
        if self.userinfo.privilege[4] == '1':
            info = self.mapp.get_by_uid(app_id)
            self.render(
                'app_theme/app/edit.html',
                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):
        if self.userinfo.privilege[4] == '1':
            post_data = {}
            for key in self.request.arguments:
                post_data[key] = self.get_arguments(key)
            self.mapp.modify_meta(post_data)
            self.update_catalog(post_data['uid'][0])
            self.update_tag(post_data['uid'][0])
            self.redirect('/{0}/{1}'.format(config.app_url_name,
                                            post_data['uid'][0]))
        else:
            return False

    @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 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]))
        for cur_info in current_catalog_infos:
            if str(cur_info.catalog.uid) 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):
        '''
        打开App.
        '''

        qian = self.get_secure_cookie('map_hist')

        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)
            return False
        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': rec.html_path,
            'cookie_str': cookie_str,
            'signature': app_id,
            'tkw': '在线计算,在线计算器',
            'tdesc': '',
            'eval_0': self.mevaluation.app_evaluation_count(app_id, 0),
            'eval_1': self.mevaluation.app_evaluation_count(app_id, 1),
        }
        self.mapp.view_count_increase(app_id)
        if self.get_current_user():
            self.musage.add_or_update(self.userinfo.uid, app_id)

        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 = 'app_theme/app/full_screen.html'
        else:

            tmpl = 'app_theme/app/show_map.html'
        self.render(
            tmpl,
            kwd=kwd,
            calc_info=rec,
            userinfo=self.userinfo,
            relations=self.mrel.get_app_relations(rec.uid),
            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,
            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
Ejemplo n.º 9
0
class AppHandler(BaseHandler):
    def initialize(self):
        self.mevaluation = MEvaluation()
        self.mapp2catalog = MApp2Catalog()
        self.mapp2tag = MApp2Label()
        self.mapp = MApp()
        self.musage = MUsage()
        self.mtag = MAppCatalog()
        self.muser = MUser()
        self.mrel = MAppRel()
        self.mreply = MApp2Reply()

        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

    def get(self, url_str=""):
        if len(url_str) > 0:
            url_arr = url_str.split("/")
        else:
            return False
        if len(url_arr) == 1 and len(url_str) == 4:
            self.show_app(url_str)
        elif len(url_arr) == 2:
            if url_arr[0] == "edit":
                self.to_edit_description(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)

    def post(self, url_str=""):

        if len(url_str) > 0:
            ip_arr = url_str.split("/")
        else:
            return False
        if ip_arr[0] == "rel":
            if self.get_current_user():
                self.add_relation(ip_arr[1])
            else:
                self.redirect("/user/login")
        elif ip_arr[0] == "comment_add":

            self.add_comment(ip_arr[1])
        elif ip_arr[0] == "edit":
            self.update()

    @tornado.web.authenticated
    def to_edit_description(self, app_id):
        if self.userinfo.privilege[4] == "1":
            info = self.mapp.get_by_uid(app_id)
            self.render(
                "app_theme/app/edit.html",
                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):
        if self.userinfo.privilege[4] == "1":
            post_data = {}
            for key in self.request.arguments:
                post_data[key] = self.get_arguments(key)
            self.mapp.modify_meta(post_data)
            self.update_catalog(post_data["uid"][0])
            self.update_tag(post_data["uid"][0])
            self.redirect("/{0}/{1}".format(config.app_url_name, post_data["uid"][0]))
        else:
            return False

    @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 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]))
        for cur_info in current_catalog_infos:
            if str(cur_info.catalog.uid) 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):
        """
        打开App.
        """

        qian = self.get_secure_cookie("map_hist")

        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)
            return False
        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": rec.html_path,
            "cookie_str": cookie_str,
            "signature": app_id,
            "tkw": "在线计算,在线计算器",
            "tdesc": "",
            "eval_0": self.mevaluation.app_evaluation_count(app_id, 0),
            "eval_1": self.mevaluation.app_evaluation_count(app_id, 1),
        }
        self.mapp.view_count_increase(app_id)
        if self.get_current_user():
            self.musage.add_or_update(self.userinfo.uid, app_id)

        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 = "app_theme/app/full_screen.html"
        else:

            tmpl = "app_theme/app/show_map.html"
        self.render(
            tmpl,
            kwd=kwd,
            calc_info=rec,
            userinfo=self.userinfo,
            relations=self.mrel.get_app_relations(rec.uid),
            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,
            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
Ejemplo n.º 10
0
class TagHandler(BaseHandler):
    def initialize(self):
        self.muser = MUser()

        self.mequa = MApp()
        self.mtag = MAppCatalog()
        self.mapp2tag = MApp2Catalog()
        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

    def get(self, url_str=''):
        if len(url_str) > 0:
            url_arr = url_str.split(r'/')

        if len(url_arr) == 1:
            self.list(url_str)
        elif len(url_arr) == 2:
            self.list(url_arr[0], url_arr[1])

    def post(self, url_str=''):
        if len(url_str) > 0:
            url_arr = url_str.split('/')
        if url_arr[0] == 'edit':
            self.edit(url_arr[1])

    def list(self, tag_slug, cur_p=''):
        '''
        根据 cat_handler.py 中的 def view_cat_new(self, cat_slug, cur_p = '')
        :param tag_slug:
        :return:
        '''
        if cur_p == '' or cur_p == '-1':
            current_page_number = 1
        else:
            current_page_number = int(cur_p)
        taginfo = self.mtag.get_by_slug(tag_slug)
        num_of_tag = self.mapp2tag.catalog_record_number(taginfo.uid)
        page_num = int(num_of_tag / config.page_num) + 1
        tag_name = taginfo.name

        kwd = {
            'tag_name': tag_name,
            'tag_slug': tag_slug,

            'title': tag_name,
        }

        # infos = self.mapp2tag.query_by_slug(tag_slug)
        self.render('app_theme/tag/list.html',
                    tag_name=tag_name,
                    infos=self.mapp2tag.query_pager_by_slug(tag_slug, current_page_number),
                    unescape=tornado.escape.xhtml_unescape,
                    kwd=kwd,
                    pager=self.gen_pager_bootstrap(tag_slug, page_num, current_page_number),
                    userinfo=self.userinfo,
                    )

    def gen_pager_bootstrap(self, cat_slug, page_num, current):
        # cat_slug 分类
        # page_num 页面总数
        # current 当前页面
        if page_num == 1:
            return ''

        pager_shouye = '''
        <li class=" {0}">
        <a  href="/tag/{1}">&lt;&lt; 首页</a>
                    </li>'''.format('hidden' if current <= 1 else '', cat_slug)

        pager_pre = '''
                    <li class=" {0}">
                    <a  href="/tag/{1}/{2}">&lt; 前页</a>
                    </li>
                    '''.format('hidden' if current <= 1 else '', cat_slug, current - 1)
        pager_mid = ''
        for ind in range(0, page_num):
            tmp_mid = '''
                    <li class="{0}">
                    <a  href="/tag/{1}/{2}">{2}</a></li>
                    '''.format('active' if ind + 1 == current else '', cat_slug, ind + 1)
            pager_mid += tmp_mid
        pager_next = '''
                    <li class=" {0}">
                    <a  href="/tag/{1}/{2}">后页 &gt;</a>
                    </li>
                    '''.format('hidden' if current >= page_num else '', cat_slug, current + 1)
        pager_last = '''
                    <li class=" {0}">
                    <a  href="/tag/{1}/{2}">末页
                        &gt;&gt;</a>
                    </li>
                    '''.format('hidden' if current >= page_num else '', cat_slug, page_num)
        pager = pager_shouye + pager_pre + pager_mid + pager_next + pager_last
        return (pager)

    def gen_pager(self, cat_slug, page_num, current):
        # cat_slug 分类
        # page_num 页面总数
        # current 当前页面
        if page_num == 1:
            return ''

        pager_shouye = '''
        <li class="pure-menu-item first {0}">
        <a class="pure-menu-link" href="/tag/{1}">&lt;&lt; 首页</a>
                    </li>'''.format('hidden' if current <= 1 else '', cat_slug)

        pager_pre = '''
                    <li class="pure-menu-item previous {0}">
                    <a class="pure-menu-link" href="/tag/{1}/{2}">&lt; 前页</a>
                    </li>
                    '''.format('hidden' if current <= 1 else '', cat_slug, current - 1)
        pager_mid = ''
        for ind in range(0, page_num):
            tmp_mid = '''
                    <li class="pure-menu-item page {0}">
                    <a class="pure-menu-link" href="/tag/{1}/{2}">{2}</a></li>
                    '''.format('selected' if ind + 1 == current else '', cat_slug, ind + 1)
            pager_mid += tmp_mid
        pager_next = '''
                    <li class="pure-menu-item next {0}">
                    <a class="pure-menu-link" href="/tag/{1}/{2}">后页 &gt;</a>
                    </li>
                    '''.format('hidden' if current >= page_num else '', cat_slug, current + 1)
        pager_last = '''
                    <li class="pure-menu-item last {0}">
                    <a class="pure-menu-link" href="/tag/{1}/{2}">末页
                        &gt;&gt;</a>
                    </li>
                    '''.format('hidden' if current >= page_num else '', cat_slug, page_num)
        pager = pager_shouye + pager_pre + pager_mid + pager_next + pager_last
        return (pager)
Ejemplo n.º 11
0
class AppHandler(BaseHandler):
    def initialize(self):
        self.mevaluation = MEvaluation()
        self.mapp2catalog = MApp2Catalog()
        self.mapp2tag = MApp2Label()
        self.mapp = MApp()
        self.musage = MUsage()
        self.mtag = MAppCatalog()
        self.muser = MUser()
        self.mrel = MAppRel()
        self.mreply = MApp2Reply()
        self.mjson = MJson()
        self.mlayout = MLayout()

        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

    def get(self, url_str=''):
        if len(url_str) > 0:
            url_arr = self.parse_url(url_str)
        else:
            return False
        if len(url_arr) == 1 and len(url_str) == 4:
            self.show_app(url_str)
        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)

    def post(self, url_str=''):
        if len(url_str) > 0:
            url_arr = self.parse_url(url_str)
        else:
            return False
        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])
        elif url_arr[0] == 'edit':
            self.update()
        elif url_arr[0] == 'add':
            self.add(url_arr[1])
        else:
            return False

    @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('app_theme/app/add.html',
                    tag_infos=self.mtag.query_all(),
                    kwd={
                        'uid': uid,
                    }
                    )

    @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('app_theme/app/edit.html', 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):
        if self.userinfo.privilege[4] == '1':
            post_data = {}
            for key in self.request.arguments:
                post_data[key] = self.get_arguments(key)
            self.mapp.modify_meta(post_data)
            self.update_catalog(post_data['uid'][0])
            self.update_tag(post_data['uid'][0])
            self.redirect('/{0}/{1}'.format(config.app_url_name, post_data['uid'][0]))
        else:
            return False

    @tornado.web.authenticated
    def add(self):
        if self.userinfo.privilege[4] == '1':
            post_data = {}
            for key in self.request.arguments:
                post_data[key] = self.get_arguments(key)
            self.mapp.modify_meta(post_data)
            self.update_catalog(post_data['uid'][0])
            self.update_tag(post_data['uid'][0])
            self.redirect('/{0}/{1}'.format(config.app_url_name, post_data['uid'][0]))
        else:
            return False


    @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 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]))
        for cur_info in current_catalog_infos:
            if str(cur_info.catalog.uid) 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)
            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})
        else:
            json_recs = []
            layout_links = []

        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 = 'app_theme/app/full_screen.html'
            # if self.userinfo.privilege[4] == '1':
            #     tmpl = 'app_theme/app/full_vip.html'
            # else:
            #     tmpl = 'app_theme/app/full_screen.html'
        else:

            tmpl = 'app_theme/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
Ejemplo n.º 12
0
class AppHandler(BaseHandler):
    def initialize(self):
        self.mevaluation = MEvaluation()
        self.mapp2catalog = MApp2Catalog()
        self.mapp2tag = MApp2Label()
        self.mapp = MApp()
        self.musage = MUsage()
        self.mtag = MAppCatalog()
        self.muser = MUser()
        self.mrel = MAppRel()
        self.mreply = MApp2Reply()
        self.mjson = MJson()
        self.mlayout = MLayout()

        if self.get_current_user():
            self.userinfo = self.muser.get_by_id(self.get_current_user())
        else:
            self.userinfo = None

    def get(self, url_str=''):
        if len(url_str) > 0:
            url_arr = self.parse_url(url_str)
        else:
            return False
        if len(url_arr) == 1 and len(url_str) == 4:
            self.show_app(url_str)
        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)

    def post(self, url_str=''):
        if len(url_str) > 0:
            url_arr = self.parse_url(url_str)
        else:
            return False
        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])
        elif url_arr[0] == 'edit':
            self.update()
        elif url_arr[0] == 'add':
            self.add(url_arr[1])
        else:
            return False

    @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('app_theme/app/add.html',
                        tag_infos=self.mtag.query_all(),
                        kwd={
                            'uid': uid,
                        })

    @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(
                'app_theme/app/edit.html',
                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):
        if self.userinfo.privilege[4] == '1':
            post_data = {}
            for key in self.request.arguments:
                post_data[key] = self.get_arguments(key)
            self.mapp.modify_meta(post_data)
            self.update_catalog(post_data['uid'][0])
            self.update_tag(post_data['uid'][0])
            self.redirect('/{0}/{1}'.format(config.app_url_name,
                                            post_data['uid'][0]))
        else:
            return False

    @tornado.web.authenticated
    def add(self):
        if self.userinfo.privilege[4] == '1':
            post_data = {}
            for key in self.request.arguments:
                post_data[key] = self.get_arguments(key)
            self.mapp.modify_meta(post_data)
            self.update_catalog(post_data['uid'][0])
            self.update_tag(post_data['uid'][0])
            self.redirect('/{0}/{1}'.format(config.app_url_name,
                                            post_data['uid'][0]))
        else:
            return False

    @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 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]))
        for cur_info in current_catalog_infos:
            if str(cur_info.catalog.uid) 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)
            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})
        else:
            json_recs = []
            layout_links = []

        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 = 'app_theme/app/full_screen.html'
            # if self.userinfo.privilege[4] == '1':
            #     tmpl = 'app_theme/app/full_vip.html'
            # else:
            #     tmpl = 'app_theme/app/full_screen.html'
        else:

            tmpl = 'app_theme/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