Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
*.swp
.svn
*.pyc
.idea/*
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Parts of reusable content across your site. Usable for validation meta codes,
analytics codes, phone numbers, addresses and so on.
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
recursive-include chunks *
global-exclude *.orig *.pyc
81 changes: 61 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,75 @@
Think of it as flatpages for small bits of reusable content you might want to insert into your templates and manage from the admin interface.
# Django chunks documentation

## Preface

Think of it as flatpages for small bits of reusable content you might want to insert into your templates and manage from the admin interface.
This is really nothing more than a model and a template tag.

By adding `chunks` to your installed apps list in your Django project and performing a `./manage.py syncdb`, you'll be able to add as many "keyed" bits of content chunks to your site.

The idea here is that you can create a chunk of content, name it with a unique key (for example: `home_page_left_bottom`) and then you can call this content from a normal template.

### Why would anyone want this? ###
### Why would anyone want this?

Well it essentially allows someone to define "chunks" (I had wanted to call it blocks, but that would be very confusing for obvious reasons) of content in your template that can be directly edited from the awesome Django admin interface. Throwing a rich text editor control on top of it make it even easier.

### Usage: ###
## Installation and basic usage

1. Install package

`` pip install git+git://github.com/shoker174/django-chunks.git``

2. Configure your settings file:

```
INSTALLED_APPS += ['chunks']
```
3. Call chunks in the html template

{% load chunks %}
Usage example №1:
``` html
{% load chunks_tags %}
<html>
<head>
<title>Test</title>
</head>
<body>
<h1> Blah blah blah</h1>
<div id="sidebar">
...
</div>
<div id="left">
{% chunk "home_page_left" %}
</div>
<div id="right">
{% chunk "home_page_right" %}
</div>
</body>
...
{% chunk "phone" %}
...
</html>
```
Usage example №2:
``` html
{% load chunks_tags %}
<html>
...
{% get_chunk "phone" as phone %}
{% if phone.content %}
<span>{{ phone.content }}</span>
{% endif %}
...
</html>
4. Apply migrations and run local server

```python
python manage.py migrate chunks
python manage.py runserver
```
5. Create chunks in admin interface

## Advansed usage
In many cases you may need in many chunks. Basic usage examples generate one request to database for each chunk -
this is a bad idea for large projects. For large progects your must use cached chunks.

This is really helpful in those cases where you want to use `django.contrib.flatpages` but you need multiple content areas. I hope this is helpful to people and I'll be making minor edits as I see them necessary.
1. Add context processor to settings.py

```python
TEMPLATES[0]['OPTIONS']['context_processors'] += ['chunks.context_processors.chunks_processor']
```
2. Call chunks in the html template
```html
<html>
...
{% if chunks.phone %}
<span>{{ chunks.phone.content }}</span>
{% endif %}
...
</html>
```
58 changes: 0 additions & 58 deletions USAGE

This file was deleted.

2 changes: 2 additions & 0 deletions chunks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__version__ = '2.2'
default_app_config = 'chunks.apps.ChunksAppConfig'
16 changes: 12 additions & 4 deletions chunks/admin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
from django.utils.translation import ugettext_lazy as _
from django.contrib import admin
from models import Chunk
from .models import Chunk


class ChunkAdmin(admin.ModelAdmin):
list_display = ('key',)
search_fields = ('key', 'content')

admin.site.register(Chunk, ChunkAdmin)
def get_content(self, obj):
return obj.content[0:50]

get_content.short_description = _('content')
list_display = ('key', 'description', 'get_content')
search_fields = ('key', 'content')


admin.site.register(Chunk, ChunkAdmin)
13 changes: 13 additions & 0 deletions chunks/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.apps import AppConfig
from django.utils.translation import ugettext_lazy as _


class ChunksAppConfig(AppConfig):
name = 'chunks'
verbose_name = _('Chunk')

class Meta:
app_label = 'chunks'

def ready(self):
from . import signals
33 changes: 33 additions & 0 deletions chunks/context_processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import json
from django.core.cache import cache
from django.utils.html import mark_safe
from .models import Chunk

CACHE_KEY = 'chunks'


def get_chunks_data():
result = {}
for chunk in Chunk.objects.all():
result[chunk.key] = {
'content': chunk.content,
'file': chunk.file.url if chunk.file else None
}
return result


def chunks_processor(request):
chunks_json = cache.get(CACHE_KEY)
if chunks_json:
raw_chunks = json.loads(chunks_json)
else:
raw_chunks = get_chunks_data()
cache.set(CACHE_KEY, json.dumps(raw_chunks, ensure_ascii=False))

chunks = {}
for key, val in raw_chunks.items():
chunks[key] = {
'content': mark_safe(val['content']),
'file': val['file']
}
return {'chunks': chunks}
Binary file added chunks/locale/ru/LC_MESSAGES/django.mo
Binary file not shown.
49 changes: 49 additions & 0 deletions chunks/locale/ru/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: chunks\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2015-04-16 14:37+0500\n"
"PO-Revision-Date: 2015-04-16 18:28+0500\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: ru\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n"
"%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n"
"X-Generator: Poedit 1.5.4\n"
"X-Poedit-SourceCharset: UTF-8\n"

#: models.py:11
msgid "Key"
msgstr "Название"

#: models.py:15
msgid "File or image"
msgstr "Файл или изображение"

#: models.py:11
msgid "A unique name for this chunk of content"
msgstr "Название должно быть уникальным"

#: models.py:12
msgid "Content"
msgstr "Содержание"

#: models.py:13
msgid "Description"
msgstr "Описание"

#: models.py:19
msgid "Chunk"
msgstr "Фрагмент"

#: models.py:20
msgid "Chunks"
msgstr "Фрагменты"
28 changes: 28 additions & 0 deletions chunks/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.20 on 2019-05-29 17:34
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Chunk',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('key', models.CharField(help_text='A unique name for this chunk of content', max_length=255, unique=True, verbose_name='Key')),
('content', models.TextField(blank=True, verbose_name='Content')),
],
options={
'verbose_name': 'Chunk',
'verbose_name_plural': 'Chunks',
},
),
]
20 changes: 20 additions & 0 deletions chunks/migrations/0002_auto__add_field_chunk_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.20 on 2019-05-29 17:34
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('chunks', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='chunk',
name='description',
field=models.TextField(blank=True, verbose_name='Description'),
),
]
30 changes: 30 additions & 0 deletions chunks/migrations/0003_auto__chg_field_chunk_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models


class Migration(SchemaMigration):

def forwards(self, orm):

# Changing field 'Chunk.description'
db.alter_column('chunks_chunk', 'description', self.gf('django.db.models.fields.TextField')())

def backwards(self, orm):

# Changing field 'Chunk.description'
db.alter_column('chunks_chunk', 'description', self.gf('django.db.models.fields.CharField')(max_length=255))

models = {
'chunks.chunk': {
'Meta': {'object_name': 'Chunk'},
'content': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'key': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'})
}
}

complete_apps = ['chunks']
20 changes: 20 additions & 0 deletions chunks/migrations/0003_chunk_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2019-06-21 10:56
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('chunks', '0002_auto__add_field_chunk_description'),
]

operations = [
migrations.AddField(
model_name='chunk',
name='file',
field=models.FileField(blank=True, null=True, upload_to=b'uploads', verbose_name='File or image'),
),
]
Empty file added chunks/migrations/__init__.py
Empty file.
Loading