Skip to content
Snippets Groups Projects
Commit 8a5282c1 authored by Tor-Einar Skog's avatar Tor-Einar Skog
Browse files

First version with support for messages coming from VIPSLogic

parent 6805dfa9
No related branches found
No related tags found
No related merge requests found
......@@ -45,6 +45,9 @@ MANAGERS = ADMINS
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en'
# Preferred language in VIPSLogic
VIPSLOGIC_LANGUAGE_CODE = "en"
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
......
......@@ -94,8 +94,8 @@ MIDDLEWARE_CLASSES = (
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'common.middleware.whodid.WhodidMiddleware'
#'django.middleware.locale.LocaleMiddleware',
'common.middleware.whodid.WhodidMiddleware',
'django.middleware.locale.LocaleMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
......
......@@ -53,14 +53,14 @@
{% for message_tag in message_tags %}
<thead>
<tr>
<th colspan="2">{{message_tag.title}}</th>
<th colspan="2">{{message_tag.default_tag_name}}</th>
</tr>
</thead>
<tbody>
{% for message in messages_by_tag|lookup:message_tag.id %}
{% for message in messages_by_tag|lookup:message_tag.message_tag_id %}
<tr>
<td>{{ message.pub_date|date:"Y-m-d" }}</td>
<td><a href="{% url 'messages:detail' message.id %}">{{message.headline}}</a></td>
<td>{{ message.date_pub|date:"Y-m-d" }}</td>
<td><a href="{% url 'messages:detail' message.message_id %}">{{message.heading}}</a></td>
</tr>
{% endfor %}
</tbody>
......
......@@ -20,23 +20,19 @@ from django.conf import settings
from messages.models import Message, MessageTag
def index(request):
# Get frontpage categories. This is defined in local_settings.py
message_tags = []
# Get front page categories. This is defined in local_settings.py
message_tags = MessageTag.get_message_tags()
messages_by_tag = {}
for message_tag_id in settings.FRONTPAGE_MESSAGE_TAG_IDS:
message_tag = MessageTag.objects.get(pk=message_tag_id)
print message_tag
message_tags.append(message_tag)
messages = Message.objects.filter(message_tags__pk=message_tag_id)
messages = Message.get_messages_by_tag(message_tag_id)
print "MESSAGES: %s" % messages
messages_by_tag[message_tag_id] = messages
#for message in messages:
# print message
#messages_ordered.append(message_tagged)
# Last 10 messages
messages = Message.objects.order_by('pub_date').reverse()[:10]
context = {
'messages' : messages,
'message_tags': message_tags,
'messages_by_tag': messages_by_tag
}
......
......@@ -15,8 +15,9 @@
# You should have received a copy of the GNU Affero General Public License
# along with VIPSWeb. If not, see <http://www.gnu.org/licenses/>.
"""
from django.contrib import admin
from messages.models import Message, MessageTag
from messages.models import Message#, MessageTag
class MessageAdmin(admin.ModelAdmin):
list_display = ['pub_date','headline','created_by']
......@@ -24,3 +25,4 @@ class MessageAdmin(admin.ModelAdmin):
admin.site.register(Message, MessageAdmin)
admin.site.register(MessageTag)
"""
\ No newline at end of file
......@@ -15,39 +15,134 @@
# You should have received a copy of the GNU Affero General Public License
# along with VIPSWeb. If not, see <http://www.gnu.org/licenses/>.
import requests
from django.conf import settings
from django.db import models
from django.contrib.auth.models import User
from datetime import timedelta
# Create your models here.
class MessageTag(models.Model):
title = models.CharField(max_length=255)
def __unicode__(self):
return self.title
class MessageTag:
def __init__(self,
message_tag_id,
default_tag_name,
local_tag_name = None
):
self.message_tag_id = message_tag_id
self.default_tag_name = default_tag_name
self.local_tag_name = local_tag_name
@staticmethod
def get_message_tags():
message_tags = []
for item in MessageTag.get_message_tags_as_json():
message_tag = MessageTag.get_instance_from_dict(item)
message_tags.append(message_tag)
return message_tags
@staticmethod
def get_message_tags_as_json():
request_result = requests.get("http://%s/rest/messagetag/list" % (settings.VIPSLOGIC_SERVER_NAME))
return request_result.json()
@staticmethod
def get_instance_from_dict(item):
return MessageTag(
item["messageTagId"],
item["defaultTagName"],
None
)
class Message(models.Model):
headline = models.CharField(max_length=255)
lead = models.TextField(blank=True)
body_text = models.TextField()
pub_date = models.DateField()
valid_to_date = models.DateField(blank=True) # Default is 14 days after pub_date. See self.save
illustration = models.ImageField(upload_to='images/messages', blank=True)
created_by = models.ForeignKey(User, related_name='messages_message_created_by', editable=False)
modified_by = models.ForeignKey(User, related_name='messages_message_modified_by', editable=False)
created_date = models.DateTimeField(auto_now_add=True, editable=False)
modified_date = models.DateTimeField(auto_now=True, editable=False)
message_tags = models.ManyToManyField(MessageTag)
def __unicode__(self):
return self.headline
def save(self, *args, **kwargs):
if self.valid_to_date == None:
self.valid_to_date = self.pub_date + timedelta(days=14)
super(Message, self).save(*args, **kwargs)
class Message:
def __init__(self,
message_id,
organization_id,
heading,
lead_paragraph,
body,
date_pub,
date_valid_to,
illustration_file_name,
illustration_caption):
self.message_id = message_id
self.organization_id = organization_id
self.heading = heading
self.lead_paragraph = lead_paragraph
self.body = body
self.date_pub = date_pub
self.date_valid_to = date_valid_to
self.illustration_file_name = illustration_file_name
self.illustration_caption = illustration_caption
def get_illustration_url(self):
return "http://%s/static/images/messages/%s/%s" % (settings.VIPSLOGIC_SERVER_NAME,self.organization_id,self.illustration_file_name)
"""
Getting information from persistence layer
Currently this is a REST service
"""
@staticmethod
def get_message(message_id):
message_json = Message.get_message_as_json(message_id)
return Message.get_instance_from_dict(message_json)
@staticmethod
def get_messages_by_tag(tag_id):
messages = []
for item in Message.get_messages_by_tag_as_json(tag_id):
message = Message.get_instance_from_dict(item)
messages.append(message)
return messages
@staticmethod
def get_messages():
messages = []
for item in Message.get_messages_as_json():
message = Message.get_instance_from_dict(item)
messages.append(message)
return messages
@staticmethod
def get_messages_as_json():
request_result = requests.get("http://%s/rest/message/list/%s" % (settings.VIPSLOGIC_SERVER_NAME,settings.VIPS_ORGANIZATION_ID))
return request_result.json()
@staticmethod
def get_messages_by_tag_as_json(tag_id):
request_result = requests.get("http://%s/rest/message/list/%s/tagfilter?tagId=%s" % (settings.VIPSLOGIC_SERVER_NAME,settings.VIPS_ORGANIZATION_ID,tag_id))
return request_result.json()
@staticmethod
def get_message_as_json(message_id):
request_result = requests.get("http://%s/rest/message/%s" % (settings.VIPSLOGIC_SERVER_NAME,message_id))
return request_result.json()
@staticmethod
def get_instance_from_dict(the_dict):
message_locale = Message.get_message_locale(the_dict["messageLocaleSet"], settings.VIPSLOGIC_LANGUAGE_CODE)
message_illustration = Message.get_illustration(the_dict["messageIllustrationSet"], settings.VIPSLOGIC_LANGUAGE_CODE)
return Message(
the_dict["messageId"],
the_dict["organizationId"],
message_locale["heading"],
message_locale["leadParagraph"],
message_locale["body"],
the_dict["datePub"],
the_dict["dateValidTo"],
message_illustration.get("file_name", None),
message_illustration.get("caption", None)
)
@staticmethod
def get_message_locale(message_locale_set, language):
for message_locale in message_locale_set:
if message_locale["messageLocalePK"]["locale"] == language:
return message_locale
@staticmethod
def get_illustration(message_illustration_set, language):
message_illustration = {}
if len(message_illustration_set) == 1:
message_illustration["file_name"] = message_illustration_set[0]["messageIllustrationPK"]["fileName"]
for caption in message_illustration_set[0]["messageIllustrationCaptionLocaleSet"]:
if caption["messageIllustrationCaptionLocalePK"]["locale"] == language:
message_illustration["caption"] = caption["caption"]
return message_illustration
\ No newline at end of file
......@@ -2,12 +2,12 @@
{% load i18n %}
{% block title%}{{ message.headline }}{%endblock%}
{% block content %}
{% if message.illustration %}
<div class="messages_illustration"><img src="{{message.illustration.url}}"/></div>
{% if message.illustration_file_name %}
<div class="messages_illustration"><img src="{{message.get_illustration_url}}" class="img-responsive"/></div>
{% endif %}
<h1>{{ message.headline }}</h1>
<p class="byline">{{message.pub_date}}</p>
<p class="lead">{{message.lead}}</p>
<p>{{message.body_text|linebreaks}}</p>
<h1>{{ message.heading }}</h1>
<p class="byline">{{message.date_pub}}</p>
<p class="lead">{{message.lead_paragraph}}</p>
<p>{{message.body|linebreaks}}</p>
{% endblock %}
\ No newline at end of file
......@@ -11,7 +11,6 @@
<tr>
<th>{% trans "Publish date" %}</th>
<th>{% trans "Headline" %}</th>
<th>{% trans "Tags" %}
<th>{% trans "Author" %}</th>
<th></th>
</tr>
......@@ -19,11 +18,10 @@
<tbody>
{% for message in messages reversed %}
<tr>
<td>{{ message.pub_date }}</td>
<td>{{ message.headline}}</td>
<td>{{ message.message_tags.all|join:","}}</td>
<td>{{ message.created_by}}</td>
<td><a href="{% url 'messages:detail' message.id %}">{% trans "Details" %}</a></td>
<td>{{ message.date_pub }}</td>
<td>{{ message.heading}}</td>
<td></td>
<td><a href="{% url 'messages:detail' message.message_id %}">{% trans "Details" %}</a></td>
</tr>
{% endfor %}
</tbody>
......
......@@ -21,12 +21,13 @@ from messages.models import Message
def index(request):
context = {
'messages' : Message.objects.all(),
'messages' : Message.get_messages(),
}
return render(request, 'messages/index.html', context)
def detail(request, message_id):
context = {
'message' : get_object_or_404(Message, pk=message_id)
#'message' : get_object_or_404(Message, pk=message_id)
'message': Message.get_message(message_id)
}
return render(request, 'messages/detail.html', context)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment