Exemplo n.º 1
0
def create_molecules(user_ids):
    molecule_sofie_id = {}

    for x in xrange(1, 30):
        res = requests.get('http://www.sofienetwork.com/probe/%i/' % x, \
                           headers={'Accept': 'application/json', 'Authorization': authorization})
        try:
            molecule_json = res.json()
            spn_id = molecule_json["ID"]
            del molecule_json['ID']
            user_id = molecule_json['UserID']
            molecule_json['UserID'] = user_ids[user_id] if user_id in user_ids else None
            molecule = Molecule(**molecule_json)
            res = requests.get('http://www.sofienetwork.com/probe/%i/image/' % x, headers={'Authorization': authorization})
            molecule._Image = res.content
            molecule.save()
            print molecule.ID
            molecule_sofie_id[spn_id] = molecule.ID

            res = requests.get('http://www.sofienetwork.com/keyword/Molecules/%i/' % x, headers={'Authorization': authorization})
            keywords = res.json()
            for keyword in keywords:
                del keyword['CreationDate']
                del keyword['KeywordID']
                key = Keyword(**keyword)
                key.ParentID = molecule.ID
                key.save()
        except Exception as e:
            print str(e)
            print 'molecule insertion error'
            db.session.rollback()
    return molecule_sofie_id
Exemplo n.º 2
0
    def post(self):
        args = keyword_post_parser.parse_args()

        keyword = Keyword(**args)
        try:
            keyword.add()
        except IntegrityError:
            return max_res('', code=401, errmsg='名称重复')

        return max_res(marshal(keyword, keyword_fields))
Exemplo n.º 3
0
 def setUp(self):
     self.controller = Controller()
     self.cache = self.controller.cache
     self.address = 'ffafaf'
     coll_address = str(uuid.uuid1())
     doc_hash_1 = str(uuid.uuid1())
     doc_hash_2 = str(uuid.uuid1())
     doc_hash_3 = str(uuid.uuid1())
     self.test_collection_evil = Collection(
         title="Test multiple33333",
         description="This is a collection! with multiple docs222",
         address=self.address,
         btc="123456789",
         keywords=[
             Keyword(name="Keyword A", id=1199),
             Keyword(name="Keyword c", id=1214),
         ],
         documents=[
             Document(
                 description="Test document Z",
                 hash="zzzzzzzz",
                 title="Test Z",
                 accesses=0,
                 filename="joe.txt",
                 collection_address="BM-2cSrapXpgDTFD8AyDmU1BGifNkB2Z6X9k8"
             ),
             Document(
                 description="Test document B",
                 hash='gdssgsdg',
                 title="Test B",
                 accesses=3,
                 filename="gile.txt",
                 collection_address="BM-2cSrapXpgDTFD8AyDmU1BGifNkB2Z6X9k8"
             ),
             Document(
                 description="Test document Bddd",
                 hash='afff',
                 title="Test B",
                 accesses=3,
                 filename="gile.txt",
                 collection_address="BM-2cSrapXpgDTFD8AyDmU1BGifNkB2Z6X9k8"
             ),
         ],
         creation_date=datetime.datetime.now(),
         oldest_date=datetime.datetime.now(),
         latest_broadcast_date=datetime.datetime.now(),
         latest_btc_tx="btctx1",
         oldest_btc_tx="btctx12",
         accesses=2,
         votes=3,
         votes_last_checked=datetime.datetime.now())
Exemplo n.º 4
0
    def _build_docs_keywords(self, payload, collection):
        """
        Builds a list of Keyword objects and a list of Document objects from the received json.

        :param payload: The payload of the FJ Message including the documents and keywords
        :return: Two lists representing the documents and keywords of the FJ Message
        """
        for key in payload["keywords"]:
            db_key = self.cache.get_keyword_by_id(key["id"])
            if db_key is not None:
                collection.keywords.append(db_key)
            else:
                collection.keywords.append(Keyword(name=key["name"]))

        for doc in payload["documents"]:
            db_doc = self.cache.get_document_by_hash(doc["hash"])
            if db_doc is not None:
                collection.documents.append(db_doc)
            else:
                collection.documents.append(
                    Document(collection_address=doc["address"],
                             description=doc["description"],
                             hash=doc["hash"],
                             title=doc["title"],
                             filename=doc["filename"],
                             accesses=doc["accesses"]))
Exemplo n.º 5
0
    def setUp(self):
        self.controller = Controller()
        self.address = self.controller.connection.create_address('Controller Test address', True)

        coll_address = str(uuid.uuid1())
        doc_hash_1 = str(uuid.uuid1())
        doc_hash_2 = str(uuid.uuid1())
        doc_hash_3 = str(uuid.uuid1())

        self.test_collection = Collection(
            title="Test",
            description="This is a collection!",
            address=self.address,
            btc="123456789",
            keywords=[
                Keyword(name="Keyword A"),
                Keyword(name="Keyword B"),
            ],
            documents=[
                Document(
                    description="Test document A",
                    hash=doc_hash_1,
                    title="Test A",
                    accesses=0,
                    filename="joe.txt",
                    collection_address="afgagahhsgh"
                    ),
                Document(
                    description="Test document B",
                    hash=doc_hash_2,
                    title="Test B",
                    accesses=3,
                    filename="gile.txt",
                    collection_address="afgagasghhhss"
                    ),
            ],
            creation_date=datetime.datetime.now(),
            oldest_date=datetime.datetime.now(),
            latest_broadcast_date=datetime.datetime.now(),
            latest_btc_tx="btctx1",
            oldest_btc_tx="btctx12",
            accesses=2,
            votes=3,
            votes_last_checked=datetime.datetime.now()
        )
        self.test_signature = Signature(pubkey='itsakey',address=self.address)
Exemplo n.º 6
0
    def delete(self, keyword_id=None):
        keyword = Keyword.find_by_id(keyword_id)

        try:
            keyword.delete()
        except Exception as e:
            return max_res('', 500, 'The record has already deleted.')

        return max_res('The keyword has been deleted.')
Exemplo n.º 7
0
def search_by_name():
    chem_name = request.args['chemical_name']
    matches = molecule_api.search.find_matches(chem_name)
    chems = {}
    resp = []

    for match in matches["PropertyTable"]["Properties"]:
        key = str(match['CID'])
        chems[key] = match
        chems[key]['seq_count'] = 0

    our_mols = Molecule.query.filter(Molecule.CID.in_(chems.keys())).all()
    our_molecules = {str(mole.CID): mole for mole in our_mols}
    our_mole_ids = [mole.ID for mole in our_mols]
    seq_count = db.session.query(Sequence.MoleculeID, func.count(Sequence.SequenceID)).group_by(Sequence.MoleculeID).filter(Molecule.ID.in_(our_mole_ids)).all()
    seq_by_mole_count = {counter[0]: counter[1] for counter in seq_count}

    for key in our_molecules:
        mol = our_molecules[str(key)]
        chem = chems[str(key)]
        chem['Description'] = mol.Description
        chem['Name'] = mol.Name
        chem['ID'] = mol.ID
        chem['seq_count'] = seq_by_mole_count[mol.ID] if mol.ID in seq_by_mole_count else 0
        resp.append(chem)

    unknown_cids = set(chems.keys()).difference(our_molecules.keys())
    if(len(unknown_cids) > 0):
        res = molecule_api.search.get_common_names(list(unknown_cids))
        res = res['InformationList']['Information']
        for chem in res:
            key = str(chem['CID'])
            res_chem = chems[key]
            res_chem['Name'] = chem['Title']
            chem['seq_count'] = 0
            resp.append(res_chem)

    if len(matches) == 0:
        Keyword.add_search_word(chem_name, search_type="molecule")
    return Response(json.dumps(resp), headers={'content-type': 'application/json'})
Exemplo n.º 8
0
    def put(self, keyword_id=None):
        keyword = Keyword.find_by_id(keyword_id)

        args = keyword_update_parser.parse_args()

        keyword = update_all_fields(args, keyword)
        #可以在这里继续添加 需要更新的字段 如
        #    if args['name']:
        #       o.name = args['name']
        #

        db.session.commit()
        try:
            keyword.update()
        except Exception as e:
            return max_res('', 500, 'Failed to modify.')

        return max_res(marshal(keyword, keyword_fields))
Exemplo n.º 9
0
    def get(self, keyword_id=None):
        if keyword_id:
            keyword = Keyword.find_by_id(keyword_id)
            return max_res(marshal(keyword, keyword_fields))
        else:
            conditions = []

            args = keyword_query_parser.parse_args()
            page = args['page']
            per_page = args['pagesize']

            if args['orderby'] not in sortable_fields:
                return max_res('', code=500, errmsg='排序非法字段')
            sort = args['orderby']
            if args['desc'] > 0:
                sort = args['orderby'] + ' desc'

            conditions = make_conditions(conditions, args)
            # 在这里添加更多的 条件查询 例如
            # if args['name'] is not None:
            #       conditions.append(Keyword.name.like('%'+args['name']+'%'))

            if conditions is []:
                pagination = Keyword.query.order_by(text(sort)).paginate(
                    page, per_page, error_out=False)
            else:
                pagination = Keyword.query.filter(*conditions).order_by(
                    text(sort)).paginate(page, per_page, error_out=False)
            paginate = {
                'total': pagination.total,
                'pageSize': pagination.per_page,
                'current': pagination.page
            }
            print(pagination.items)

            return max_res(
                marshal(
                    {
                        'pagination':
                        paginate,
                        'list':
                        [marshal(u, keyword_fields) for u in pagination.items]
                    }, keyword_list_fields))
Exemplo n.º 10
0
def create_keywords():
    keywords = request.json['keywords']
    for key in keywords:
        if "KeywordID" in key:
            if "CreationDate" in key:
                del key['CreationDate']
            keyw = Keyword.query.filter_by(KeywordID=key['KeywordID']).first()
            keyw.merge_fields(**key)
        else:
            keyw = Keyword()
            keyw.merge_fields(**key)
            keyw.validate_required_fields()

        db.session.add(keyw)
    db.session.commit()
    return "OK"
Exemplo n.º 11
0
def put_collection(address_password, title, description, keywords, btc):
    """ Create a collection in local cache
        :param address_password: The password with which to protect the collection.
        Should be at least 20 characters for optimal security and unique.  Generates 
        the unique collection ID deterministically
        :param title: The title of the created collection
        :param description: The description of the created collection
        :param keywords: Comma-separated keywords for the resulting collection
        :param BTC: the Bitcoin address of the resulting collection
    """
    bitmessage_connection = Bitmessage()
    cache = Cache()
    address = bitmessage_connection.create_address(address_password)

    input_keywords = [Keyword(name=x) for x in keywords.split(",")]
    keywords = []
    for key in input_keywords:
            db_key = cache.get_keyword_by_name(key.name)
            if db_key is not None:
                keywords.append(db_key)
            else:
                keywords.append(key)
    collection = Collection(
        title=title,
        description=description,
        address=address,
        accesses=0,
        votes=0,
        btc=btc,
        keywords=keywords,
        documents=[],
        creation_date=datetime.datetime.now(),
        oldest_date=datetime.datetime.now(),
        votes_last_checked=datetime.datetime.now(),
        latest_broadcast_date=datetime.datetime.now()
    )
    cache.insert_new_collection(collection)
    print ("Collection inserted with address/ID " + address)
Exemplo n.º 12
0
"""
for mole in moles:
    mole.save_image_from_pubchem()
    print 'saving image'
    time.sleep(1)
"""

mapped_moles = {str(mole.CID): mole for mole in moles}
for chunk in chunks(mapped_moles.keys(), 10):
    url = "https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/%s/synonyms/JSON" % ','.join(chunk)
    resp = requests.get(url)
    pubchem_info = resp.json()['InformationList']['Information']
    for info in pubchem_info:
        cid = info['CID']
        if "Synonym" in info:
            for syn in info['Synonym']:
                mole = mapped_moles[str(cid)]
                syn = syn[0:299]
                print syn
                key = Keyword(Type='Molecules', \
                              ParentID=mole.ID, \
                              Keyword=syn, \
                              Category='Synonym', \
                              DisplayFormat=syn)
                db.session.add(key)
    db.session.commit()




#http://www.nirs.go.jp/research/division/mic/db/eng/mplsearch.php?target=target_null&nuclide=nuclide_all&compound=&keyword=
Exemplo n.º 13
0
 async def get_all() -> List[Keyword_dto]:
     return await Keyword_dto.from_queryset(Keyword.all())
Exemplo n.º 14
0
 def insert(self, name: str) -> Keyword:
     return Keyword(name=name)
Exemplo n.º 15
0
def get_all_keywords():
    try:
        totalKeyWords = Keyword.objects()
        return totalKeyWords, 200
    except Keyword.DoesNotExist:
        return jsonify(msg='not found'), 404