Skip to content

Commit

Permalink
showing article image in article window
Browse files Browse the repository at this point in the history
  • Loading branch information
l-dfa committed Dec 18, 2018
2 parents c356235 + 35f5eee commit 2cb713c
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 23 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ colorama==0.3.9
Django==2.1.2
django-concurrency==1.4
django-fullurl==0.5
django-taggit==0.23.0
docutils==0.14
idna==2.7
imagesize==1.0.0
Expand Down
5 changes: 3 additions & 2 deletions rstsite/rstblog/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from django.contrib import admin


from rstblog.models import Category
from rstblog.models import Category, CategoryAdmin
from rstblog.models import Author, AuthorAdmin
from rstblog.models import Article, ArticleAdmin


admin.site.register(Category)
admin.site.register(Category, CategoryAdmin)
admin.site.register(Author, AuthorAdmin)
admin.site.register(Article, ArticleAdmin)

57 changes: 50 additions & 7 deletions rstsite/rstblog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _

from taggit.managers import TaggableManager
from taggit.models import TagBase, GenericTaggedItemBase

from concurrency.fields import IntegerVersionField

from .const import ARTICLES_DIR
Expand All @@ -28,6 +31,19 @@
MEDIUM_LEN = 250
LONG_LEN = 2000

class LocTag(TagBase):
language = models.CharField(
verbose_name=_('language'),
max_length = 2,
null=False,
blank = False,
choices=list(LANGUAGES.items()),
default = list(LANGUAGES.keys())[0], ) # BEWARE.from py 3.6+ dict preserve keys order by insertion

class Meta:
verbose_name = _("Tag")
verbose_name_plural = _("Tags")

class Author(models.Model):
'''author: article author
Expand Down Expand Up @@ -72,14 +88,29 @@ class Category(models.Model):
name = models.CharField(
'name',
max_length=SHORT_LEN,
null=False,
blank=False,
unique=True,)
null = False,
blank = False,)
language = models.CharField(
'language',
max_length = 2,
null = False,
blank = False,
choices = list(LANGUAGES.items()),
default = list(LANGUAGES.keys())[0], ) # BEWARE.in py 3.6+ dict preserve keys order by insertion
translation_of = models.ForeignKey( # link to original category
'self',
on_delete=models.SET_DEFAULT,
null=True,
blank=True,
related_name='translated_by',
verbose_name='translation_of',
default=None, )

def __str__(self):
return self.name

class Meta:
unique_together = ('name', 'language',)
verbose_name_plural = "categories"


Expand Down Expand Up @@ -144,9 +175,7 @@ class Article(models.Model):
max_length = 2,
null=False,
blank = False,
#choices=LANGUAGE,
choices=list(LANGUAGES.items()),
#default = ITALIAN, )
default = list(LANGUAGES.keys())[0], ) # BEWARE.from py 3.6+ dict preserve keys order by insertion
markup = models.CharField(
'markup_language',
Expand Down Expand Up @@ -190,6 +219,11 @@ class Article(models.Model):
null=False,
blank = False,
default=True, )
image_in_content = models.BooleanField(
'show article image in content',
null=False,
blank = False,
default=True, )
hit = models.IntegerField(
'hit',
null=False,
Expand Down Expand Up @@ -275,13 +309,15 @@ class Meta:
'slug', 'atype', 'published', 'offer_home', 'hit',
'authors',
'category',
'translation_of', )
'translation_of',
'image_in_content', )
def clean_slug(self):
return self.cleaned_data['slug'] or None


class ArticleAdmin(admin.ModelAdmin):
form = ArticleForm
list_display = ('title', 'language')


class AuthorForm(forms.ModelForm):
Expand All @@ -296,4 +332,11 @@ class AuthorAdmin(admin.ModelAdmin):
form = AuthorForm



class CategoryForm(forms.ModelForm):
class Meta:
model = Category
fields = ('name', 'language', 'translation_of', )

class CategoryAdmin(admin.ModelAdmin):
form = CategoryForm
list_dispay = ('language', 'name')
9 changes: 8 additions & 1 deletion rstsite/rstblog/templates/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@
</p>
</div>
{% endif %}

{% if article.image != None and article.image_in_content %}
<div style="float:left;">
{% with "/media/images/"|add:article.image as img %}
<img class="container-fluid hidden-xs soiola" src="{{ img }}" alt="{{article.image}}" style="width:240px;">
<!-- <img src="{{ img }}" alt="{{article.image}}" style="width:100px;border:0;padding:0;"> -->
{% endwith %}
</div>
{% endif %}
<div>
{{ content |safe }}
</div>
Expand Down
22 changes: 12 additions & 10 deletions rstsite/rstblog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ def get_record(dst):
from authors.
'''

file_content = get_file_content(dst)
file_content = get_file_content(dst) # grab the article's file overall content
#pdb.set_trace()
result = separate(file_content.decode('utf-8'))
result = separate(file_content.decode('utf-8')) # split (using "hic sunt leones") the overall content in => (attributes, content, ) tuple of strings
if result:
attributes, content = result
record = docinfos(attributes)
Expand Down Expand Up @@ -282,20 +282,20 @@ def cOu_article_record(pth, must_be_original='ignore'):
'''create or update article record from Path
parameters:
- pth Path, to file to use
- pth Path, file to use
- must_be_original str, 'yes', 'no', 'ignore' (or whatever else)
if 'yes' elaborate file only if original
'no' elaborate only if translation
'ignore' elaborate in any case
return:
- article record
- None if ignore file
- None if refused file
- could raise exception '''

article = None
try:
record, authors = get_record(pth)
record, authors = get_record(pth) # get fields infos from file
# if: must be original and is a translation
# OR: must be translation and is original
# BAIL OUT with None
Expand Down Expand Up @@ -332,7 +332,7 @@ def load_article(request):
try:
#pdb.set_trace()
dst = upload_file(request, item_type='article', dirdst = Path(ARTICLES_DIR))
article = cOu_article_record(dst, must_be_original='ignore')
article = cOu_article_record(dst, must_be_original='ignore') # create or update article record
msg = 'article {} loaded'.format( dst.name, )
messages.add_message(request, messages.INFO, msg)
except Exception as ex:
Expand Down Expand Up @@ -506,8 +506,7 @@ def get_field(area, field):
def rough_docinfos(content):
'''get docinfo fields from content
params:
- content str, reST document
params: content str, reST document of fields (i.e.: ":markup: restructuredtext\n:language: en\n...")
return: a dict with field names as keys and field bodies as values
Expand Down Expand Up @@ -566,9 +565,11 @@ def docinfos(content):
- None, if translated article is not found
- in case value=='', dictionary voice is deleted'''

#pdb.set_trace()

infos = rough_docinfos(content)
#pdb.set_trace()
if 'language' not in infos:
infos['language'] = settings.RSTBLOG.get('languages', {'en': 'english',}).keys()[0] # set language at default if not declared in article

# elaborate category, authors, created, modified
for name, body in infos.items():
Expand Down Expand Up @@ -604,8 +605,9 @@ def docinfos(content):
# check category
if name == 'category':
category = None
language = list(LANGUAGES.keys())[0] # BEWARE.from py 3.6+ dict preserve keys order by insertion
try:
category = Category.objects.get(name=body)
category = Category.objects.get(name=body, language=language)
except:
category = Category.objects.get(name='uncategorized')
if category:
Expand Down
9 changes: 6 additions & 3 deletions rstsite/rstsite/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
pass



articles_dict = {
# the next line is valid for articles AND pages
# so PageSitemap is no more needed
Expand All @@ -70,6 +71,7 @@

}


urlpatterns = [
path('blog/', include('rstblog.urls', namespace='rstblog')),
path('load-page', views.load_page, name='load_page'),
Expand All @@ -81,9 +83,10 @@
path('', views.index, name='index'),
url(r'^i18n/', include('django.conf.urls.i18n')),
path('admin/', admin.site.urls),
#path('login/', auth_views.LoginView, {'template_name': 'users/registration/login.html',}, name='login'),
path('login/', auth_views.LoginView.as_view(), name='login'),
path('logout/', auth_views.LogoutView, {'next_page': settings.LOGIN_REDIRECT_URL}, name='logout'),
#path('login/', auth_views.login, {'template_name': 'login.html',}, name='login'), #-chg ldfa @2018-11-27
#path('logout/', auth_views.logout, {'next_page': '/login'}, name='logout'), #-chg ldfa @2018-11-27
path('login/', auth_views.LoginView.as_view(), name='login'), #+chg ldfa @2018-11-27
path('logout/', auth_views.LogoutView, {'next_page': settings.LOGIN_REDIRECT_URL}, name='logout'), #+chg ldfa @2018-11-27
]

if settings.DEBUG is True:
Expand Down

0 comments on commit 2cb713c

Please sign in to comment.