Beispiel #1
0
def insert_catergory():
    user = validate_cookie(request)
    responseWrapper = ResponseWrapper()
    response = any_response(request)

    if user != None:
        category = Category()

        form_data = request.form['data']
        json_data = json.loads(form_data)
        category.name = json_data['category_name']
        result = categoryDAO.insert_category(category)

        if result != None:
            responseWrapper.set_error(False)
            responseWrapper.set_data(result)
        else:
            responseWrapper.set_error(True)
            responseWrapper.set_data(["error writing category"])

    else:
        responseWrapper.set_error(True)
        responseWrapper.set_data(["User not found. Please login again"])
        response.status_code = 302

    response.data = json.dumps(responseWrapper, default=ResponseWrapper.__str__)
    response.mimetype = "application/json"
    return response
Beispiel #2
0
def test_not_should_allow_add_category_with_the_same_name():
    name_1 = Category('Romance')
    name_2 = Category('Romance')
    categories = CategoryDatabase()

    with pytest.raises(Exception):
        categories.add(name_1)
        categories.add(name_2)
Beispiel #3
0
 def get_data(self, id):
     result = self.redis.get("cat:%s" % id)
     if result:
         result = eval(result)
         category = Category()
         category.load_from_data(result)
         return category
     else:
         return None
Beispiel #4
0
 def get_child_category_list(self, category_id):
     category_list = list()
     result = self.redis.get("child_cat_list:%s" % category_id)
     if result:
         result = eval(result)
         for item in result:
             category = Category()
             category.load_from_data(item)
             category_list.append(category)
     return category_list
Beispiel #5
0
 def postCategory(self, item):
     data = Category(0, self.window.inputName.text())
     headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
     r = requests.post("http://localhost:8080/api/v1/categories",
                       data=data.toJson(),
                       headers=headers)
     print(r.status_code)
     if r.status_code == 200:
         #self.addCategory(Category(**r.json()))
         self.getCategory()
Beispiel #6
0
 def putCategory(self, item):
     data = Category(
         self.view.listview.itemWidget(item).data.id,
         self.window.inputName.text())
     headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
     r = requests.put("http://localhost:8080/api/v1/categories",
                      data=data.toJson(),
                      headers=headers)
     print(r.text)
     if r.status_code == 204:
         self.getCategory()
 def update(self):
   self.rec = Category.get(self.params.get('key'))
   self.rec.content = self.params.get('content')
   self.rec.put()
   
   self.redirect('/category/edit/' + self.params.get('key'))
   pass
Beispiel #8
0
    def _sort_categories(self, categories, product_obj):
        """Sort categories with regular expressions and criterias."""
        # For every word in the json key 'categories'
        for c in categories.split(','):
            # Getting rid of spaces
            c = sub(self._re_spaces, '', c)

            # Getting rid of dashes and underscores
            c = sub(self._re_dash, ' ', c)

            # If c look like en:/fr: or don't have more then
            # 2 caracters
            if match(self._re_header, c) or len(c) <= 2:
                # We don't register it
                continue

            else:
                c.capitalize()

                # Turn it to a Category object
                category_obj = Category(name=c)

                # Append it to the Product object which he belong to
                product_obj.belong_to.append(category_obj)

        return product_obj
def update():
    result = False
    data = dict(request.get_json())
    try:
        result = RC.update(Category(**data))
    except:
        pass
    return result
Beispiel #10
0
class Favorite(BaseModel):
    id = PrimaryKeyField()
    user = ForeignKeyField(User)
    bot = ForeignKeyField(Bot, null=True)
    custom_bot = CharField(null=True)
    date_added = DateField()

    CUSTOM_CATEGORY = Category(id=1000,
                               order=1000,
                               emojis='👤',
                               name='Others')

    @staticmethod
    def add(user, item: Bot):
        """
        :return: Tuple of (Favorite, created: Boolean)
        """
        try:
            fav = Favorite.get(Favorite.bot == item, Favorite.user == user)
            return fav, False
        except Favorite.DoesNotExist:
            fav = Favorite(user=user,
                           bot=item,
                           date_added=datetime.date.today())
            fav.save()
            return fav, True

    @staticmethod
    def select_all(user):
        user_favs = list(Favorite.select().where(Favorite.user == user))
        for n, f in enumerate(user_favs):
            try:
                if not fn.exists(f.bot):
                    bot = Bot(category=Favorite.CUSTOM_CATEGORY,
                              username=f.custom_bot,
                              approved=True,
                              date_added=datetime.date.today())
                    f.bot = bot
                    user_favs[n] = f
                if not fn.exists(f.bot.category):
                    f.bot.category = Favorite.CUSTOM_CATEGORY
            except (Bot.DoesNotExist, AttributeError):
                f.delete_instance()
        return user_favs

    @staticmethod
    def get_oldest(user):
        return Favorite.select().where(Favorite.user == user).order_by(
            Favorite.date_added).first()

    @staticmethod
    def search_by_bot(user, bot):
        fav = Favorite.select().where(
            Favorite.user == user,
            (Favorite.bot == bot or (Favorite.custom_bot != None
                                     and Favorite.custom_bot == bot))).first()
        return fav
Beispiel #11
0
 def getCategory(self):
     self.window.inputCategory.setEnabled(False)
     self.window.inputCategory.clear()
     r = requests.get("http://localhost:8080/api/v1/categories")
     if r.status_code == 200:
         self.categories = [Category(**x) for x in r.json()]
         self.window.inputCategory.addItems(
             [category.name for category in self.categories])
         self.window.inputCategory.setEnabled(True)
Beispiel #12
0
	def get_categories(self):

		collection = self.categories
		results = collection.find()

		if results != None:
			category_list = []
			for result in results:
				print result['name']
				category = Category()
				obj_id = result["_id"]
				category.id = str(obj_id)
				category.name = result["name"]
				category_list.append(category)

			return category_list
		else:
			return None
Beispiel #13
0
    def __prepare_data(self, config):
        categories = {"all": Category("All", "All projects.")}

        for category_yaml in config['categories']:
            categories[category_yaml['name']] = Category(
                category_yaml['title'], category_yaml['desc'])

        for repo_yaml in config['repositories']:
            repo = Repository(repo_yaml['url'])
            for category_yaml in repo_yaml['categories']:
                repo.categories.append(categories[category_yaml])
            # grab info from the web
            repo.fetchRepoData()
            # add repo to the referred categories
            for category in repo.categories:
                category.repositories.append(repo)
            # add repo to the category All
            categories['all'].repositories.append(repo)
Beispiel #14
0
def index():

    entries = Entry().query(None, 0, const.PAGE_SIZE * 2)
    categories = Category().query(None, 0, const.MAX_CATEGORY_SIZE)

    return render_template("index.html",
                           entries=entries,
                           categories=categories,
                           page=2,
                           cat="")
Beispiel #15
0
def edit(request):
    category_facade = facade.factory.create_category_facade()

    if request.method =="POST":
        category_id = func.get_int_param_from_post(request,'category_id')
        old_category = category_facade.get_data(category_id)
        category = Category()
        if old_category:
            category = old_category
        category.category_id = func.get_int_param_from_post(request,'category_id')
        category.category_name = func.get_str_param_from_post(request,'category_name')
        category.parent_category_id = func.get_int_param_from_post(request,'parent_category_id')
        category.root_category_id = func.get_int_param_from_post(request,'root_category_id')
        category.article_type = func.get_int_param_from_post(request,'article_type')
        category.description = func.get_str_param_from_post(request,'description')

        if old_category:
            category_facade.update(category)
        else:
            category_facade.insert(category)
        return HttpResponseRedirect('category?parent_category_id=%s'%category.parent_category_id)


    category_id = func.get_int_param_from_get(request,'category_id')
    category = category_facade.get_data(category_id)

    parent_category_id = func.get_int_param_from_get(request,'parent_category_id')
    parent_category = category_facade.get_data(parent_category_id)
    if category:
        parent_category = category_facade.get_data(category.parent_category_id)
    
    root_category_id = 0
    if parent_category:
        root_category_id = parent_category.root_category_id
    root_category = category_facade.get_data(root_category_id)

    if not category:
        category = Category()
        category.root_category_id = root_category_id
        category.parent_category_id = parent_category_id

    
    return render_to_response('admin/category_edit.html',locals())
Beispiel #16
0
 def getCategory(self):
     self.view.listview.setEnabled(False)
     self.view.listview.clear()
     try:
         r = requests.get("http://localhost:8080/api/v1/categories")
         if r.status_code == 200:
             for data in r.json():
                 self.addCategory(Category(**data))
     except:
         pass
     self.view.listview.setEnabled(True)
Beispiel #17
0
 def buy(self, cat_name, prod_name, price):
     """ Place a bid on a product
     :param cat_name: category name
     :param prod_name: product name
     :param price: value of the bid
     """
     try:
         cat = Category.search_category(self._auct._categories, cat_name)
         auct = cat.search_auction(prod_name)
         auct.bid(price=price, user=self.user)
         self.response(1)
     except UserException, e:
         self.response(5, res_msg=str(e))
Beispiel #18
0
 def unbuy(self, cat_name, prod_name):
     """ Remove our last offer only if it's the highest
     :param cat_name: category name
     :param prod_name: product name
     """
     try:
         cat = Category.search_category(self._auct._categories, cat_name)
         auct = cat.search_auction(prod_name)
         auct.unbid(self.user)
         self.response(1)
     except CategoryException, e:
         debug_print(e)
         self.response(0)
Beispiel #19
0
 def seed_categories(self):
     """Seed the  database with some products and categories"""
     cat_l = Category("libri")
     pippo = self._users['pippo']
     a = Auction("Il Signore degli anelli", 10, pippo)
     b = Auction("La Metamorfosi", 7, pippo)
     cat_l.add_auction(a)
     cat_l.add_auction(b)
     self._categories['libri'] = cat_l
     cat_v = Category("vestiti")
     c = Auction("Smoking nero usato", 500, pippo)
     d = Auction("Scarpe in pelle", 70, pippo)
     cat_v.add_auction(c)
     cat_v.add_auction(d)
     self._categories['vestiti'] = cat_v
Beispiel #20
0
 def register(self, cat_name):
     """Register a new category
     :param cat_name: category name
     """
     if not cat_name:
         self.response(0, res_msg="Invalid category name")
         return
     if cat_name in self._auct._categories:
         self.response(0, res_msg="Categoria gia' esistente")
         return
     try:
         self._auct._categories[cat_name] = Category(cat_name)
     except Exception, e:
         self.response(0, "Categoria non valida")
Beispiel #21
0
def upload():
    categories = Category().query(None, 0, const.MAX_CATEGORY_SIZE)
    if request.method == 'POST':
        file_storage = request.files['upload_img']
        file_name = file_storage.filename

        if file_name:
            ext = file_name.rsplit('.', 1)[1]
            date_dir = get_date_dir()
            uuid = get_uuid()
            name = '%s_source.%s' % (uuid, ext)
            source = uploads.save(file_storage, folder=date_dir, name=name)

            thumb_name = '%s_thumb.%s' % (uuid, ext)
            thumb = os.path.join(date_dir, thumb_name).replace('\\', '/')
            thumb_path = os.path.join(app.config["UPLOADS_DEFAULT_DEST"],
                                      "uploads/" + thumb)

            source_path = os.path.join(app.config["UPLOADS_DEFAULT_DEST"],
                                       "uploads/" + source)
            ret = resizeImg(source_path, thumb_path, ext)

            if ret["status"]:
                height = ret.get("height", 0)
                width = ret.get("width", 0)

            entry = Entry()
            document = entry.document()
            document.update({
                "title": request.form['title'],
                "link": request.form['link'],
                "categories": request.form['categories'],
                "tags": request.form['tags'],
                "description": request.form['description'],
                "source": source,
                "thumb": thumb,
                "width": width,
                "height": height
            })

            entry.insert(document)

            flash(u"恭喜你,上传图片成功了")
            return redirect(url_for("upload"))
        else:
            flash(ret["msg"])
    else:
        flash(u"亲,必须选择一张图片的哦")

    return render_template("upload.html", categories=categories)
Beispiel #22
0
    def create(self):
     try:
        categories = Category.all()
        for category in categories:
          name = self.params.get("name_%s" % category.category_id)
          id = self.params.get("category_%s" % category.category_id)
          wk = db.GqlQuery("SELECT * FROM Category WHERE category_id = :1",id).get()
          if wk != None:
            category.name=name
            category.category_id = id
            category.put()

        new = self.params['new_category']
        if new:
          id = self.params['new_category_id']
          category = Category(name=new, category_id=id,order=max)
          category.put()

        # URL '/category/' にリダイレクト
        self.redirect('/site/category/')
     except Exception, ex:
        # 例外メッセージを表示する
        self.render(text='Exception: %s' % ex)
Beispiel #23
0
 def get_category_list(self):
     body = self.page
     for li_label in body.find("div", class_="nav").find_all("li"):
         href = li_label.find("a")['href']
         if href == "/":
             continue
         text = li_label.find("a").text
         category_url = self.join_url(href)
         self.url_quene.append(category_url)
         category = Category(site=self.url,
                             text=text,
                             url=category_url,
                             short_url=href)
         self.category_list.append(category)
     return self.category_list
Beispiel #24
0
    def delete(self):

      key = self.params.get("key");

      # カテゴリに属するドキュメントがあったら削除できない
      res= {"status":"success","msg":"削除しました"}
      query = db.GqlQuery("SELECT * FROM Document WHERE category = :1",db.Key(key))
      if query.count() > 0:
        res= {"status":"error","msg":"カテゴリに属するドキュメントがあるため削除できません。"}
      else:
        category = Category.get(db.Key(key));
        if category:
          category.delete()
          # リナンバー
          query = Category.all();
          num = 1;
          for c in query:
            c.order = num
            c.put()
            num = num +1
        else:
          res= {"status":"error","msg":"削除に失敗しました"}

      self.render(json=self.to_json(res))
Beispiel #25
0
def addCategory():
    # html文件修改为新建题目的文件
    if request.method == "GET":
        return render_template("提交分类.html")
    elif request.method == "POST":
        req = request.values
        # 暂时略过合法性检测
        categoryName = req['categoryName']
        categoryNameD = Category.query.filter_by(
            categoryName=categoryName).first()
        if categoryNameD:
            return ops_renderErrJSON(msg="相同分类已存在,请再换一个试试")
        # 注册写入数据库
        model_category = Category()
        model_category.categoryName = categoryName
        db.session.add(model_category)
        db.session.commit()
        # json化data
        temp = {}
        temp["categoryName"] = categoryName
        data = []
        data.append(temp)
        return ops_renderJSON(msg="添加成功", data=data)
    return "添加成功"
Beispiel #26
0
def init_db(db_session: Session):
    # Tables should be created with Alembic migrations
    # But if you don't want to use migrations, create
    # the tables un-commenting the next line
    # Base.metadata.create_all(bind=engine)
    # user
    user = user_service.get_by_email(db_session, email='*****@*****.**')
    if not user:
        user = User(
            email='*****@*****.**',
            hashed_password=get_password_hash('admin'),
            name='admin',
            is_superuser=True,
        )
        db_session.add(user)
        db_session.commit()

    with open('data.csv') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        line = 0
        for row in csv_reader:
            print(f'--- {line} {row[0]} {row[1]} {row[5]} {row[6]}')
            try:
                product = Product()
                product.upc = row[0]
                product.name = row[1]
                if row[5]:
                    category = db_session.query(Category).filter(Category.name == row[5]).first()
                    if not category:
                        category = Category(name=row[5])
                        db_session.add(category)
                        db_session.commit()
                    product.category_id = category.id

                if row[6]:
                    department = db_session.query(Department).filter(Department.name == row[6]).first()
                    if not department:
                        department = Department(name=row[6])
                        db_session.add(department)
                        db_session.commit()
                    product.department_id = department.id

                db_session.add(product)
                db_session.commit()
            except Exception as e:
                print(e)
                db_session.rollback()
            line += 1
Beispiel #27
0
 def close_bid(self, cat_name, prod_name):
     """ Close a bid
     :param cat_name: category name
     :param prod_name: product name
     """
     print "Closing bid " + prod_name
     try:
         cat = Category.search_category(self._auct._categories, cat_name)
         auct = cat.search_auction(prod_name)
         auct.close(self.user)
         cat.del_auction(auct)
         self.response(1)
     except CategoryException, e:
         # Not matching category
         error_print(e)
         self.response(0)
Beispiel #28
0
 def sell(self, cat_name, prod_name, price):
     """ Add a new product on the server
     :param cat_name: category name
     :param prod_name: product name
     :param price: base price for the auction
     """
     print "Selling: "
     print price
     try:
         cat = Category.search_category(self._auct._categories, cat_name)
         a = Auction(prod_name, price, self.user)
         cat.add_auction(a)
         self.response(1)
     except CategoryException, e:
         debug_print(e)
         self.response(0, res_msg=str(e))
    def convert_to_category(categories: Dict) -> List[Category]:
        """
        Convert list of dictionnaries to list of Category.

        :param categories: list containing dictionnaries
        :return: List[Category]
        """
        category_list: List[Category] = []

        for category in categories:
            if (category.get('name') and category.get('url')
                    and category.get('id')
                    and category.get('products') > 1000):
                category_list.append(
                    Category(name=category['name'],
                             off_id=category['id'],
                             url=category['url']))

        return category_list
Beispiel #30
0
class Recruit(Schema):
    description = fields.Str()
    category = fields.Function(lambda f: Category.of(f['category']).title)
    limit_date = fields.Function(
        lambda f: dt.strptime(f['limit_date'], '%Y-%m-%d').date())
    link = fields.Url()
    minimum_career = fields.Function(
        lambda f: Career.of(f['minimum_career']).title)
    minimum_qualifications = fields.List(fields.Str())
    preferred_qualifications = fields.List(fields.Str())
    is_tomorrows_limit = fields.Method('calculate_is_tomorrows_limit')
    is_outdated = fields.Method('calculate_is_outdated')

    def calculate_is_tomorrows_limit(self, recruit):
        limit_date = dt.strptime(recruit['limit_date'], '%Y-%m-%d').date()
        tomorrow = date.today() + timedelta(days=1)
        return tomorrow == limit_date

    def calculate_is_outdated(self, recruit):
        limit_date = dt.strptime(recruit['limit_date'], '%Y-%m-%d').date()
        return limit_date < date.today()
 def category(self, category: Category):
     self.__category = None
     if category.isValid():
         self.__category = category
Beispiel #32
0
 def testInsertEntity(self):
   category = Category()
   category.put()
   self.assertEqual(1, len(Category.query().fetch(2)))
Beispiel #33
0
 def edit(self):
   self.rec = Category.get(self.params.get('id'))
Beispiel #34
0
def evaluate(args):
    _input_path = args.input
    _n = args.n
    _ans_mode = args.ans_mode if args.ans_mode else "single"
    _delimiter = args.delimiter if args.delimiter else "\t"
    _graph = args.graph if args.graph else "n"

    prediction_data_list = []
    ans_flag_list = []
    ans_list = []

    # store all category's data
    category_dict = {}

    f = open(_input_path, "r")
    lines = f.readlines()
    for line in lines:
        _line = line.strip()

        tokens = [x.strip() for x in _line.split(_delimiter)]

        if len(tokens) != 2 and len(tokens) != 3:
            raise BaseException("Wrong input format")

        answer = tokens[0]
        candidates = [x.strip() for x in tokens[1].split(",")]

        if _ans_mode == "single":
            ans_dict = {answer: 1}
            category_dict[answer] = Category(answer)
        else:
            ans_dict = get_multi_ans_dict(answer)
            for category in ans_dict:
                category_dict[category] = Category(category)

        ans_list.append(ans_dict)

        _tmp_predict_data = []
        _tmp_ans_flag = []

        for candi in candidates:
            _tmp_predict_data.append(candi)
            if candi in ans_dict:
                _tmp_ans_flag.append(ans_dict[candi])
            else:
                _tmp_ans_flag.append(0)

        prediction_data_list.append(_tmp_predict_data)
        ans_flag_list.append(_tmp_ans_flag)

    # sum answer categories weight
    category_dict = sum_each_answer_categories(category_dict, ans_list)
    
    # list for graph
    macro_precision_list = []
    micro_precision_list = []
    macro_recall_list = []
    micro_recall_list = []

    n_predict_data = np.transpose(np.array(prediction_data_list))
    n_ans_flag_list = np.transpose(np.array(ans_flag_list))

    for i in range(0, _n):
        data = n_predict_data[i]

        # calc tp, fp
        for j in range(0, len(data)):
            category = data[j]

            if n_ans_flag_list[i][j] > 0:
                # tp
                category_dict[category].tp += n_ans_flag_list[i][j]
            else:
                # fp
                if category not in category_dict:
                    category_dict[category] = Category(category)

                fp = 0
                _tmp_ans_dict = ans_list[j]
                for ans in _tmp_ans_dict:
                    if category != ans:
                        fp += _tmp_ans_dict[ans]

                category_dict[category].fp += fp

        # calc fn
        for category in category_dict:
            # fn = ans sum - tp
            category_dict[category].fn = category_dict[category].ans_sum - category_dict[category].tp

        # macro avg
        macro_precision, macro_recall = calc_precision_recall_macro_avg(category_dict)
        
        # micro avg
        micro_precision, micro_recall = calc_precision_recall_micro_avg(category_dict)

        # print result
        category_num = len(category_dict)
        print("------------------------------------")
        print("N: %s \t# of category set: %s" % (str(i+1), str(category_num)))
        print("\nMacro Precision P@%s: %s" % (str(i+1), str(macro_precision)))
        print("Macro Recall R@%s: %s" % (str(i+1), str(macro_recall)))
        print("Macro F1-score N=%s: %s" % (str(i+1), f1_score(macro_precision, macro_recall)))

        print("\nMicro Precision P@%s: %s" % (str(i+1), str(micro_precision)))
        print("Micro Recall R@%s: %s" % (str(i+1), str(micro_recall)))
        print("Micro F1-score N=%s: %s" % (str(i+1), f1_score(micro_precision, micro_recall)))

        # data for drawing graph
        macro_precision_list.append(macro_precision)
        micro_precision_list.append(micro_precision)
        macro_recall_list.append(macro_recall)
        micro_recall_list.append(micro_recall)
    
    if _graph == "y":
        idx = [i+1 for i in range(0, _n)]
        graph_input = [macro_precision_list, micro_precision_list, macro_recall_list, micro_recall_list]
        plt_show(idx, graph_input)
Beispiel #35
0
def add(restaurant, category_name): 
  if isinstance(restaurant, str) or isinstance(restaurant, unicode):
    restaurant = ndb.Key(Restaurant, restaurant)
  check_get_restaurant(restaurant)
  category = Category(restaurant_key = restaurant, name = category_name)
  return category.put().get()
Beispiel #36
0
def init_db():
    category = Category()
    category.save()
Beispiel #37
0
 def index(self):
     query = Category.all()
     self.result = query.fetch(limit=1000)
Beispiel #38
0
    def post(self):
        '''
            Create a category.

            **Example Request**

            ..sourcode:: json

                {
                    "categories": [
                        {
                            "name": "gender",
                            "sites": [1, 2, 7]
                        },
                        ...
                    ]
                }

        **Example Response**

        ..sourcecode:: json

            {
                "message": "2 new categories created."
            }

        :<header Content-Type: application/json
        :<header X-Auth: the client's auth token
        :>json list categories: a list of categories to create
        :>json str categories[n].name: name of category to create

        :>header Content-Type: application/json
        :>json str message: api response message

        :status 200: created
        :status 400: invalid request body
        :status 401: authentication required
        '''

        request_json = request.get_json()
        categories = list()

        # Validate input
        for category_json in request_json['categories']:
            validate_request_json(category_json, GROUP_ATTRS)

            try:
                request_site_ids = [int(s) for s in category_json['sites']]
            except TypeError:
                raise BadRequest('Sites must be integer site ids')

            if len(request_site_ids) == 0:
                raise BadRequest('At least one site is required.')

            sites = g.db.query(Site)\
                        .filter(Site.id.in_(request_site_ids))\
                        .all()
            site_ids = [site.id for site in sites]
            missing_sites = list(set(request_site_ids) - set(site_ids))

            if len(missing_sites) > 0:
                raise BadRequest('Site ids {} do not exist'.format(','.join(
                    str(s) for s in missing_sites)))

        # Create categories
        for category_json in request_json['categories']:
            try:
                category = Category(name=category_json['name'].strip(),
                                    sites=sites)
                g.db.add(category)
                g.db.flush()
                # Create dict for API JSON response
                category_dict = category.as_dict()
                # Add a link to the created category
                category_dict['url-for'] = url_for('CategoryView:get',
                                                   id_=category.id)
                categories.append(category_dict)
            except IntegrityError:
                g.db.rollback()
                raise BadRequest('Category "{}" already exists'.format(
                    category.name))

        # Save categories
        g.db.commit()

        # Send redis notifications
        for category in categories:
            notify_mask_client(channel='category',
                               message={
                                   'id': category['id'],
                                   'name': category['name'],
                                   'status': 'created',
                                   'resource': category['url-for']
                               })

        message = '{} new categories created' \
                  .format(len(request_json['categories']))
        response = jsonify(message=message, categories=categories)
        response.status_code = 200

        return response
Beispiel #39
0
 def show(self):
     r = Category.get(self.params.get('id'))
#!/usr/bin/env python3

from repository.sqlite import db_session, create_database
from model.user import User
from model.category import Category
from model.item import Item


create_database()

# Create football category and its items
football = Category(name='Football')
db_session.add(football)
db_session.commit()

description = ('Footballers wear lightweight, comfortable and durable shoes '
               'that are usually studded to provide good grip on muddy or '
               'slippery surfaces.')
shoes = Item(name='Shoes', description=description, category=football)
db_session.add(shoes)
db_session.commit()

description = ('Goalies are allowed to wear headgears during the play though '
               'it is not mandatory, but many of them opt for it as it '
               'protects them against any head injury.')
goalie_gloves = Item(name='Goalie Gloves',
                     description=description,
                     category=football)
db_session.add(goalie_gloves)
db_session.commit()
Beispiel #41
0
def search(user_id):
    result = {}

    q_arg = request.args.get("q")
    sort_arg = request.args.get("sort")
    p_arg = request.args.get("p")
    category_arg = request.args.get("category")
    location_arg = request.args.get("location")
    zone_arg = request.args.get("zone")
    area_arg = request.args.get("area")
    filter_arg = request.args.get("filter")

    category = Table("category")
    merchant = Table("merchant")
    store = Table("store")

    q = ""
    haveLocation = False

    if location_arg:
        locations = location_arg.split(",")
        try:
            locations[0] = Decimal(locations[0])
            locations[1] = Decimal(locations[1])
            q = ("SELECT *, (6371*acos(cos(radians(" + str(locations[0]) +
                 "))*cos(radians(lat))*cos(radians(`long`)-radians(" +
                 str(locations[1]) + "))+sin(radians(" + str(locations[0]) +
                 "))*sin(radians(lat)))) AS distance")
            haveLocation = True
        except (ValueError, DecimalException):
            abort(400)
    else:
        q = "SELECT *"

    q += " FROM store"

    if sort_arg:
        if str(sort_arg) == "popular":
            c = "SELECT store_id, COUNT(*) as count FROM transaction GROUP BY store_id"
            q += " LEFT JOIN (" + c + ") AS count ON count.store_id = id"
        elif str(sort_arg) == "match":
            pass

    wheres = []

    if category_arg:
        q2 = Query.from_(category).select("*").where(
            category.id == category_arg)
        cursor = get_db().cursor()
        cursor.execute(str(q2))
        record = cursor.fetchone()
        cursor.close()

        if record == None:
            abort(400)
        else:
            result["category"] = Category(record)
            q2 = (Query.from_(merchant).select(
                merchant.id).where(merchant.category_id == category_arg))
            cursor = get_db().cursor()
            cursor.execute(str(q2))
            merchants = cursor.fetchall()
            cursor.close()

            wheres.append("store.merchant_id IN (" +
                          ",".join(str(i[0]) for i in merchants) + ")")

    if q_arg:
        wheres.append("store.name LIKE '%" + q_arg + "%'")

    if zone_arg:
        wheres.append("store.zone = " + zone_arg)

    if area_arg:
        wheres.append("store.area_level_2 = '" + area_arg + "'")

    q += " WHERE " + wheres[0]

    for i in wheres[1:]:
        q += " AND " + i

    if filter_arg:
        filters = filter_arg.split(";")
        for i in filters:
            splits = i.split(",")
            if splits[0] == "distance":
                if haveLocation:
                    q += (" HAVING distance BETWEEN " + str(splits[1]) +
                          " AND " + str(splits[2]))

    if sort_arg:
        if str(sort_arg) == "popular":
            q += " ORDER BY count.count DESC"
        elif str(sort_arg) == "distance":
            if haveLocation:
                q += " ORDER BY distance ASC"

    if p_arg:
        try:
            p_arg = int(p_arg)
            q += " LIMIT " + str((p_arg - 1) * 10) + ", 10"
        except ValueError:
            return abort(400)

    cursor = get_db().cursor()
    cursor.execute(str(q))
    records = cursor.fetchall()
    cursor.close()

    newStore = lambda a: Store(a, 0)
    if not category_arg:
        newStore = lambda a: Store(a, 0)

    q = "INSERT INTO keyword(content, category, area, user_id, time, records"

    if zone_arg:
        q += ", zone"

    q += (") VALUES('" + q_arg + "', '" + category_arg + "', '" + area_arg +
          "','" + str(user_id[0]) + "'," + str(int(time.time() * 1000)) + "," +
          str(len(records)))

    if zone_arg:
        q += "," + zone_arg

    q += ")"

    cursor = get_db().cursor()
    cursor.execute(q)
    cursor.close()
    get_db().commit()

    records = list(map(newStore, records))
    result["stores"] = records
    return jsonify(**result)
Beispiel #42
0
def get_all_by_restaurant_key(restaurant_key):
  check_get_restaurant(restaurant_key)
  return Category.query(Category.restaurant_key == restaurant_key).fetch()
Beispiel #43
0
 def edit(self):
     self.categories = Category.all().order("-post_at")
     self.rec = Document.get(self.params.get("id"))
     self.pages = db.GqlQuery("SELECT * FROM Page WHERE document = :1 ORDER BY order", db.Key(self.params.get("id")))
Beispiel #44
0
def init_db():
	category = Category()
	category.save()
Beispiel #45
0
 def insert(self, category: dict) -> Category:
     category_loaded = Category(**category)
     self.session.add(category_loaded)
     self.session.commit()
     self.session.refresh(category_loaded)
     return category_loaded
Beispiel #46
0
def add(restaurant_uid, category_name, dish_name, image_key = None):
  categories = Category.query(ndb.AND(
      Category.restaurant_key == ndb.Key(Restaurant, restaurant_uid),
      Category.name == category_name)).fetch()
  "TODO: check if category exist. Throw exception if so."
  return add_by_category_key(categories[0].key, dish_name, image_key)
Beispiel #47
0
from model.apiaccess import APIAccess
from model.country import Country
from model.group import Group
from model.keywordmodel import Keyword
from model.notifications import Notifications
from model.user import User
from model.suggestion import Suggestion
from model.favorite import Favorite
from model.message import Message
from model.statistic import Statistic
from model.statistic import track_activity
from model.ping import Ping
from model.revision import Revision

if __name__ == "__main__":
    Category.create_table(fail_silently=True)
    Bot.create_table(fail_silently=True)
    Country.create_table(fail_silently=True)
    Channel.create_table(fail_silently=True)
    User.create_table(fail_silently=True)
    Suggestion.create_table(fail_silently=True)
    Group.create_table(fail_silently=True)
    Notifications.create_table(fail_silently=True)
    Keyword.create_table(fail_silently=True)
    Favorite.create_table(fail_silently=True)
    APIAccess.create_table(fail_silently=True)

    APIAccess.insert({
        'user': User.get(User.username == 'Bfaschatsbot'),
        'token': '474609801:AAFrSFYP9YXPFa5OmQReEjTn6Rs44XQVuDM',
    }).execute()