コード例 #1
0
ファイル: category.py プロジェクト: cocox/django-lfs
def add_category(request, category_id="", template_name="manage/category/add_category.html"):
    """Provides an add form and adds a new category to category with given id.
    """
    if category_id == "":
        parent = None
    else:
        try:
            parent = Category.objects.get(pk=category_id)
        except ObjectDoesNotExist:
            parent = None

    if request.method == "POST":
        form = CategoryAddForm(data=request.POST)
        if form.is_valid():
            new_category = form.save(commit=False)
            new_category.parent = parent
            new_category.position = 999
            if parent:
                new_category.level = parent.level + 1
            new_category.save()

            # Update positions
            manage_utils.update_category_positions(parent)
            url = reverse("lfs_manage_category", kwargs={"category_id": new_category.id})
            return HttpResponseRedirect(url)
    else:
        form = CategoryAddForm(initial={"parent": category_id})

    return render_to_response(template_name, RequestContext(request, {
        "next": request.REQUEST.get("next", request.META.get("HTTP_REFERER")),
        "category": parent,
        "form": form,
    }))
コード例 #2
0
ファイル: category.py プロジェクト: django-lfs/lfs
def update_category(request, category_id):
    """Updates category with given id. This is called after a position change
    has been taken place.
    """
    try:
        if category_id == "":
            parent_category = None
        else:
            parent_category = Category.objects.get(pk=category_id)
    except ObjectDoesNotExist:
        pass
    else:
        sorted_category_ids = request.POST.getlist("categories")
        for i, category_id in enumerate(sorted_category_ids):
            if category_id == "":
                continue
            try:
                id = category_id.split("_")[1]
                category = Category.objects.get(pk=id)
            except ValueError, ObjectDoesNotExist:
                continue
            else:
                category.parent = parent_category
                category.position = i
                category.save()
        
        if parent_category is not None:
            manage_utils.update_category_positions(parent_category)
コード例 #3
0
ファイル: category.py プロジェクト: cocox/django-lfs
def delete_category(request, id):
    """Deletes category with given id.
    """
    category = lfs_get_object_or_404(Category, pk=id)
    parent = category.parent
    category.delete()
    manage_utils.update_category_positions(parent)

    url = reverse("lfs_manage_categories")
    return HttpResponseRedirect(url)
コード例 #4
0
ファイル: category.py プロジェクト: IsmaelMT/django-lfs
def delete_category(request, id):
    """Deletes category with given id.
    """
    category = lfs_get_object_or_404(Category, pk=id)
    parent = category.parent

    # Delete the property group related to this category
    PropertyGroup.objects.filter(name=category.name).delete()

    category.delete()
    manage_utils.update_category_positions(parent)

    url = reverse("lfs_manage_categories")
    return HttpResponseRedirect(url)
コード例 #5
0
ファイル: category.py プロジェクト: IsmaelMT/django-lfs
def add_category(request, category_id="", template_name="manage/category/add_category.html"):
    """Provides an add form and adds a new category to category with given id.
       Also creates a property group for the respective category.
    """
    if category_id == "":
        parent = None
    else:
        try:
            parent = Category.objects.get(pk=category_id)
        except ObjectDoesNotExist:
            parent = None

    if request.method == "POST":
        form = CategoryAddForm(data=request.POST)
        if form.is_valid():
            new_category = form.save(commit=False)
            new_category.parent = parent
            new_category.position = 999
            if parent:
                new_category.level = parent.level + 1
            new_category.save()

            # TODO: Me quede en como actualizar las propiedades de las categorias padre
            # Create the respective property group
            group = PropertyGroup.objects.create(
                        name=new_category.name,
                    )
            group.categories.add(new_category)
            # If category has a parent, then we should
            # assign the properties of the parent category.
            if parent:
                parent_groups = PropertyGroup.objects.filter(categories=parent)

                for group in parent_groups:
                    group.categories.add(new_category)

            # Update positions
            manage_utils.update_category_positions(parent)
            url = reverse("lfs_manage_category", kwargs={"category_id": new_category.id})
            return HttpResponseRedirect(url)
    else:
        form = CategoryAddForm(initial={"parent": category_id})

    return render_to_response(template_name, RequestContext(request, {
        "category": parent,
        "form": form,
        "came_from": request.REQUEST.get("came_from", reverse("lfs_manage_categories")),
    }))