def init_db(): arbit_time = datetime.datetime.now() resources = [{ "name": "oxygen", "type": "consumable", "availability_start": str(arbit_time), "availability_end": str(arbit_time), "amount": "123.123", "rate": "12.34" }, { "name": "water", "type": "consumable", "availability_start": str(arbit_time), "availability_end": str(arbit_time), "amount": "5254.2342", "rate": "74.443" }, { "name": "battery", "type": "energy", "availability_start": str(arbit_time), "availability_end": str(arbit_time), "amount": "423", "rate": "1135.3" }] Resources.insert_many(resources).execute()
def _store_files(kind, name, files): ''' Store a group of files as attachment. Args: kind: attachment type. e.g. 'image/cover' name: attachment name. files: list of (mime, meta, data). Returns: Attachments object. ''' ref_id = db.next_id() atta = Attachments(_id=ref_id, user_id=ctx.user._id, kind=kind, name=name) resources = [] for mime, meta, data in files: r_id = db.next_id() url = '/files/%s/%s' % (datetime.now().strftime('%Y/%m/%d'), r_id) r = Resources(_id=r_id, ref_id=ref_id, url=url, size=len(data), mime=mime, meta=meta, data=data) resources.append(r) atta.size = resources[0].size atta.resource_ids = ','.join([r._id for r in resources]) with db.transaction(): atta.insert() for r in resources: r.insert() return atta
def importFile(self, root_path): dir_or_files = os.listdir(root_path) # 获取文件夹名当作title title = root_path for dir_file in dir_or_files: # 获取目录或者文件的路径 dir_file_path = os.path.join(root_path, dir_file) # 判断该路径为文件还是路径 if os.path.isdir(dir_file_path): # 递归获取所有文件和目录的路径 self.importFile(dir_file_path) else: res_title = title.split('/')[-1] # 检查文件是否需要被清理并获得文件类型 resPath = self.clean(dir_file_path) # 如果没有被清理则导入数据库 if resPath != None: res = Resources(res_type=resPath[2], res_class=self.resclass, res_subclass=self.ressubclass, res_title=res_title, res_origin='PC', res_path=resPath[0], file_name=resPath[1], comment1=resPath[3]) try: db.session.add(res) db.session.commit() print('导入文件成功:', resPath[0]) except DataError as e: print('导入%s失败' % (resPath[0])) print(e)
def get(self, user): try: value = Resources().allocate(user) except OutResourcesError: return 'Out of resources.', 503 else: return value, 201
def get(self, resource): try: value = Resources().deallocate(resource) except NotFoundError: return 'Not allocated.', 404 else: return value, 204
def post(self): resource = Resources( name=request.json.get('name', ''), category=request.json.get('category', ''), address=request.json.get('address', ''), zip_code=request.json.get('zip_code', ''), quantity=request.json.get('quantity', ''), start_time=request.json.get('start_time', time(hour=00, minute=00)), end_time=request.json.get('end_time', time(hour=00, minute=00)), timestamp=request.json.get('timestamp', ), description=request.json.get('description', ''), image_url=request.json.get('image_url', ''), url=request.json.get('url', ''), min_age=request.json.get('min_age', 0), max_age=request.json.get('max_age', 99), gender=request.json.get('gender', ''), accessibility=request.json.get('accessibility', '')) print("*** resource object generated:", resource, file=sys.stderr) try: db.session.add(resource) db.session.commit() print("*** Added resource:", str(resource), file=sys.stderr) return resource, 201, {'Content-Type': 'application/json'} except exc.IntegrityError: db.session().rollback() return "Potential duplicate entry", 409, { 'Content-Type': 'application/json' }
def get_image_url(atta_id, index): ' index = 0 (origin), 1 (large), 2 (medium), 3 (small)... ' a = Attachments.get_by_id(atta_id) if not a: raise notfound() if not a.kind.startswith('image/'): raise notfound() rs = Resources.select('where ref_id=?', atta_id) rs.sort(cmp=_cmp_image) url = rs[-1].url if index >= len(rs) else rs[index].url raise redirect(url)
async def _retry_error_page(self): while self.fail_page_info: page_info = self.fail_page_info.pop() resource = Resources(name=page_info[0], owner='电波字幕组', stype='tvshow', original=page_info[1]) session.add(resource) try: session.commit() except Exception: session.rollback() self.fail_page_info.append(page_info) else: session.close()
def ask_resource(self, name): """ returns data related to tuple with name name """ print "request for ", name, " received" # resource_string = get_resource_string(name) resource = Resources.get(Resources.name == name) resource_string = "" + resource.name + " # " + resource.type resource_string += " # " + str(resource.availability_start) resource_string += " # " + str(resource.availability_end) resource_string += " # " + str(resource.rate) resource_string += " # " + str(resource.amount) # Sending the resource as a string return str(resource_string)
async def process_page_info(self, html): """ 返回单页上所有的数据列表 """ soup2 = self.soup(html).find_all('a', attrs={'rel': 'bookmark'}) info_list = [] for s in soup2: s = self.soup(str(s)) info_list.append( (s.span.string, s.a['href'])) # todo 放进redis数据库后再读取 resource = Resources(name=s.span.string, owner='电波字幕组', stype='tvshow', original=s.a['href']) session.add(resource) try: session.commit() info_list = [] except Exception as e: session.rollback() logging.error('插入数据库发生错误--{}'.format(str(e))) finally: session.close() return info_list
def set_updated_resource_values(self, resource_object_string): """ updates the tuple with name = resource_object_string.name """ # input strings are expected to have columns seperated with a '#' strManager = StringManager() resource_string_object = strManager.str_to_object( resource_object_string) resource = Resources.get(Resources.name == resource_string_object.name) # Updating tuple resource.rate = resource_string_object.rate resource.amount = resource_string_object.amount resource.availability_start = parser.parse( resource_string_object.availability_start) resource.availability_end = parser.parse( resource_string_object.availability_end) if (resource.save()): print resource_string_object.name, " tuple successfully updated" return "SUCCESS" else: print "error while updating tuple ", resource_string_object.name return "FAILURE"
def run(self, count, name, *args, **kwargs): Resources().init(count, name) return super(ResmApp, self).run(*args, **kwargs)
def get(self): return Resources().reset(), 204
def get(self, user=None): return Resources().get_list(user)