Exemplo n.º 1
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)
Exemplo n.º 2
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
Exemplo n.º 3
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
Exemplo n.º 5
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
Exemplo n.º 6
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)
Exemplo n.º 7
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)
Exemplo n.º 8
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="")
Exemplo n.º 9
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()
Exemplo n.º 10
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()
Exemplo n.º 11
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)
Exemplo n.º 12
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")
Exemplo n.º 13
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)
Exemplo n.º 14
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
Exemplo n.º 15
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
Exemplo n.º 16
0
    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
Exemplo n.º 17
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 "添加成功"
Exemplo n.º 18
0
import json

import mysql.connector
import requests

from constantes import IP,USER,PASSWORD,CREATE_CATEGORY,CREATE_PRODUIT,CREATE_FAVORI,DELETE_DOUBLONS,CREATE_TABSTORE,\
    CREATE_POSSESION
from model.category import Category
from model.produit import Produit

dic = {
    0: Category("1", "Produits à tartiner"),
    1: Category("2", "Plats préparés"),
    2: Category("3", "Céréales pour petit-déjeuner"),
    3: Category("4", "Pizzas"),
    4: Category("5", "Confiseries"),
    5: Category("6", "Boissons")
}


def drop_data():
    """ delete databasse """
    cnx = mysql.connector.connect(user=USER,
                                  password=PASSWORD,
                                  host=IP,
                                  database='')
    cur = cnx.cursor()
    cur.execute("DROP DATABASE pur_beurre")
    cnx.close()

Exemplo n.º 19
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)
Exemplo n.º 20
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
Exemplo n.º 21
0
def init_db():
    category = Category()
    category.save()
Exemplo n.º 22
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
Exemplo n.º 23
0
def test_not_should_allow_add_name_in_blank():
    name = ''
    with pytest.raises(Exception):
        Category(name)
Exemplo n.º 24
0
#!/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()
Exemplo n.º 25
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)
Exemplo n.º 26
0
def post(db_session: Session, request: CreateCategory) -> Optional[Category]:
    category = Category(name=request.name, description=request.description)
    db_session.add(category)
    db_session.commit()
    return category