Exemple #1
0
def addCategory(request):
    if request.method=='POST':
        name = request.POST['name']
        slug = request.POST['slug']
        desc = request.POST['desc']
        type = request.POST['type']
        if type and type == 'add':
            try:
                cats=Category.objects.filter(name=name)
                if cats.count() >= 1:
                    messages.add_message(request,messages.INFO,'categry [%s] already exits!'%(name))
                else:
                    cat = Category(name=name,slug=slug,desc=desc)
                    cat.save()
                    messages.add_message(request,messages.INFO,'categry [%s] save ok!'%(name))
            except:
                pass
                
        elif type and type == 'edit':
            id = request.POST.get('id','')
            cat = Category.objects.get(id=id)
            cat.name=name
            cat.slug=slug
            cat.desc=desc
            cat.save()
        return HttpResponseRedirect('/admin/categories')
Exemple #2
0
def addCategory(request):
    name = request.POST['name']
    slug = request.POST['slug']
    desc = request.POST['desc']
    type = request.POST['type']
    pid = request.POST['category_parent']
    if pid == '0':
        pid = None
    if type and type == 'add':
        try:
            cats=Category.objects.filter(name=name)
            if cats.count() >= 1:
                messages.add_message(request,messages.INFO,'categry [%s] already exits!'%(name))
            else:
                cat = Category(name=name,slug=slug,desc=desc,parent_id=pid)
                cat.save()
                messages.add_message(request,messages.INFO,'categry [%s] save ok!'%(name))
        except Exception as e:
            print 'exception:',e
    elif type and type == 'edit':
        id = request.POST.get('id','')
        cat = Category.objects.get(id=id)
        cat.name=name
        cat.slug=slug
        cat.desc=desc
        cat.parent_id = pid
        cat.save()
    return HttpResponseRedirect('/admin/categories')
Exemple #3
0
 def test_creating_a_new_comment_and_saving_it_to_the_database(self):
     # start by create one Category and one post
     category = Category()
     category.name = "First"
     category.slug = "first"
     category.save()
     
     # create new post
     post = Post()
     post.category = category
     post.title = "First post"
     post.content = "Content"
     post.visable = True
     post.save()
     
     # create one comment
     comment = Comment()
     comment.name = "John"
     comment.content = "This is cool"
     comment.post = post
     
     # check save
     comment.save()
     
     # now check we can find it in the database
     all_comment_in_database = Comment.objects.all()
     self.assertEquals(len(all_comment_in_database), 1)
     only_comment_in_database = all_comment_in_database[0]
     self.assertEquals(only_comment_in_database, comment)
     
     # and check that it's saved its two attributes: name and content
     self.assertEquals(only_comment_in_database.name, comment.name)
     self.assertEquals(only_comment_in_database.content, comment.content)
Exemple #4
0
 def test_creating_a_new_post_and_saving_it_to_the_database(self):
     # start by create one Category
     category = Category()
     category.name = "First"
     category.slug = "first"
     category.save()
     
     # create new post
     post = Post()
     post.category = category
     post.title = "First post"
     post.content = "Content"
     post.visable = True
     
     # check we can save it
     post.save()
     
     # check slug
     self.assertEquals(post.slug, "first-post")
     
     # now check we can find it in the database
     all_post_in_database = Post.objects.all()
     self.assertEquals(len(all_post_in_database), 1)
     only_post_in_database = all_post_in_database[0]
     self.assertEquals(only_post_in_database, post)
     
     # check that it's saved all attributes: category, title, content, visable and generate slug
     self.assertEquals(only_post_in_database.category, category)
     self.assertEquals(only_post_in_database.title, post.title)
     self.assertEquals(only_post_in_database.content, post.content)
     self.assertEquals(only_post_in_database.visable, post.visable)
     self.assertEquals(only_post_in_database.slug, post.slug)
    def POST(self):
        def check(cate):
            parent=cate.parent_cat
            skey=cate.key()
            while parent:
                if parent.key()==skey:
                    return False
                parent=parent.parent_cat
            return True

        action=self.param("action")
        name=self.param("name")
        slug=self.param("slug")
        parentkey=self.param('parentkey')
        key=self.param('key')


        vals={'action':action,'postback':True}

        try:

                if action=='add':
                    cat= Category(name=name,slug=slug)
                    if not (name and slug):
                        raise Exception(_('Please input name and slug.'))
                    if parentkey:
                        cat.parent_cat=Category.get(parentkey)

                    cat.put()
                    self.redirect('/admin/categories')

                    #vals.update({'result':True,'msg':_('Saved ok')})
                    #self.render2('views/admin/category.html',vals)
                elif action=='edit':

                        cat=Category.get(key)
                        cat.name=name
                        cat.slug=slug
                        if not (name and slug):
                            raise Exception(_('Please input name and slug.'))
                        if parentkey:
                            cat.parent_cat=Category.get(parentkey)
                            if not check(cat):
                                raise Exception(_('A circle declaration found.'))
                        else:
                            cat.parent_cat=None
                        cat.put()
                        self.redirect('/admin/categories')

        except Exception ,e :
            if cat.is_saved():
                cates=[c for c in Category.all() if c.key()!=cat.key()]
            else:
                cates= Category.all()

            vals.update({'result':False,'msg':e.message,'category':cat,'key':key,'categories':cates})
            self.render2('views/admin/category.html',vals)
Exemple #6
0
def add_new_category(request):
    if request.method == 'POST':
        text = request.POST.get('text')
        slug = request.POST.get('slug')
    category = Category()
    category.text = text
    category.slug = slug
    category.save()

    my_dict = {'taxonomy':'category'}
    return render(request, 'backend/taxonomy.html', context=my_dict)
Exemple #7
0
    def test_create_category(self):
        category = Category()

        category.name = 'python'
        category.description = 'The python language'
        category.slug = 'python'

        category.save()

        all_categories = Category.objects.all()
        self.assertEquals(len(all_categories), 1)
        only_category = all_categories[0]
        self.assertEquals(only_category, category)

        self.assertEquals(only_category.name, 'python')
        self.assertEquals(only_category.description, 'The python language')
        self.assertEquals(only_category.slug, 'python')
Exemple #8
0
    def test_creating_a_new_category_and_saving_it_to_the_database(self):
        # start by createing a new Category object
        category = Category()
        category.name = "First"
        category.slug = "first"

        # check we can save it to the database
        category.save()

        # now check we can find it in the database again
        all_category_in_database = Category.objects.all()
        self.assertEquals(len(all_category_in_database), 1)
        only_category_in_database = all_category_in_database[0]
        self.assertEquals(only_category_in_database, category)

        # and check that it's saved its two attributes: name and slug
        self.assertEquals(only_category_in_database.name, category.name)
        self.assertEquals(only_category_in_database.slug, category.slug)
Exemple #9
0
 def test_unique_slug_generate(self):
     # start by create one Category
     category = Category()
     category.name = "First"
     category.slug = "first"
     category.save()
     
     # create new posts
     post1 = Post(category=category, title="First post", content="Content", visable=True)
     post1.save()
     
     post2 = Post(category=category, title="First post", content="Content", visable=True)
     post2.save()
     
     # check posts then it's the same title
     self.assertEquals(post1.title, post2.title)
     # and different slug
     self.assertNotEquals(post1.slug, post2.slug)
Exemple #10
0
    def test_create_category(self):
        # Create the category
        category = Category()

        # Add attributes
        category.name = 'python'
        category.description = 'The Python programming language'
        category.slug = 'python'

        # Save it
        category.save()

        # Check we can find it
        all_categories = Category.objects.all()
        self.assertEquals(len(all_categories), 1)
        only_category = all_categories[0]
        self.assertEquals(only_category, category)

        # Check attributes
        self.assertEquals(only_category.name, 'python')
        self.assertEquals(only_category.description, 'The Python programming language')
        self.assertEquals(only_category.slug, 'python')
Exemple #11
0
    def test_create_category(self):
        # Create the category
        category = Category()

        # Add attributes
        category.name = 'python'
        category.description = 'The Python programming language'
        category.slug = 'python'

        # Save it
        category.save()

        # Check we can find it
        all_categories = Category.objects.all()
        self.assertEquals(len(all_categories), 1)
        only_category = all_categories[0]
        self.assertEquals(only_category, category)

        # Check attributes
        self.assertEquals(only_category.name, 'python')
        self.assertEquals(only_category.description,
                          'The Python programming language')
        self.assertEquals(only_category.slug, 'python')