Ejemplo n.º 1
0
def create_new_category(name):
    verify_category_does_not_exists(name)

    new_category = Category()
    new_category.type = name
    save(new_category)

    return get_response({'category': name})
Ejemplo n.º 2
0
def add_category(category):
    '''Add a category from a dict'''

    if db.session.query(Category.id).filter_by(name=category['name']).scalar():
        LOGGER.warning("Category %s already exists", category['name'])
        return

    record = Category(**category)
    db.session.add(record)
    db.session.commit()
    LOGGER.info("Loaded %s", category['name'])
Ejemplo n.º 3
0
    def add_data_to_database(self, categories_and_questions):
        with self.app.app_context():
            self.db = SQLAlchemy()
            self.db.init_app(self.app)

            for i in range(len(categories_and_questions)):
                cat = Category()
                cat.type = "Category" + str(i+1)
                self.db.session.add(cat)
                self.db.session.commit()
                for j in range(categories_and_questions[i]):
                    question = Question()
                    question.question = "Question" + str(i) + "." + str(j)
                    question.answer = "Answer" + str(i) + "." + str(j)
                    question.category_id = cat.id
                    question.difficulty = 1
                    question.answer_attempt_count = 0
                    question.answer_success_count = 0
                    self.db.session.add(question)
            self.db.session.commit()
Ejemplo n.º 4
0
def edit():
    form = PostForm()
    p = Category()
    if request.method == "POST":
        p.title = form.title.data
        p.content = form.text.data
        p.user = current_user.id
        p.update_time = datetime.datetime.utcnow()
        db.session.add(p)
        db.session.commit()
        for _user in current_user.followers:
            info = Information()
            info.launch_id = current_user.id
            info.receive_id = _user.follower_id
            info.time = datetime.datetime.utcnow()
            db.session.add(info)
            db.session.flush()
            info.info = u"您关注的用户 " + current_user.username + u" 发表了新的文章 " + u"<a style='color: #d82433' " \
                u"href='{}?check={}'>{}</a>".format(u"/display/" + str(p.id), info.id, p.title) + u"。"
        t = threading.Thread(target=work,
                             args=(str(p.id), p.content.encode("utf-8")))
        t.start()
        db.session.commit()

        flash(u'保存成功!', 'success')
        return redirect(url_for('main.edit'))
    return render_template('edit.html', form=form)
Ejemplo n.º 5
0
 def test_model_repr(self):
     self.assertTrue(str(Category(name='Test')) == 'Test')
     self.assertTrue(str(TermStatus(status='Test')) == 'Test')
     self.assertTrue(
         str(Link(text='Test', address="http://x.com")) == 'Test')
     self.assertTrue(str(Location(name='Test')) == 'Test')
     self.assertTrue(str(Link(text='Test')) == 'Test')
     self.assertTrue(str(Column(name='Test')) == 'Test')
     self.assertTrue(str(Rule(name='Test')) == 'Test')
     self.assertTrue(str(Document(name='Test')) == 'Test')
     self.assertTrue(str(DocumentType(type='Test')) == 'Test')
     self.assertTrue(str(DocumentType(type='Test')) == 'Test')
     self.assertTrue(str(Table(name='Test')) == 'Test')
Ejemplo n.º 6
0
def load_categories(file_name):
    '''
    Load categories from CSV file

    :param file_Name: The name of the CSV file to load from
    '''
    LOGGER.info("Loading categories")

    with open(file_name, 'rt') as csvfile:
        reader = csv.DictReader(csvfile, delimiter=',')
        for row in reader:
            LOGGER.info("Loaded %s", row['category'])

            record = Category(**{
                'name': row['category'],
                'description': row['description']
            })
            db.session.add(record)
            db.session.commit()
Ejemplo n.º 7
0
def add_term(term):
    '''Add a term from a dict'''

    if db.session.query(Term.id).filter_by(name=term['name']).scalar():
        LOGGER.warning("Term %s already exists", term['name'])
        return

    term_to_load = remove_key(term, 'owner', 'steward', 'status', 'categories')

    # Get the objects to associate
    status = TermStatus.query.filter_by(status=term['status']).first()
    owner = Person.query.filter_by(name=term['owner']).first()
    steward = Person.query.filter_by(name=term['steward']).first()

    term_to_load['steward'] = steward
    term_to_load['owner'] = owner
    term_to_load['status'] = status

    record = Term(**term_to_load)
    LOGGER.info("Loaded term %s", term['name'])

    for category_to_associate in term['categories']:

        # Find the term to associate the rule with
        category = Category.query.filter_by(name=category_to_associate).first()

        # If the term is found associate with the rule
        if category:
            record.categories.append(category)
            LOGGER.info("Added category %s to term %s", record.name, category_to_associate)
        else:
            LOGGER.warning("Added non-existent category %s to associate with term %s",
                           category_to_associate,
                           term['name'])
            category = Category(name=category_to_associate)
            db.session.add(category)
            record.categories.append(category)


    db.session.add(record)
    db.session.commit()
Ejemplo n.º 8
0
 def test_term_category(self):
     tc = Category(name='Test', description='Test category')
     db.session.add(tc)
     db.session.commit()
     assert tc.name == 'Test'
     self.assertTrue(Category.query.filter_by(id=1).first().name == 'Test')
Ejemplo n.º 9
0
                email=email,
                phone_number=phone_number,
                address=address)
    user.set_password(password)
    access_level = input('Access level: ')
    access_level = int(access_level)
    if access_level == 2:
        user.set_mod()
    elif access_level == 3:
        user.set_admin()
    else:
        print('Normal user')
    db.session.add(user)
    db.session.commit()
elif int(choice) == 1:
    category_list = ['Thời trang', 'Mỹ phẩm', 'Thiết kế', 'Khác']
    for category in category_list:
        new_category = Category(name=category)
        db.session.add(new_category)
        db.session.commit()
    print('Đã thêm category mẫu')
elif int(choice) == 3:
    order_status_list = [
        'Chờ xác nhận', 'Đã xác nhận', 'Đang tiến hành', 'Đã hoàn thành', 'Hủy'
    ]
    for order_status in order_status_list:
        new_status = OrderStatus(name=order_status)
        db.session.add(new_status)
        db.session.commit()
    print('Đã thêm status mẫu')