Exemplo n.º 1
0
Arquivo: views.py Projeto: ideak/kamu
def edit_item(request, item_id):
    if not request.user.is_staff:
        raise Http403

    item = Item.objects.get(pk=item_id)
    revision = item.get_latest(lang=get_language())

    if request.method == 'POST':
        form = NewRevisionForm(request.POST, instance=revision)
        args = {'form': form, 'item': item }
        if form.is_valid():
            # We want to save a new one
            form.instance.id = None
            # For now just assume that missing revision == missing content
            if revision is None:
                content = Content(item=item, language=get_language())
                content.save()
                revision = form.save(commit=False)
                revision.content = content
                revision.save()
            else:
                form.save()
            # First iteration, at least news are shown on main page
            return redirect('/');
    else:
        form = NewRevisionForm(instance=revision)
        args = {'revision': revision, 'item': item, 'form': form}

    return render_to_response('edit_item.html', args,
                              context_instance=RequestContext(request))
Exemplo n.º 2
0
def process_file(root, filename, category_name, mu_type):
    print("Processing file %s" % os.path.join(root, filename))

    #Special case for newsitems
    if filename.startswith("news"):
        (category_name, newsdate, order) = filename.split("_")

    category = Category.objects.filter(name=category_name)

    if category.count() == 0:
        print("Creating category %s" % category_name)
        category = Category(name=category_name)
        category.save()
    else:
        assert (category.count() == 1)
        category = category[0]

    if category_name == "news":
        print("Processing newsitem with date %s" % newsdate)
        item = Newsitem.objects.filter(category=category, date=newsdate)
        # FIXME: Many newsitems per date
        if not item.count():
            item = Newsitem(category=category, date=newsdate)
            item.save()
        else:
            assert (item.count() == 1)
            item = item[0]
    else:
        item = Item.objects.filter(category=category)
        if item.count() == 0:
            print("Creating_item under category %s" % category_name)
            item = Item(category=category)
            item.save()
        else:
            item = item[0]

    full_fname = os.path.join(root, filename)

    content = Content.objects.filter(item=item, language=language)
    if content.count() == 0:
        print("Creating content for item %s with lang %s" % (item, language))
        content = Content(item=item, language=language)
        content.save()
    else:
        content = content[0]
        revision = content.get_latest_revision()
        mtime = datetime.fromtimestamp(os.path.getmtime(full_fname))
        if revision and revision.date >= mtime:
            print("\tSkipping based on file mtime")
            return

    f = codecs.open(full_fname, mode="r", encoding="utf8")

    if category_name == "news":
        # Newsfiles contain the subject as first line
        subject = f.readline()[:-1]
        # Summary is defined as being all the text after subject until
        # marker "[full]" alone at the beginning of line with LF at end
        summary = ""
        for line in f:
            if line == "[full]\n":
                break
            summary += line
    else:
        subject = "Initial commit for %s in %s" % (category, language)
        summary = ""

    data = f.read()
    content_data = data
    while data:
        data = f.read()
        content_data += data
    content_data = content_data.strip()

    revision = Revision(content=content,
                        subject=subject,
                        summary=summary,
                        summary_markup_type=mu_type,
                        data=content_data,
                        data_markup_type=mu_type)
    revision.save()
    print("Prepared revision %s" % revision)
Exemplo n.º 3
0
def process_file(root, filename, category_name, mu_type):
    print("Processing file %s" % os.path.join(root, filename))

    #Special case for newsitems
    if filename.startswith("news"):
        (category_name, newsdate, order) = filename.split("_")

    category = Category.objects.filter(name=category_name)

    if category.count() == 0:
        print("Creating category %s" % category_name)
        category = Category(name=category_name)
        category.save()
    else:
        assert(category.count() == 1)
        category = category[0]

    if category_name == "news":
        print("Processing newsitem with date %s" % newsdate)
        item = Newsitem.objects.filter(category=category, date=newsdate)
        # FIXME: Many newsitems per date
        if not item.count():
            item = Newsitem(category=category, date=newsdate)
            item.save()
        else:
            assert(item.count() == 1)
            item = item[0]
    else:
        item = Item.objects.filter(category=category)
        if item.count() == 0:
            print("Creating_item under category %s" % category_name)
            item = Item(category=category)
            item.save()
        else:
            item = item[0]

    full_fname = os.path.join(root, filename)

    content = Content.objects.filter(item=item, language=language)
    if content.count() == 0:
        print("Creating content for item %s with lang %s" % (item, language))
        content = Content(item=item, language=language)
        content.save()
    else:
        content = content[0]
        revision = content.get_latest_revision()
        mtime = datetime.fromtimestamp(os.path.getmtime(full_fname))
        if revision and revision.date >= mtime:
            print("\tSkipping based on file mtime")
            return

    f = codecs.open(full_fname, mode="r", encoding="utf8")

    if category_name == "news":
        # Newsfiles contain the subject as first line
        subject = f.readline()[:-1]
        # Summary is defined as being all the text after subject until
        # marker "[full]" alone at the beginning of line with LF at end
        summary = ""
        for line in f:
            if line == "[full]\n":
                break
            summary += line
    else:
        subject = "Initial commit for %s in %s" % (category, language)
        summary = ""

    data = f.read()
    content_data = data
    while data:
        data = f.read()
        content_data += data
    content_data = content_data.strip()

    revision = Revision(content=content, subject=subject, summary=summary,
                        summary_markup_type=mu_type, data=content_data,
                        data_markup_type=mu_type)
    revision.save()
    print("Prepared revision %s" % revision)