def download(self, request_json): app_id = request_json["app_id"] view_name = request_json["view"] # confirm view app = pykintone.login(self.env.domain, self.env.login_id, self.env.password).app(app_id) view = None query = "" fields = [] if view_name: views = app.administration().view().get().views for v in views: if v.name == view_name: view = v break if view is None: raise kintoneException( "指定された名前のビュー「{}」は、アプリに作成されていません".format(view_name)) # make query query = view.filter_cond if view.filter_cond else "" query += " order by " + view.sort if view.sort else "" fields = view.fields # make dfe kapp = Application(self.env) dfe = kapp.load(app_id, query, fields) return dfe
def get_app_id(self, app_name): kintone = pykintone.login(self.env.domain, self.env.login_id, self.env.password) result = kintone.administration().select_app_info() if result.ok: matched = [a for a in result.infos if a.name == app_name] if len(matched) > 0: return matched[0].app_id else: return "" else: raise Exception("Error occurred when getting the app_id")
def test_api_interface(self): # load from file kintone = pykintone.load(envs.FILE_PATH) app = kintone.app() self.assertTrue(app) # login app = pykintone.login("domain", "user_id", "password").app(1, api_token="token") self.assertTrue(app) # app app = pykintone.app("domain", 1, "token") self.assertTrue(app)
def request_to_dfe(self, request_json): app_id = request_json["app_id"] field_settings = request_json["fields"] view_name = request_json["view"] # check fields setting features = [] target = "" for fs in field_settings: if field_settings[fs]["usage"] == 1: target = fs elif field_settings[fs]["usage"] == 0: features.append(fs) if not target: raise kintoneException("予測対象の項目が指定されていません") if len(features) == 0: raise kintoneException("予測に使用する項目が指定されていません") # confirm view app = pykintone.login(self.env.domain, self.env.login_id, self.env.password).app(app_id) view = None query = "" if view_name: views = app.administration().view().get().views for v in views: if v.name == view_name: view = v break if view is None: raise kintoneException( "指定された名前のビュー「{}」は、アプリに作成されていません".format(view_name)) # make query query = view.filter_cond if view.filter_cond else "" query += " order by " + view.sort if view.sort else "" # make field setting fields = features if view is None else [ f for f in view.fields if f in features ] fields = [target] + fields # make dfe kapp = Application(self.env) dfe = kapp.load(app_id, query, fields, target) return dfe
def get_fields(self, app_id): # todo: if app_id exists on karura app, get field definition from it. app = pykintone.login(self.env.domain, self.env.login_id, self.env.password).app(app_id) fields = app.administration().form().get() if not fields.ok: raise Exception( "Error occurred when getting the form information from kintone." ) fs = fields.raw d = OrderedDict() for f_code in fs: f = Field.create(f_code, fs[f_code]) if f: d[f_code] = f return d
def load(self, app_id, query="", fields=(), target=""): app = pykintone.login(self.env.domain, self.env.login_id, self.env.password).app(app_id) fields_d = self.get_fields(app_id) if len(fields) > 0: d = OrderedDict() for f in fields: if f in fields_d: d[f] = fields_d[f] fields_d = d q = query + " " if query else "" records = [] _fields = list(fields_d.keys()) selected = app.select(query=q + "limit {}".format(self._kintone_limit), fields=_fields) records = selected.records if selected.total_count > self._kintone_limit: repeat = np.floor( min(self.max_count, selected.total_count) / self._kintone_limit) for i in range(int(repeat)): selected = app.select( query=q + "limit {} offset {}".format(self._kintone_limit, (i + 1) * self._kintone_limit), fields=_fields) if len(selected.records) > 0: records += selected.records data = [] columns = [] for i, r in enumerate(records): row = [] if i == 0: columns = [f for f in _fields if f in r] for f in columns: v = r[f]["value"] row.append(v) if len(row) > 0: data.append(row) fs = [fields_d[c] for c in columns] df = pd.DataFrame(np.array(data), columns=[f.label for f in fs]) categoricals = [ f.label for f in fs if f.get_feature_type() == FType.categorical ] numericals = [ f.label for f in fs if f.get_feature_type() == FType.numerical ] datetimes = [ f.label for f in fs if f.get_feature_type() == FType.datetime ] texts = [f.label for f in fs if f.get_feature_type() == FType.text] uniques = [f.label for f in fs if f.get_feature_type() == FType.unique] dfe = DataFrameExtension(df, categoricals, numericals, datetimes, texts, uniques) if target: dfe.target = fields_d[target].label return dfe