Commit e2394117 authored by Sli's avatar Sli

Début de la gestion des articles

parent c9ba9650
env/
static
__pycache__
gala/settings.py
/media
/scripts
.DS_Store
db.sqlite3
from django.db import models
from django.contrib import admin
from markdownx.models import MarkdownxField
from markdownx.widgets import AdminMarkdownxWidget
from .models import Article
class ArticleAdmin(admin.ModelAdmin):
formfield_overrides = {
MarkdownxField: {'widget': AdminMarkdownxWidget},
}
list_display = ('title', 'published', 'date')
list_editable = ('title', 'published')
ordering = ('-date', )
search_fields = ['title', 'content']
admin.site.register(Article, ArticleAdmin)
from django.apps import AppConfig
class BlogConfig(AppConfig):
name = 'blog'
# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2016-06-04 22:32
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Article',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=100)),
('slug', models.SlugField(editable=False, max_length=150, unique=True)),
('content', models.TextField()),
('published', models.BooleanField(default=False)),
],
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2016-06-04 23:32
from __future__ import unicode_literals
from django.db import migrations
import markdownx.models
class Migration(migrations.Migration):
dependencies = [
('blog', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='article',
name='content',
field=markdownx.models.MarkdownxField(),
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2016-06-05 16:31
from __future__ import unicode_literals
import datetime
from django.db import migrations, models
from django.utils.timezone import utc
class Migration(migrations.Migration):
dependencies = [
('blog', '0002_auto_20160604_2332'),
]
operations = [
migrations.AddField(
model_name='article',
name='date',
field=models.DateTimeField(auto_now_add=True, default=datetime.datetime(2016, 6, 5, 16, 31, 46, 374730, tzinfo=utc), verbose_name='Date de parution'),
preserve_default=False,
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2016-06-05 16:34
from __future__ import unicode_literals
from django.db import migrations, models
import markdownx.models
class Migration(migrations.Migration):
dependencies = [
('blog', '0003_article_date'),
]
operations = [
migrations.AlterField(
model_name='article',
name='content',
field=markdownx.models.MarkdownxField(verbose_name='Contenu'),
),
migrations.AlterField(
model_name='article',
name='published',
field=models.BooleanField(default=False, verbose_name='Publication'),
),
migrations.AlterField(
model_name='article',
name='title',
field=models.CharField(max_length=100, verbose_name='Titre'),
),
]
from django.db import models
from django.utils.text import slugify
from blog.snippets import SlugUnique
from django.core.urlresolvers import reverse
from markdownx.models import MarkdownxField
class Article(models.Model):
"""Classe article"""
title = models.CharField(max_length=100, verbose_name="Titre")
slug = models.SlugField(unique=True, max_length=150, editable=False)
content = MarkdownxField(verbose_name="Contenu")
published = models.BooleanField(default=False, verbose_name="Publication")
date = models.DateTimeField(auto_now_add=True, auto_now=False,
verbose_name="Date de parution")
def __str__(self):
return self.title
def save(self, *args, **kwargs):
SlugUnique.unique_slugify(self, self.title)
super(Article, self).save(*args, **kwargs)
import re
from django.template.defaultfilters import slugify
def unique_slugify(instance, value, slug_field_name='slug', queryset=None,
slug_separator='-'):
"""
Calculates and stores a unique slug of ``value`` for an instance.
``slug_field_name`` should be a string matching the name of the field to
store the slug in (and the field to check against for uniqueness).
``queryset`` usually doesn't need to be explicitly provided - it'll default
to using the ``.all()`` queryset from the model's default manager.
"""
slug_field = instance._meta.get_field(slug_field_name)
slug = getattr(instance, slug_field.attname)
slug_len = slug_field.max_length
# Sort out the initial slug, limiting its length if necessary.
slug = slugify(value)
if slug_len:
slug = slug[:slug_len]
slug = _slug_strip(slug, slug_separator)
original_slug = slug
# Create the queryset if one wasn't explicitly provided and exclude the
# current instance from the queryset.
if queryset is None:
queryset = instance.__class__._default_manager.all()
if instance.pk:
queryset = queryset.exclude(pk=instance.pk)
# Find a unique slug. If one matches, at '-2' to the end and try again
# (then '-3', etc).
next = 2
while not slug or queryset.filter(**{slug_field_name: slug}):
slug = original_slug
end = '%s%s' % (slug_separator, next)
if slug_len and len(slug) + len(end) > slug_len:
slug = slug[:slug_len-len(end)]
slug = _slug_strip(slug, slug_separator)
slug = '%s%s' % (slug, end)
next += 1
setattr(instance, slug_field.attname, slug)
def _slug_strip(value, separator='-'):
"""
Cleans up a slug by removing slug separator characters that occur at the
beginning or end of a slug.
If an alternate separator is used, it will also replace any instances of
the default '-' separator with the new separator.
"""
separator = separator or ''
if separator == '-' or not separator:
re_sep = '-'
else:
re_sep = '(?:-|%s)' % re.escape(separator)
# Remove multiple instances and if an alternate separator is provided,
# replace the default '-' separator.
if separator != re_sep:
value = re.sub('%s+' % re_sep, separator, value)
# Remove separator from the beginning and end of the slug.
if separator:
if separator != '-':
re_sep = re.escape(separator)
value = re.sub(r'^%s+|%s+$' % (re_sep, re_sep), '', value)
return value
\ No newline at end of file
<!DOCTYPE html>
{% load markdown_deux_tags %}
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
{% for article in articles %}
<h1>{{article.title}}</h1>
{% markdown %}
{{article.content}}
{% endmarkdown %}
{% endfor %}
</body>
</html>
\ No newline at end of file
from django.test import TestCase
# Create your tests here.
# -*- coding: utf-8 -*-
# @Author: klmp200
# @Date: 2016-06-04 23:24:06
# @Last Modified by: klmp200
# @Last Modified time: 2016-06-04 23:40:24
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('blog.views',
url(r'^$', 'blog', name='blog'),
)
\ No newline at end of file
#-*- coding: utf-8 -*-
from django.shortcuts import render
from blog.models import Article
def blog(request):
articles = Article.objects.exclude(published=False).order_by('-date')
return render(request, 'blog/blog.html', {'articles': articles})
......@@ -37,6 +37,8 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
'markdownx',
]
MIDDLEWARE_CLASSES = [
......@@ -104,7 +106,7 @@ AUTH_PASSWORD_VALIDATORS = [
# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/
LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'fr-FR'
TIME_ZONE = 'UTC'
......@@ -119,3 +121,4 @@ USE_TZ = True
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = 'absolute-path-to/static'
\ No newline at end of file
......@@ -13,9 +13,16 @@ Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.conf.urls import url, include, patterns
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
import re
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
urlpatterns = patterns('',
url(r'^', include('blog.urls')),
url(r'^markdownx/', include('markdownx.urls')),
url(r'^admin/', include(admin.site.urls)),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment