<commit_before>#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import cherrypy
import urllib.parse

from PressUI.cherrypy.PressApp import PressApp
from PressUI.cherrypy.PressConfig import PressConfig
from PressUI.cherrypy.server import quickstart
from PressUI.utils.browser_cache import add_cache_control_header
from model.Bookmark import Bookmark
import PressUI.API.FB.login as FBlogin
import model.Parse

def safe_access(fn):

    @cherrypy.expose
    def wrapped(*args, **kwargs):
        try:
            FBlogin.cherrypy_authenticate(
                PressConfig.get('fb_app_id'),
                PressConfig.get('fb_app_secret'),
            )
        except:
            raise Exception('Access denied')

        allowed_ids = PressConfig.get('fb_allowed_user_ids')
        if cherrypy.request.fb_user_id not in allowed_ids:
            raise Exception('Access denied')

        return fn(*args, **kwargs)

    return wrapped

class LinkMarks(PressApp):
    def _js_sources(self):
        return [
            'components/Bookmark.js',
            'components/BookmarkList.js',
            'components/BookmarkEdit.js',
            'controller/index.js',
            'controller/login.js',
            'controller/show_all.js',
            'controller/add_engine.js',
            'controller/search.js',
            'controller/new.js',
            'controller/edit.js',
            'controller/main.js',
        ]

    @cherrypy.tools.allow(methods = ['GET'])
    @cherrypy.expose
    def fb_login_info_json(self):
        ret = self._json({
            'app_id': PressConfig.get('fb_app_id'),
            'hostname': cherrypy.request.base,
        }, no_cache = False)
        add_cache_control_header(years = 1)
        return ret

    @cherrypy.tools.allow(methods = ["GET"])
    @safe_access
    def show_all_json(self):
        bookmarks = Bookmark.all()
        ret = self._json(list(map(lambda b: b.to_json(), bookmarks)))
        add_cache_control_header(days = 1)
        return ret

    def __search_common(self, query, key_transform):
        query = urllib.parse.unquote_plus(query)
        if len(query.strip()) == 0:
            return {'keyword': False, 'bookmarks': []}
        keyword = query.split()[0]

        key_bookmark_p = Bookmark.gen_find_keyword(keyword)
        bookmarks_p = Bookmark.gen_find_all(query)

        key_bookmark = key_bookmark_p.prep()
        if key_bookmark is not None:
            term = urllib.parse.quote_plus(query[len(keyword) + 1:])
            key_ret = key_transform(key_bookmark, keyword, term)
            if key_ret is not None:
                return {'keyword': True, 'ret': key_ret}

        bookmarks = bookmarks_p.prep()
        return {'keyword': False, 'bookmarks': bookmarks}<commit_msg>Fix params<commit_after>#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import cherrypy
import urllib.parse

from PressUI.cherrypy.PressApp import PressApp
from PressUI.cherrypy.PressConfig import PressConfig
from PressUI.cherrypy.server import quickstart
from PressUI.utils.browser_cache import add_cache_control_header
from model.Bookmark import Bookmark
import PressUI.API.FB.login as FBlogin
import model.Parse

def safe_access(fn):

    @cherrypy.expose
    def wrapped(*args, **kwargs):
        try:
            FBlogin.cherrypy_authenticate(
                PressConfig.get('fb_app_id'),
                PressConfig.get('fb_app_secret'),
            )
        except:
            raise Exception('Access denied')

        allowed_ids = PressConfig.get('fb_allowed_user_ids')
        if cherrypy.request.fb_user_id not in allowed_ids:
            raise Exception('Access denied')

        return fn(*args, **kwargs)

    return wrapped

class LinkMarks(PressApp):
    def _js_sources(self):
        return [
            'components/Bookmark.js',
            'components/BookmarkList.js',
            'components/BookmarkEdit.js',
            'controller/index.js',
            'controller/login.js',
            'controller/show_all.js',
            'controller/add_engine.js',
            'controller/search.js',
            'controller/new.js',
            'controller/edit.js',
            'controller/main.js',
        ]

    @cherrypy.tools.allow(methods = ['GET'])
    @cherrypy.expose
    def fb_login_info_json(self):
        ret = self._json({
            'app_id': PressConfig.get('fb_app_id'),
            'hostname': cherrypy.request.base,
        })
        add_cache_control_header(years = 1)
        return ret

    @cherrypy.tools.allow(methods = ["GET"])
    @safe_access
    def show_all_json(self):
        bookmarks = Bookmark.all()
        ret = self._json(list(map(lambda b: b.to_json(), bookmarks)))
        add_cache_control_header(days = 1)
        return ret

    def __search_common(self, query, key_transform):
        query = urllib.parse.unquote_plus(query)
        if len(query.strip()) == 0:
            return {'keyword': False, 'bookmarks': []}
        keyword = query.split()[0]

        key_bookmark_p = Bookmark.gen_find_keyword(keyword)
        bookmarks_p = Bookmark.gen_find_all(query)

        key_bookmark = key_bookmark_p.prep()
        if key_bookmark is not None:
            term = urllib.parse.quote_plus(query[len(keyword) + 1:])
            key_ret = key_transform(key_bookmark, keyword, term)
            if key_ret is not None:
                return {'keyword': True, 'ret': key_ret}

        bookmarks = bookmarks_p.prep()
        return {'keyword': False, 'bookmarks': bookmarks}
{'fraction_non_alphanumeric': 0.11165666484815422, 'fraction_numerical': 0.002545917439534461, 'mean_word_length': 2.392967304133251, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3455880', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1640, 'n_words': 322}