Exemple #1
0
def add_cat(no, name, level, views, likes, description='', url=''):
    # c = Category.objects.filter(Q(name=name) | Q(name=(name+" *")))
    c = Category.objects.filter(no=no)

    if c.count() > 0:
        # c = Category.objects.get(Q(name=name) | Q(name=(name+" *")))
        c = Category.objects.get(no=no)

        if c.subject == Subject.objects.get(slug="cse-postgraduate"):
            c.subject = Subject.objects.get(slug="both")
            c.save()
    else:
        c = Category()

        c.name = name
        c.no = no
        c.level = level
        c.views = views
        c.likes = likes
        c.description = description
        c.url = url
        c.subject = Subject.objects.get(slug="cse-undergraduate")

        c.save()

    return c
Exemple #2
0
	def test_slug_line_creation(self):
		"""
		slug_line_creation checks to make sure that when we add
		a category an appropriate slug line is created
		i.e. "Random Category String" -> "random-category-string"
		"""
		cat = Category()
		cat.name = "Random Category String"
		cat.save()
		self.assertEqual(cat.slug, 'random-category-string')
def add_category(request):
    form = CategoryForm()
    if request.method == 'POST':
        form = CategoryForm(request.POST)
        if form.is_valid():
            cat = Category()
            cat.name = form.cleaned_data['name']
            cat.save()
            messages.success(request, 'Category '+cat.name+' successfully included! ')
            return redirect('index')
        else:
            messages.error(request, 'Category not included, invalid form!')
            print(form.errors)
    return render(request, 'rango/add_category.html', {'form': form})
def ajax(request):

    if request.method == "POST":
        category = Category()
        category.name = request.POST["name"]
        category.save()

    category_list = list(Category.objects.all())

    test_list = []
    for ii in category_list:
        test_list.append({
            "name":ii.name,
            })

    return HttpResponse(dumps(test_list), content_type='application/json')