Skip to content
Snippets Groups Projects
views.py 3.04 KiB
#
# Copyright (c) 2013-2023 NIBIO.
#
# This file is part of VIPSWeb 
# (see https://gitlab.nibio.no/VIPS/VIPSWeb).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#

import json

from django.shortcuts import render
from django.utils import translation
from django.http import HttpResponse, Http404

from vips_messages.models import Message
from vips_messages.forms import MessagePublishedDateFilterForm
from organisms.models import Organism, CropCategory

def index(request):
    messages = None
    form = MessagePublishedDateFilterForm(request.GET)
    if form == None:
        form = MessagePublishedDateFilterForm() 
    elif form.is_valid():
        messages = Message.get_messages(
                              translation.get_language(), 
                              form.cleaned_data["publishedFrom"], 
                              form.cleaned_data["publishedTo"]
                              )
    else:
        messages = Message.get_messages(translation.get_language())
    
    # Sort the messages
    
    
    crops = Organism.get_all_crops()    
    
    context = {
               'messages' : messages,
               'crop_categories': CropCategory.get_crop_categories(translation.get_language()),
               'form' : form,
               'crops' : crops
               }
    return render(request, 'messages/index.html', context)

def detail(request, message_id):
    message = Message.get_message(message_id, translation.get_language())
    if message is not None:
        context = {
                #'message' : get_object_or_404(Message, pk=message_id)
                'message': Message.get_message(message_id, translation.get_language())
                }
        return render(request, 'messages/detail.html', context)
    raise Http404("This page does not exist")

# Send messages as JSON
# TODO: Skip intermediary Python object, get json directly from VIPSLogic
def messages_by_tag_json(request):
    # Get front page categories. This is defined in local_settings.py
    messages_by_tag = {}
    try:
        for message_tag_id in map(int, request.GET.get("messageTagIds","").split(',')):
            messages = Message.get_messages_by_tag_as_json(message_tag_id)
            #print "MESSAGES: %s" % messages
            messages_by_tag[message_tag_id] = messages
    except ValueError:
        messages_by_tag = {}
    #print messages_by_tag
    the_response = json.dumps(messages_by_tag)
    return HttpResponse(the_response, content_type='application/json')