def __init__(self, gallery, debug=False): '''debug: True면 디버그용 문자열 출력''' self.debug = debug self.gallery = gallery self.galleryUrl = 'http://gall.dcinside.com/board/lists/?id=%s&page=%d' self.idSet = set() # db넣기전 1차 id 체크용 self.s_db = Session() if self.s_db.query(Category).filter(Category.name == self.gallery).count() == 0: category = Category() category.name = self.gallery self.s_db.add(category) self.s_db.commit() self.categoryId = category.id else: id, = self.s_db.query(Category.id).filter(Category.name == self.gallery)[0] self.categoryId = id headers = {} headers['User-Agent'] = USERAGENT headers['Accept-Language'] = 'ko-KR,ko;q=0.8,en-US;q=0.5,en;q=0.3' headers['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' headers['Referer'] = REFERER % self.gallery self.s_req = requests.Session() self.s_req.headers.update(headers) r = self.s_req.get(self.galleryUrl % (self.gallery, 1)) id = getStartArticleId(r.text) if id is None: raise RuntimeError('[error]wrong gall')
def assign_category(club_id): club = Club.query.filter_by(id=club_id).first() if not club: return json.dumps({'success': False, 'error': 'Club not found'}), 404 post_body = json.loads(request.data) category = Category.query.filter_by(name=post_body.get('name')).first() if not category: category = Category(name=post_body.get('name', '')) club.categories.append(category) db.session.add(category) db.session.commit() return json.dumps({'success': True, 'data': category.serialize()}), 200
def post(self): args = parser.parse_args() new_category = Category() new_category.name = args['name'] new_category.color = args['color'] new_category.user_id = current_user.id db.session.add(new_category) db.session.commit() return jsonify(status='success', result={ 'id': new_category.id, 'name': new_category.name, 'color': new_category.color })
def add_category(bot, update): """Добавляем новую категорию""" session = db.session user_id = update.message.chat_id name_of_category = update.message.text # проверяем наличие такой категории в бд user_categories = session.query(Category).filter_by(user_id=user_id) for category in user_categories: if category.name_of_category.lower() == name_of_category.lower(): update.message.reply_text('Такая категория уже существует.') return 'categories_menu' else: continue # добавляем категорию в бд category = Category(name_of_category=name_of_category, user_id=user_id) session.add(category) session.commit() update.message.reply_text('Категория успешно добавлена.') return start(bot, update)
def _get_or_create_category(cat): category = Category.query.filter_by(category=cat).first() if category is not None: return category category = Category(category=cat) db.session.commit() return category
def update(self, *args, msg=None, hand=None, **kwargs): cat = Category.find_by(msg.text) if not cat: hand.send_msg(msg, f'Категория {msg.text} не найдена') return hand.send_msg(msg, f'Категория {msg.text}', reply_markup=hand.keybords.in_line_kb_menu(menu_items=cat.wares)) hand.send_msg(msg, 'Ok', reply_markup=hand.keybords.list_kb_menu(menu_items=hand.update_from_category()))
def get_ads_cat_filtered(cat_name, longitude, latitude, total): from db import Ad, Category cat_id = Category.match(cat_name).id ads = Ad.query.filter(Ad.category_id == cat_id).all() locations = [] for ad in ads: locations += [ad.location] return location_sort((longitude, latitude), locations, total)
def define_categories(self): cats = [ 'Soccer', 'Basketball', 'Baseball', 'Frisbee', 'Snowboarding', 'Rock Climbing', 'Foosball', 'Skating', 'Hockey' ] self.categories = dict( (cat, Category(name=cat, user=random.choice(self.users))) for cat in cats)
def create_category(budget_id): budget = Budget.query.filter_by(id=budget_id).first() if budget is not None: category_body = json.loads(request.data) category_name = category_body.get('name') category = Category.query.filter_by(budget_id=budget.id, name=category_name).first() if category is None: category = Category( name = category_name, total = 0, budget_id = budget.id ) budget.categories.append(category) db.session.add(category) db.session.commit() return json.dumps({'success': True, 'data': category.serialize()}), 201 return json.dumps({'success': False, 'error': 'Category already exists!'}), 201 return json.dumps({'success': False, 'error': 'Budget not found!'}), 404
def show_categories(): category_text = ["Here are the list of categories the bot knows about:\n"] for category in Category.select(): category_text.append(" - *%s* (_%s_)" % (category.display_name, category.name)) constants.slack.chat.post_message(OUTPUT_CHANNEL, "\n".join(category_text), as_user=True)
def update_from_category(self): events = [] cat_cmd = CaregoryCommand() for cat in Category.all(): event = str(cat) events.append(event) if not self.check_event(event): router.reg_command(cat_cmd, event) events.append(settings.NAV_MENU) return events
def fake_category(self, count: int = 10): """构造虚拟分类""" for i in range(count): category_obj = Category(name=self.fake.word()) session.add(category_obj) try: session.commit() session.refresh(category_obj) except Exception as e: session.rollback()
def newCategories(): categories = session.query(Category).order_by(asc(Category.name)) if request.method == 'POST': newCategory = Category(name=request.form['name']) session.add(newCategory) flash('New Category \"%s\" Successfuly Created' % newCategory.name) session.commit() return redirect(url_for('showCategories')) else: return render_template('newCategory.html',categories=categories)
def handle_summary_parsing(summary_text, user_profile): print("Attempting to parse text...") results = parse_summary(summary_text, user_profile) if results is not None and len(results) > 0: glory_walls = save_glory_walls(results, user_profile) render_to_wiki(Category.select(), UTOPIA_AGE) send_response(glory_walls, user_profile) else: response = chatbot.get_response(summary_text) constants.slack.chat.post_message(OUTPUT_CHANNEL, response, as_user=True)
def assign_category(user_id, trip_id, event_id): user = User.query.filter_by(id=user_id).first() if not user: return json.dumps({'success': False, 'error': 'User not found!'}), 404 trip = Trip.query.filter_by(id=trip_id).first() if not trip: return json.dumps({'success': False, 'error': 'Trip not found!'}), 404 if ((trip in user.trips) == False): return json.dumps({'success': False, 'error': 'Trip not found!'}), 404 event = Event.query.filter_by(id=event_id).first() if not event: return json.dumps({'success': False, 'error': 'Event not found!'}), 404 if ((event in trip.events) == False): return json.dumps({'success': False, 'error': 'Event not found!'}), 404 post_body = json.loads(request.data) category = Category.query.filter_by(name=post_body.get('name', '')).first() if not category: category = Category(name=post_body.get('name', '')) event.categories.append(category) db.session.add(category) db.session.commit() return json.dumps({'success': True, 'data': category.serialize()}), 200
def add_new_item(self, **arguments): category_name = arguments["category"] category = self.session.query(Category).filter_by( owner=self.current_user, name=category_name).first() if not category: category = Category(name=category_name, owner=self.current_user) new_item = Budget(category=category, date=arguments["date"], title=arguments["title"], amount=arguments["amount"], currency=arguments["currency"]) self.session.add(new_item)
def assign_category(task_id): task = Task.query.filter_by(id=task_id).first() if not task: return json.dumps({'success': False, 'error': 'Task not found!'}), 404 post_body = json.loads(request.data) category = Category.query.filter_by( description=post_body.get('description', '')).first() if not category: category = Category(description=post_body.get('description', ''), color=post_body.get('color')) task.categories.append(category) db.session.commit() return json.dumps({'success': True})
def user_category_post(u_id: int): form = request.form logging.info('user {} data {}'.format(u_id, form)) name = form.get('name', '') order = form.get('order', '') if not name or not order or not order.isdigit(): return jsonify({'status': -1}) order = int(order) if 1 > order or order > 1000: return jsonify({'status': -1}) cate = Category(name=name, order=order, user=request.user) logging.info('user {} add new category {} {}'.format(u_id, cate.id, name)) commit() return jsonify({'status': 1, 'id': cate.id})
def select_website(): """查询当前用户的网址""" sites = UserSite.select(lambda x: x.user == request.user and x.status == UserSiteStatus.normal)[:] categories = Category.select( lambda c: c.user == request.user and not c.delete).order_by( Category.order)[:] ss = defaultdict(list) for s in sites: key = s.cate.id if s.cate else "" ss[key].append(s) categories = list(categories) categories.insert(0, {"id": '', 'name': '', 'order': 0}) return ss, categories
def get_ads_and_write_to_db(self, link, response): try: category = Category.get(name=link) except DoesNotExist: category = Category.create( name=link ) ad_blocks = response.html.xpath('//div[@class="offer-wrapper"]') for ad in ad_blocks: try: ad_title = ad.xpath('//h3')[0].text ad_date = ad.xpath('//p[@class="lheight16"]/small[2]')[0].text ad_price = ad.xpath('//p[@class="price"]')[0].text ad_photo = ad.xpath('//img/@src')[0] ad_link = ad.xpath('//h3/a/@href')[0] ad_city = ad.xpath('//p[@class="lheight16"]/small[1]')[0].text ad_data = { 'title': ad_title, 'date': ad_date, 'price': ad_price, 'photo': ad_photo, 'link': ad_link, 'city': ad_city, 'category_id': category } except Exception as e: print('Parse error\n', e, type(e)) try: Ad.create(**ad_data) except Exception as e: print('DB error\n', e, type(e))
def user_category_delete(u_id: int): form = request.form logging.info('user {} delete data {}'.format(u_id, form)) cate_id = form.get('id', '') if not cate_id or not cate_id.isdigit(): return jsonify({'status': -1}) cate_id = int(cate_id) cate = Category.select(lambda x: x.id == cate_id and x.user == request.user and not x.delete).first() if not cate: return jsonify({'status': -1}) cate.delete = True logging.warning('user {} delete category {}'.format(u_id, cate.id)) commit() return jsonify({'status': 1, 'id': cate.id})
def assign_category(task_id): task = Task.query.filter_by(id=task_id).first() if task is None: return failure_response("Task not found!") body = json.loads(request.data) description = body.get('description') if description is None: return failure_response("No description!") category = Category.query.filter_by(description=description).first() if category is None: category = Category(description=description, color=body.get('color', 'purple')) task.categories.append(category) db.session.commit() return success_response(task.serialize())
def test_get_specific_question_category(self): # We Will Insert New Category , New Question belong to this category # and get them category = Category('Test') category.insert() newQuestion = Question('Test Question ?', "True", category.id, 5) newQuestion.insert() request = self.client().get(f'/categories/{category.id}/questions') # Remove Inserted Data category.delete() newQuestion.delete() data = json.loads(request.data) self.assertEqual(data['questions'][0]['answer'], "True")
def user_category_put(u_id: int): form = request.form logging.info('user {} update data {}'.format(u_id, form)) cate_id = form.get('id', '') name = form.get('name', '') order = form.get('order', '') if not cate_id or not cate_id.isdigit( ) or not name or not order or not order.isdigit(): return jsonify({'status': -1}) order = int(order) if 1 > order or order > 1000: return jsonify({'status': -1}) cate = Category.select(lambda x: x.id == cate_id and x.user == request.user and not x.delete).first() cate.name = name cate.order = order commit() logging.info('user {} update category {}'.format(u_id, cate.id)) return jsonify({'status': 1, 'id': cate.id})
def create_category(topic_url): topic = session.query(Topic).filter_by(url=topic_url).one() if 'username' not in login_session: return redirect('/login') if request.method == 'POST': is_unique = check_category_uniqueness(topic.id, request.form['cname']) if is_unique: new_url = url_maker(request.form['cname']) new_category = Category(name=request.form['cname'], url=new_url, topic_id=topic.id) session.add(new_category) session.commit() return redirect(url_for('show_topic', topic_url=topic_url)) else: return render_template('new_category.html', topic=topic, error=True) else: return render_template('new_category.html', topic=topic)
def user_website_post(u_id: int): form = request.form logging.info('user {} post data {}'.format(u_id, form)) cate_id = form.get('cate_id', '') name = form.get('name', '') url = form.get('url', '') order = form.get('order', '') type_ = form.get('type', '') if not name or not url or not order or not order.isdigit(): return jsonify({'status': -1}) order = int(order) if order > 1000 or order < 1: return jsonify({'status': -1}) if not cate_id or not cate_id.isdigit(): cate_id = None cate = None else: cate_id = int(cate_id) cate = Category.select(lambda x: x.user == request.user and x.id == cate_id and not x.delete).first() if not cate: return jsonify({'status': -1}) try: icon = query_icon(urlparse(url).netloc) except Exception: icon = '' site = UserSite(name=name, url=url, user=request.user, icon=icon, cate=cate, order=order) commit() logging.info('user {} post site {}'.format(u_id, site.id)) if not icon: logging.warning("icon not found, send to redis") download_icon_job(site.id) if type_ == 'index': return redirect_home() return jsonify({'status': 1, 'id': site.id})
def batch_insert_website(data, user): """批量插入数据 :type data: dict key -> category, values -> sites :type user: User """ with db_session: i = 0 for k, ws in data.items(): i += 1 if k: c = Category(name=k, user=user, order=10 * i) else: c = None k = 0 for w in ws: k += 1 UserSite(name=w[0], cate=c, user=user, url=w[1], icon=w[2], order=10 * k)
def create(): """ Creates an ad Requires the following param: * long, lat * category(id) , email, title, price, image, description """ # validation required_fields = ("long", "lat", "email", "title", "price", "image") for field in required_fields: if not field in request.form: return jsonify({"res": False, "error": "There is an error creating your ad due to missing field(s)."}) # check for price integer try: float(request.form.get("price")) except ValueError: return jsonify({"res": False, "error": "Price needs to be a numerical value."}) # email format validation validate_me = StringValidator(request.form.get("email")) if not validate_me.isEmail(): return jsonify({"res": False, "error": "Not a valid email address."}) from db import Location, Category, Ad location = Location(longitude=request.form.get("long"), latitude=request.form.get("lat")) category = Category.get(request.form.get("category")) id = Ad.create( location, request.form.get("email"), request.form.get("title"), request.form.get("price"), save_file(request.form.get("image")), category, request.form.get("description", ""), ) return jsonify({"res": id})
def make_budget(owner): categories = { 'Food': [ 'Chicken breast', 'Bread', 'Salad', 'Milk', 'Chocolate', 'Sausage', 'Apples', 'Cake' ], 'Home': ['Soap', 'Shampoo', 'Flower', 'Dinnerware', 'Trash bags'], 'Clothes': ['Jacket', 'Skirt', 'Sneakers', 'Hat', 'Blouse', 'Sweater'], 'Transport': [ 'Bus ticket', 'Gasoline 20 l', 'Car wash', 'Parking 8 h', 'Train ticket' ], 'Entertainment': ['Restaurant', 'Cafe', 'Cinema', 'Gym membership'], 'Bills': [ 'Electricity', 'Cold water', 'Hot water', 'Internet', 'Heating', 'Mobile phone' ], 'Other spendings': ['Gift for friend', 'Haircut', 'Manicure'] } random_cat = fake.random_element(categories.keys()) random_title = fake.random_element(categories[random_cat]) category = session.query(Category).filter_by(owner=owner, name=random_cat).first() if not category: category = Category(name=random_cat, owner=owner) session.add(category) budget = Budget(category=category, date=fake.date_this_year(), title=random_title, amount=fake.random_int(min=1, max=2000), currency="UAH") session.add(budget) return budget
# Importing the database classes from db import Base, Category, Items # Connect to the database engine = create_engine('sqlite:///catalog.db') # Bind engine to Base Base.metadata.bind = engine # Creating instance of sessionmaker DBSession = sessionmaker(bind=engine) # Instatiate the DBSession Class session = DBSession() category1 = Category(name= 'Udacity') session.add(category1) session.commit() category2 = Category(name= 'Google') session.add(category2) session.commit() item1 = Items(name= 'FSND', price= '1000$', desc= 'Full Stack NanoDegree', category= category1) session.add(item1) session.commit() item2 = Items(name= 'Android', price= '2000$', desc= 'Android Nanodegree', category= category1) session.add(item2) session.commit()
from db import Category print Category.gen_options()
def insert_categories_to_database(self): with self.app.app_context(): self.db.session.add(Category('category')) self.db.session.add(Category('category2')) self.db.session.commit()
def main(): sys.path.append(ROOT_PATH) import db from db import Information, Category, Group, Type os.chdir(os.path.dirname(__file__)) clear_dir('locales') sessions = {} for lcid, name in LCID.items(): sessions[lcid] = db.create_session( os.path.join('locales', lcid + '.db')) categories_yaml = load_yaml(os.path.join('sde', 'fsd', 'categoryIDs.yaml')) groups_yaml = load_yaml(os.path.join('sde', 'fsd', 'groupIDs.yaml')) types_yaml = load_yaml(os.path.join('sde', 'fsd', 'typeIDs.yaml')) category_ids = [] group_ids = [] def id_(key): try: value = items[key] except KeyError: value = 0 return value def locale(key): try: value = items[key][lcid] except KeyError: value = '' return value.strip() for i, items in categories_yaml.items(): name = items['name']['en'] if name not in CATEGORY_NAMES: continue category_ids.append(i) for lcid, session in sessions.items(): session.add(Category(id=i, name=locale('name'))) for i, items in groups_yaml.items(): category_id = id_('categoryID') if category_id not in category_ids: continue group_ids.append(i) for lcid, session in sessions.items(): session.add( Group(id=i, categoryID=category_id, name=locale('name'))) for i, items in types_yaml.items(): group_id = id_('groupID') if group_id not in group_ids: continue for lcid, session in sessions.items(): session.add( Type(id=i, groupID=group_id, name=locale('name'), description=locale('description'))) for lcid, session in sessions.items(): session.add(Information(lcid=lcid, name=LCID[lcid])) session.commit()
from db import Ad, Category, Author cat = Category.get(Category.id == 4) for ad in cat.ads: print(ad.name, ad.price)