148 lines
5.7 KiB
Python
148 lines
5.7 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.contrib.auth import get_user_model
|
|
from django.core.mail import EmailMultiAlternatives
|
|
from django.http import HttpResponseRedirect, JsonResponse
|
|
from django.template import RequestContext
|
|
from django.template.loader import render_to_string
|
|
|
|
from combo.apps.dashboard.models import Tile
|
|
from combo.data.models import ConfigJsonCell
|
|
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)
|
|
|
|
|
|
def stats(request, *args, **kwargs):
|
|
data = {}
|
|
User = get_user_model()
|
|
data['users'] = {}
|
|
data['users']['count'] = User.objects.all().count()
|
|
data['tiles'] = {}
|
|
tiles_by_user = {}
|
|
manual_tiles_by_user = {}
|
|
|
|
# preload
|
|
cells = {}
|
|
for cell in ConfigJsonCell.objects.filter(placeholder__in=['_dashboard', '_suggested_tile']):
|
|
cells[cell.id] = cell
|
|
|
|
for tile in Tile.objects.filter(dashboard__isnull=False):
|
|
cell = cells[tile.cell_pk] # no db access
|
|
|
|
if cell.key not in settings.JSON_CELL_TYPES:
|
|
continue
|
|
|
|
if tile.user_id not in tiles_by_user:
|
|
tiles_by_user[tile.user_id] = []
|
|
tiles_by_user[tile.user_id].append(cell.key)
|
|
|
|
if cell.key not in data['tiles']:
|
|
data['tiles'][cell.key] = {
|
|
'name': settings.JSON_CELL_TYPES[cell.key]['name'],
|
|
'count': 0,
|
|
'manual': 0,
|
|
}
|
|
|
|
data['tiles'][cell.key]['count'] += 1
|
|
|
|
if cell.placeholder != '_suggested_tile':
|
|
data['tiles'][cell.key]['manual'] += 1
|
|
if tile.user_id not in manual_tiles_by_user:
|
|
manual_tiles_by_user[tile.user_id] = []
|
|
manual_tiles_by_user[tile.user_id].append(cell.key)
|
|
|
|
data['users']['have-tiles'] = len(tiles_by_user.keys())
|
|
data['users']['have-more-than-suggested-tiles'] = len(manual_tiles_by_user.keys())
|
|
data['users']['have-no-tiles'] = data['users']['count'] - data['users']['have-tiles']
|
|
dashboard_lengths = [len(x) for x in tiles_by_user.values()]
|
|
if dashboard_lengths:
|
|
dashboard_lengths.sort()
|
|
data['users']['max-tiles'] = max(dashboard_lengths)
|
|
data['users']['median-tiles'] = dashboard_lengths[len(dashboard_lengths) // 2]
|
|
|
|
return JsonResponse(data)
|
|
|
|
|
|
def cartads_unsubscribe(request, *args, **kwargs):
|
|
cell = ConfigJsonCell.objects.filter(key='cartads-dossiers').first()
|
|
if cell:
|
|
next_url = cell.page.get_online_url()
|
|
else:
|
|
next_url = '/'
|
|
numero_dossier = request.GET.get('numero_dossier')
|
|
if numero_dossier:
|
|
context = RequestContext(request, {'request': request})
|
|
context.update(cell.get_cell_extra_context(context))
|
|
action_url = get_templated_url(
|
|
settings.JSON_CELL_TYPES['cartads-dossiers']['actions']['unsubscribe']['url'], context
|
|
)
|
|
action_url += '&dossier_number=' + numero_dossier
|
|
response = requests.get(action_url, remote_service='auto', without_user=True)
|
|
if response.ok:
|
|
messages.info(request, u'Le dossier a été retiré de votre suivi.')
|
|
return HttpResponseRedirect(next_url)
|
|
messages.error(request, u"Le dossier n'a pas pu être retiré de votre suivi.")
|
|
return HttpResponseRedirect(next_url)
|