combo-plugin-gnm/combo_plugin_gnm/views.py

67 lines
2.8 KiB
Python

# -*- coding: utf-8 -*-
# combo-plugin-gnm - Combo GNM plugin
# Copyright (C) 2017 Entr'ouvert
#
# 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/>.
from django.conf import settings
from django.contrib import messages
from django.core.mail import EmailMultiAlternatives
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.template.loader import render_to_string
from combo.utils import requests, get_templated_url
def plusone(request, *args, **kwargs):
# add reference to a jsondatastore with slug "plus1"
reference = request.GET.get('ref')
if reference:
context = RequestContext(request, {'request': request})
passerelle_url = '[passerelle_url]jsondatastore/plus1/data/create'
if request.user and request.user.is_authenticated():
passerelle_url += '?name_id=[user_nameid]'
passerelle_url = get_templated_url(passerelle_url, context)
headers = {'content-type': 'application/json'}
requests.post(passerelle_url, remote_service='auto',
without_user=True,
headers={'Accept': 'application/json'},
json={'reference': reference},
timeout=2)
messages.info(request, u'Merci, votre confirmation nous est utile.')
if request.user and request.user.is_authenticated():
return HttpResponseRedirect('/')
else:
return HttpResponseRedirect('/services/')
def share(request, *args, **kwargs):
subject_template = 'gnm/share_email_subject.txt'
text_body_template = 'gnm/share_email_body.txt'
html_body_template = 'gnm/share_email_body.html'
ctx = {}
ctx['request'] = request
ctx['name'] = request.POST['name']
ctx['url'] = request.build_absolute_uri(request.POST['url'])
subject = render_to_string([subject_template], ctx, request=request).strip()
text_body = render_to_string([text_body_template], ctx, request=request)
html_body = render_to_string([html_body_template], ctx, request=request)
message = EmailMultiAlternatives(subject, text_body, to=[request.POST['email']])
message.attach_alternative(html_body, 'text/html')
message.send()
url = request.POST.get('url')
return HttpResponseRedirect(url)