<commit_before>from time import time

from plugin import Plugin, PluginDocument

from database import TableBase, Column, Text, Integer, Boolean
from database import engine, session
from .dbupload.dbprofile import Gene


class PanoDocument(TableBase, PluginDocument):

    __tablename__ = 'pano'

    def __init__(self, owner, title='Untitled', text=''):
        super().__init__()
        self.owner = owner
        self.title = title
        self.text = text
        self.description = 'not support description'
    text = Column(Text())
    created = Column(Integer())
    public = Column(Boolean())
    comments = Column(Text())
    praises = Column(Text())


class Pano(Plugin):
    def __init__(self):
        super().__init__()
        self.documents.set_document_type('pano')
        self.documents.set_document_table(PanoDocument)
        self.name = 'biobrick_manager'
        try:
            PanoDocument.__table__.create(engine)
        except:
            pass

    def process(self, request):
        return getattr(self, request['action'])(**request)

    def new(self, title, data, public, img, **kwargs):
        doc = PanoDocument(self.user.id, title, data)
        doc.description = img
        doc.created = time()
        doc.last_modified = time()
        public = 1 if public else 0
        doc.public = public
        doc.comments = '[]'
        doc.praises = 'set()'
        self.documents.create(doc)
        return dict(id=doc.id)

    def save(self, id, title, data, img, **kwargs):
        id = int(id)
        doc = self.documents.get(id)
        if not (doc and doc.owner == self.user.id):
            raise ValueError('Document %d does not exists.' % id)
        doc.title = title
        doc.text = data
        doc.description = img
        doc.last_modified = time()
        self.documents.update(doc)
        return {}

    def list(self, **kwargs):
        docs = self.documents.list(self.user.id)
        return dict(ids=list(map(lambda x:x.id, docs)))

    def load(self, id, **kwargs):
        id = int(id)
        doc = self.documents.get(id)
        if not (doc and (doc.owner == self.user.id or doc.public)):
            raise ValueError('Document %d does not exists.' % id)
        return dict(data=doc.text, title=doc.title, owner=doc.owner, ctime=doc.created, mtime=doc.last_modified,
                public=doc.public, comments=eval(doc.comments), praises=len(eval(doc.praises)), img=doc.description)

    def delete(self, id, **kwargs):
        id = int(id)
        doc = self.documents.get(id)
        if not (doc and doc.owner == self.user.id):
            raise ValueError('Document %d does not exists.' % id)
        self.documents.delete(doc)
        return {}

    def get_event_data(self, **kwargs):
        events = []
        for i in session.query(PanoDocument).filter(PanoDocument.created > time()-7*24*3600).all():
            if(i.public):
                events.append(dict(project_id=i.id, user_id=i.owner, time=i.created, img_src=i.description, state='Create',
                        project_name=i.title, last_update_time=i.last_modified, praise=self.user.id in eval(i.praises),
                        comment=eval(i.comments)))
        for i in session.query(PanoDocument).filter(PanoDocument.last_modified > time()-7*24*3600).all():
            if(i.public):
                events.append(dict(project_id=i.id, user_id=i.owner, time=i.created, img_src=i.description, state='Update',
                    project_name=i.title, last_update_time=i.last_modified, praise=self.user.id in eval(i.praises),
                    comment=eval(i.comments)))
        return dict(events=events)

    def submit_comment(self, comment, event_id, **kwargs):
        id = int(event_id)
        doc = self.documents.get(id)
        if not (doc and (doc.owner == self.user.id or doc.public)):
            raise ValueError('Document %d does not exists.' % id)
        comments = eval(doc.comments)
        comments.append(dict(user_id=self.user.id, content=comment, time=time()))
        doc.comments = repr(comments)
        self.documents.update(doc)
        return {}

    def submit_praise(self, modify, event_id, **kwargs):
        modify = modify=='true' if type(modify) is str else bool(modify)
        id = int(event_id)
        doc = self.documents.get(id)
        if not (doc and (doc.owner == self.user.id or doc.public)):
            raise ValueError('Document %d does not exists.' % id)
        praises = eval(doc.praises)
        praises.discard(self.user.id)
        if modify:
            praises.add(self.user.id)
        doc.praises = repr(praises)
        self.documents.update(doc)
        return {}

    def get_project_data(self, **kwargs):
        events = []
        for i in self.documents.list(self.user.id):
            events.append(dict(project_id=i.id, user_id=i.owner, time=i.created, img_src=i.description, public=i.public,
                project_name=i.title, last_update_time=i.last_modified, praise=self.user.id in eval(i.praises),
                comment=eval(i.comments)))
        return dict(project=events)

    def match_node(self, s, **kwargs):
        if len(s) < 3:
            return None

        suggest = {'nodes':[]}
        for res in session.query(Gene).filter(Gene.Symbol.like('%'+s+'%')).limit(100):
            temp = {'name': res.Symbol, 'gene_id': res.gene_id, 'info': res.description,
                    'tax_id': res.tax_id, 'equal': res.Symbol == s}
            suggest['nodes'].append(temp)

        sorted(suggest['nodes'], key=lambda x: x['equal'], reverse=True)

        for res in session.query(Gene).filter(Gene.gene_id == s):
            temp = {'name': res.Symbol, 'gene_id': res.gene_id, 'info': res.description,
                    'tax_id': res.tax_id, 'equal': res.Symbol == s}
            suggest['nodes'].insert(res, 0)
        
        return suggest


__plugin__ = Pano()
<commit_msg>Change uid searching<commit_after>from time import time

from plugin import Plugin, PluginDocument

from database import TableBase, Column, Text, Integer, Boolean
from database import engine, session
from .dbupload.dbprofile import Gene


class PanoDocument(TableBase, PluginDocument):

    __tablename__ = 'pano'

    def __init__(self, owner, title='Untitled', text=''):
        super().__init__()
        self.owner = owner
        self.title = title
        self.text = text
        self.description = 'not support description'
    text = Column(Text())
    created = Column(Integer())
    public = Column(Boolean())
    comments = Column(Text())
    praises = Column(Text())


class Pano(Plugin):
    def __init__(self):
        super().__init__()
        self.documents.set_document_type('pano')
        self.documents.set_document_table(PanoDocument)
        self.name = 'biobrick_manager'
        try:
            PanoDocument.__table__.create(engine)
        except:
            pass

    def process(self, request):
        return getattr(self, request['action'])(**request)

    def new(self, title, data, public, img, **kwargs):
        doc = PanoDocument(self.user.id, title, data)
        doc.description = img
        doc.created = time()
        doc.last_modified = time()
        public = 1 if public else 0
        doc.public = public
        doc.comments = '[]'
        doc.praises = 'set()'
        self.documents.create(doc)
        return dict(id=doc.id)

    def save(self, id, title, data, img, **kwargs):
        id = int(id)
        doc = self.documents.get(id)
        if not (doc and doc.owner == self.user.id):
            raise ValueError('Document %d does not exists.' % id)
        doc.title = title
        doc.text = data
        doc.description = img
        doc.last_modified = time()
        self.documents.update(doc)
        return {}

    def list(self, **kwargs):
        docs = self.documents.list(self.user.id)
        return dict(ids=list(map(lambda x:x.id, docs)))

    def load(self, id, **kwargs):
        id = int(id)
        doc = self.documents.get(id)
        if not (doc and (doc.owner == self.user.id or doc.public)):
            raise ValueError('Document %d does not exists.' % id)
        return dict(data=doc.text, title=doc.title, owner=doc.owner, ctime=doc.created, mtime=doc.last_modified,
                public=doc.public, comments=eval(doc.comments), praises=len(eval(doc.praises)), img=doc.description)

    def delete(self, id, **kwargs):
        id = int(id)
        doc = self.documents.get(id)
        if not (doc and doc.owner == self.user.id):
            raise ValueError('Document %d does not exists.' % id)
        self.documents.delete(doc)
        return {}

    def get_event_data(self, **kwargs):
        events = []
        for i in session.query(PanoDocument).filter(PanoDocument.created > time()-7*24*3600).all():
            if(i.public):
                events.append(dict(project_id=i.id, user_id=i.owner, time=i.created, img_src=i.description, state='Create',
                        project_name=i.title, last_update_time=i.last_modified, praise=self.user.id in eval(i.praises),
                        comment=eval(i.comments)))
        for i in session.query(PanoDocument).filter(PanoDocument.last_modified > time()-7*24*3600).all():
            if(i.public):
                events.append(dict(project_id=i.id, user_id=i.owner, time=i.created, img_src=i.description, state='Update',
                    project_name=i.title, last_update_time=i.last_modified, praise=self.user.id in eval(i.praises),
                    comment=eval(i.comments)))
        return dict(events=events)

    def submit_comment(self, comment, event_id, **kwargs):
        id = int(event_id)
        doc = self.documents.get(id)
        if not (doc and (doc.owner == self.user.id or doc.public)):
            raise ValueError('Document %d does not exists.' % id)
        comments = eval(doc.comments)
        comments.append(dict(user_id=self.user.id, content=comment, time=time()))
        doc.comments = repr(comments)
        self.documents.update(doc)
        return {}

    def submit_praise(self, modify, event_id, **kwargs):
        modify = modify=='true' if type(modify) is str else bool(modify)
        id = int(event_id)
        doc = self.documents.get(id)
        if not (doc and (doc.owner == self.user.id or doc.public)):
            raise ValueError('Document %d does not exists.' % id)
        praises = eval(doc.praises)
        praises.discard(self.user.id)
        if modify:
            praises.add(self.user.id)
        doc.praises = repr(praises)
        self.documents.update(doc)
        return {}

    def get_project_data(self, **kwargs):
        events = []
        for i in self.documents.list(self.user.id):
            events.append(dict(project_id=i.id, user_id=i.owner, time=i.created, img_src=i.description, public=i.public,
                project_name=i.title, last_update_time=i.last_modified, praise=self.user.id in eval(i.praises),
                comment=eval(i.comments)))
        return dict(project=events)

    def match_node(self, s, **kwargs):

        suggest = {'nodes':[]}

        for res in session.query(Gene).filter(Gene.gene_id == s):
            print("There there")
            temp = {'name': res.Symbol, 'gene_id': res.gene_id, 'info': res.description,
                    'tax_id': res.tax_id, 'equal': res.Symbol == s}
            suggest['nodes'].append(temp)
            return suggest

        if len(s) < 3:
            return None

        for res in session.query(Gene).filter(Gene.Symbol.like('%'+s+'%')).limit(100):
            temp = {'name': res.Symbol, 'gene_id': res.gene_id, 'info': res.description,
                    'tax_id': res.tax_id, 'equal': res.Symbol == s}
            suggest['nodes'].append(temp)

        sorted(suggest['nodes'], key=lambda x: x['equal'], reverse=True)

        return suggest


__plugin__ = Pano()

{'fraction_non_alphanumeric': 0.13482105263157895, 'fraction_numerical': 0.0034526315789473686, 'mean_word_length': 2.414606095457159, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '2797218', 'n_tokens_mistral': 3724, 'n_tokens_neox': 3599, 'n_words': 801}