Exemple #1
0
    def post(self):
        data = create_research.parse_args()

        current_username = get_jwt_identity()['username']
        current_user = User.find_by_username(current_username)
        new_research = Research()
        print(new_research.id)
        try:
            new_research.topic = data.get('topic')
            new_research.description = data.get('description')

            for word in data.get('keywords'):
                new_keyword = ResearchKeyword(keyword=word,
                                              researchId=new_research.id)
                new_research.keywords.append(new_keyword)
                db.session.add(new_keyword)

            for module in data.get('modules'):
                new_module = ResearchModule(module=module,
                                            researchId=new_research.id)
                new_research.modules.append(new_module)
                db.session.add(new_module)

            new_research.updateInterval = data.get('update_interval')
            new_research.type_of_research = data.get('isPublic')
            new_research.analysers = data.get('analysers')
            new_research.appId = data.get('app_id')
            new_research.appName = data.get('app_name')
            new_research.appDev = data.get('app_dev')

            if 'search' in data.get('modules'):
                new_search = SearchTrends(id=new_research.id)
                new_search.query = ' '.join(data.get('keywords')[:5])
                new_research.search.append(new_search)
                db.session.add(new_search)

            user_res = UserResearchPermission(userId=current_user.id,
                                              researchId=new_research.id,
                                              permission=True)

            user_res.researches.append(new_research)
            current_user.my_researches.append(user_res)
            current_user.owners.append(new_research)

            db.session.add(new_research)
            db.session.commit()

        except Exception as e:
            print(e)
            return {'response': False, 'message': 'Internal server error'}, 500

        from requests import get as req

        try:
            print(new_research.id)
            response = req(
                'http://localhost:5000/ml/api/v1.0/update?res_id={}'.format(
                    new_research.id)).json()
            if response['done'] == False:
                return {"message": "Internal server error"}, 500

            itters = ConductedResearch.query.filter_by(
                researchId=new_research.id).all()

            new_research.conducted.append(itters[-1])
            db.session.add(itters[-1])
            db.session.commit()
            print('commit')

        except Exception as e:
            return {'response': False, 'message': str(e)}, 404

        return {
            'response': True,
            'id': new_research.id,
            'message': 'Research {} was created'.format(new_research.topic)
        }
Exemple #2
0
    def post(self):
        data = create_research.parse_args()
        
        current_username = get_jwt_identity()
        current_user = User.find_by_username(current_username)

        try:
            new_research = Research()
            new_research.topic = data.get('topic')
            new_research.description = data.get('description')

            for word in data.get('keywords'):
                new_keyword = ResearchKeyword(
                    keyword=word, 
                    researchId=new_research.id
                )
                new_research.keywords.append(new_keyword)
                db.session.add(new_keyword)
            
            print('\n\n\n',data.get('modules'),'\n\n\n')
            for module in data.get('modules'):
                new_module = ResearchModule(
                    module=module,
                    researchId=new_research.id
                )
                new_research.modules.append(new_module)
                db.session.add(new_module)
            
            new_research.updateInterval = data.get('update_interval')
            new_research.type_of_research = data.get('isPublic')
            new_research.analysers = data.get('analysers')
            new_research.appId = data.get('app_id')
            new_research.appName = data.get('app_name')
            new_research.appDev = data.get('app_dev')

            if 'search' in data.get('modules'):
                new_search = SearchTrends(id=new_research.id)
                new_search.query = ' '.join(data.get('keywords')[:5])
                new_research.search.append(new_search)
                db.session.add(new_search)

            user_res = UserResearchPermission(
                userId=current_user.id,
                researchId=new_research.id,
                permission=True
            )
            
            user_res.researches.append(new_research)
            current_user.my_researches.append(user_res)
            current_user.owners.append(new_research)
            
            db.session.add(new_research)
            db.session.commit()
        
        except Exception as e:
            return {
                'response': False,
                'message': 'Internal server error'
            }, 500

        return {
            'response': True,
            'id': new_research.id,
            'message': 'Research {} was created'.format(new_research.topic)
        }