Beispiel #1
0
def menus_edit_item(request, item_id):
    if user_has_access_to_menus(request.user):
        item = ZornaMenuItem.objects.get(pk=item_id)
        if request.method == 'POST':
            if request.POST.has_key('bdelete'):
                item.delete()
                return HttpResponseRedirect(reverse('menus_home_view'))
            if request.FILES or 'image-clear' in request.POST:
                try:
                    shutil.rmtree(u"%s/m%s" % (get_upload_menus(), item.pk))
                except Exception, e:
                    print e

            if request.POST.has_key('childs_ids') and request.POST['childs_ids']:
                ids = request.POST['childs_ids'].split(',')
                last_m = ZornaMenuItem.objects.get(pk=ids.pop(0))
                last_m.move_to(last_m.parent, position='first-child')
                for id in ids:
                    last_m = ZornaMenuItem.objects.get(pk=last_m.id)
                    m = ZornaMenuItem.objects.get(pk=id)
                    m.move_to(last_m, position='right')
                    last_m = m

        if item.object_id:
            if item.content_type == ContentType.objects.get(model='articlecategory'):
                return menus_edit_item_article_category(request, item)
            elif item.content_type == ContentType.objects.get(model='faq'):
                return menus_edit_item_faq(request, item)
            elif item.content_type == ContentType.objects.get(model='formsform'):
                return menus_edit_item_form(request, item)
        else:
            return menus_edit_item_url(request, item)
Beispiel #2
0
def save_form(request, form):
    if 'image' in request.FILES:
        image_file = request.FILES['image']
    else:
        image_file = None

    if image_file:
        item = form.save()
        upload_path = get_upload_menus()
        path_src = u"%s/%s" % (upload_path, item.image)
        filehead, filename = os.path.split(path_src)
        s = os.path.splitext(filename)
        filename = u"%s%s" % (slugify(s[0]), s[1])
        path_dest = u"%s/m%s" % (upload_path, item.pk)
        os.makedirs(path_dest)
        shutil.move(path_src, path_dest+"/%s" % filename)
        item.image = "m%s/%s" % (item.pk, filename)
    else:
        item = form.save(commit=False)
    return item
Beispiel #3
0
import os
from django.conf import settings
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
from django.core.files.storage import FileSystemStorage
from django.template.defaultfilters import slugify

from mptt.models import MPTTModel

from zorna.utilit import get_upload_menus

fs = FileSystemStorage(location=get_upload_menus(), base_url='/images/menus/')


def get_menu_image_filepath(instance, filename):
    s = os.path.splitext(filename)
    filename = u"%s%s" % (slugify(s[0]), s[1])
    return os.path.join(u"m%s/%s" % (str(instance.pk), filename))


class ZornaMenuItem(MPTTModel):
    parent = models.ForeignKey(
        'self', null=True, blank=True, related_name='children')
    name = models.CharField(_(u'name'), max_length=255, default='')
    slug = models.SlugField(_('slug'), max_length=255, unique=True)
    title = models.CharField(_(u'title'), max_length=255, default='',
                             help_text=_(u'Shown when hovering over the menu link'), blank=True)
    image = models.ImageField(
        storage=fs, max_length=1024, upload_to=get_menu_image_filepath, blank=True, default='')