Ejemplo n.º 1
0
 def add_rating(cls, **kwargs):
     # 取出UserId 和 App Id
     # print(kwargs)
     user = UserBusiness.get_by_user_ID(kwargs['user_ID'])
     app = AppBusiness.get_by_id(kwargs['app'])
     if user.user_ID == app.user.user_ID:
         return []
     rate = AppRatingBusiness.app_rating_repo.read({
         'app': app,
         'user': user
     })
     # print(rate)
     if len(rate) > 0:
         rate = rate[0]
         # 进行更新内容, 但是不会添加新的记录
         del kwargs['user_ID']
         del kwargs['app']
         # rate.update(**kwargs)
         # rate.save()
         # return cls._add_rate_info(rate)
         # print(rate.id, kwargs)
         return cls.update_rating(rate.id, **kwargs)
     else:
         kwargs['create_time'] = datetime.datetime.utcnow()
         kwargs['user'] = user
         kwargs['app'] = app
         rate = AppRatingBusiness.add_rating(**kwargs)
         return cls._add_rate_info(rate)
Ejemplo n.º 2
0
 def get_rate_by_app_and_user(cls, user_ID, app_id):
     user = UserBusiness.get_by_user_ID(user_ID)
     app = AppBusiness.get_by_id(app_id)
     rate = AppRatingBusiness.app_rating_repo.read({
         'app': app,
         'user': user
     })
     if len(rate) > 0:
         rate = rate[0]
         return cls._add_rate_info(rate)
     return []
Ejemplo n.º 3
0
 def get_single_app(cls, app_id):
     app = AppBusiness.get_by_id(app_id)
     path = app.path + '/OVERVIEW.md'
     if os.path.exists(path):
         with open(path, 'r') as file:
             app.overview = file.read()
     app.versions = [app.versions[-1]]
     app.average_rate = app.avg_rate
     app.call_num = app.call
     app.user_ID = app.user.user_ID
     app.user_name = app.user.username
     app.avatarV = app.user.avatarV
     app.args = AppBusiness.load_app_params(app, app.versions[-1])
     app.avatar_url = app.user.avatar_url
     app.view_num = EventBusiness.get_number({'app': app, 'action': "view"})
     return app
Ejemplo n.º 4
0
 def related_app(cls, app_id, user_ID=None, page_size=7):
     app = AppBusiness.get_by_id(app_id)
     tags = app.tags
     related_apps = AppBusiness.get_objects(tags=tags,
                                            has_version=True,
                                            privacy='public')
     related_apps, _ = related_apps.objects, related_apps.count
     related_ids = [str(a.id) for a in related_apps]
     # 拷贝一份
     back_apps = [app for app in related_apps]
     # 删除重复
     if app_id in related_ids:
         index = related_ids.index(app_id)
         del back_apps[index]
     if user_ID:
         self_app, _ = cls.self_apps(user_ID)
         # 拼接, 去重
         for a in self_app:
             if str(
                     a.id
             ) not in related_ids and 'tutorial' not in a.display_name.lower(
             ):
                 back_apps.append(a)
                 related_ids.append(str(a.id))
     # 判断是否小于七个
     if len(related_apps) < page_size:
         # 判断当前个数, 寻找剩余数量
         supplement_apps = AppBusiness.get_objects(page_size=page_size,
                                                   has_version=True,
                                                   privacy='public').objects
         for a in supplement_apps:
             if str(
                     a.id
             ) not in related_ids and 'tutorial' not in a.display_name.lower(
             ):
                 back_apps.append(a)
     # 添加数据
     for app in back_apps:
         path = app.path + '/OVERVIEW.md'
         if os.path.exists(path):
             with open(path, 'r') as file:
                 app.overview = file.read()
         app.versions = app.versions[-1]
         app.average_rate = app.avg_rate
         app.call_num = app.call
         app.avatar_url = app.user.avatar_url
     return back_apps, page_size
Ejemplo n.º 5
0
    def run_app(cls, app_id, input_json, user_ID, version):
        """

        :param app_id: app id
        :param input_json:
        :param user_ID:
        :param version:
        :return:
        :rtype:
        """
        app = AppBusiness.get_by_id(project_id=app_id)
        url = '-'.join([app.user.user_ID, app.name, version])
        domin = f"http://{DOCKER_IP}:8080/function/"
        url = domin + url
        payload = json.dumps(input_json)
        headers = {
            'content-type': "application/json",
        }
        response = requests.request("POST", url, data=payload, headers=headers)
        pattern = re.compile(r'STRHEAD(.+?)STREND', flags=re.DOTALL)
        results = pattern.findall(response.text)
        print(results, 111)
        try:
            output_json = json.loads(results[0])
        except IndexError as e:
            try:
                errors = cls.business.get_service_logs(app, version)
            except IndexError as e:
                output_json = {
                    'errors': ['Service is down please deploy again!']
                }
            else:
                output_json = {'errors': errors}
        # output_json = response.json()
        # 成功调用后 在新的collection存一笔
        user_obj = UserBusiness.get_by_user_ID(user_ID=user_ID)
        # 筛选 input_json

        StatisticsBusiness.use_app(
            user_obj=user_obj,
            app_obj=app,
            output_json=output_json
            # input_json=input_json,
            # output_json=output_json
        )
        return output_json
Ejemplo n.º 6
0
    def run_app(cls, app_id, input_json, user_ID):
        """

        :param app_id: app id
        :type app_id: ObjectId
        :param input_json:
        :type input_json:
        :param user_ID:
        :type user_ID:
        :return:
        :rtype:
        """
        app = AppBusiness.get_by_id(project_id=app_id)
        url = app.user.user_ID + "-" + app.name
        domin = f"http://{DOCKER_IP}:8080/function/"
        url = domin + url
        payload = json.dumps(input_json)
        headers = {
            'content-type': "application/json",
        }
        response = requests.request("POST", url, data=payload, headers=headers)
        print(response.text)
        pattern = re.compile(r'STRHEAD(.+?)STREND', flags=re.DOTALL)
        results = pattern.findall(response.text)
        output_json = json.loads(results[0])
        print(output_json)
        # output_json = response.json()
        # 成功调用后 在新的collection存一笔
        user_obj = UserBusiness.get_by_user_ID(user_ID=user_ID)
        # 筛选 input_json

        StatisticsBusiness.use_app(
            user_obj=user_obj, app_obj=app,
            output_json=output_json
            # input_json=input_json,
            # output_json=output_json
        )
        return output_json
Ejemplo n.º 7
0
 def get_action_entity(cls, app_id, **kwargs):
     app = AppBusiness.get_by_id(app_id)
     return AppBusiness.get_action_entity(app, **kwargs)
Ejemplo n.º 8
0
 def get_call_num(cls, app_id):
     app = AppBusiness.get_by_id(app_id)
     return app.call