def create_project(project_id, xml_project=None, project_category=None): if xml_project is None: xml = XML(config.XML_URL) project = xml.get_project(project_id) category = xml.get_category(project.categoryid.text) else: category = project_category project = xml_project if Categories.get_category(int(project.categoryid.text)) is None: if category is None: xml = XML(config.XML_URL) category = xml.get_category(project.categoryid.text) Categories.create_category(category_id=int( project.categoryid.text), category_name=category.text) html = HTML(project.url.text) project_data = { 'offer_id': project['id'], 'category': int(project.categoryid.text), 'url': project.url.text, 'price': float(project.price.text), 'currency': project.currencyid.text, 'picture': ImageInfo.create_img(project.picture.text), 'available': project['available'], 'short_desc': project.typeprefix.text, 'full_desc': html.get_full_description(), 'goal_price': html.get_goal_price(), 'current_price': html.get_current_price(), 'city': html.get_city(), 'model': project.model.text, } Projects.create_project(project_data)
def get_projects_by_category(category_id, amount=None, page=0): if amount is None: projects = Projects.select().where( Projects.category == category_id) else: projects = Projects.select().where( Projects.category == category_id).limit(amount).offset( int(page) * amount) return projects
def delete_projects_with_xml(): xml = XML(xml_url=config.XML_URL) projects = Projects.select() for project in projects: xml_project = xml.get_project(project_id=project.offer_id) if xml_project is None: Database.delete_project(project_id=project.offer_id, database_project=project)
def resize_all_img(new_size): xml = XML(xml_url=config.XML_URL) projects = Projects.select() for project in projects: try: img_url = xml.get_project( project_id=project.offer_id).picture.text Database.resize_img(img_id=project.picture, new_size=new_size, database_project=project, img_url=img_url) except AttributeError: Database.update_project(project_id=project.offer_id, available=0)
def resize_img(img_id, new_size, database_project=None, img_url=None): if database_project is None: project = Projects.get(picture=img_id) else: project = database_project if img_url is None: xml = XML(xml_url=config.XML_URL) picture_url = xml.get_project( project_id=project.offer_id).picture.text else: picture_url = img_url filename = ImageInfo.get_img_content(url=picture_url) filename = ImageInfo.resize_image(filename=filename, new_size=new_size) data = ImageInfo.upload_photo(img_filename=filename) project.picture.template_owner_id = data.get('owner_id') project.picture.template_media_id = data.get('media_id') project.picture.save()
def get_or_create_project(project_id): project = Projects.get_projects_by_id(project_id) if project is None: return Database.create_project(project_id) else: return project
def get_project(project_id): return Projects.get_projects_by_id(project_id)