From b7c382a1a81887ee63ee14db61a8787a4412bde5 Mon Sep 17 00:00:00 2001
From: klmp200
Date: Sat, 22 Jul 2017 22:35:01 +0200
Subject: [PATCH] Basic search structure
---
core/views/__init__.py | 11 ++
matmat/__init__.py | 0
matmat/admin.py | 3 +
matmat/migrations/__init__.py | 0
matmat/models.py | 3 +
matmat/templates/matmat/search_form.jinja | 50 ++++++++
matmat/tests.py | 3 +
matmat/urls.py | 32 +++++
matmat/views.py | 146 ++++++++++++++++++++++
sith/settings.py | 1 +
sith/urls.py | 1 +
11 files changed, 250 insertions(+)
create mode 100644 matmat/__init__.py
create mode 100644 matmat/admin.py
create mode 100644 matmat/migrations/__init__.py
create mode 100644 matmat/models.py
create mode 100644 matmat/templates/matmat/search_form.jinja
create mode 100644 matmat/tests.py
create mode 100644 matmat/urls.py
create mode 100644 matmat/views.py
diff --git a/core/views/__init__.py b/core/views/__init__.py
index 470db647..b8205f57 100644
--- a/core/views/__init__.py
+++ b/core/views/__init__.py
@@ -143,6 +143,17 @@ class CanViewMixin(View):
self.get_queryset = types.MethodType(get_qs, self)
return super(CanViewMixin, self).dispatch(request, *arg, **kwargs)
+
+class WasSuscribed(View):
+ """
+ This view check if the user was at least an old subscriber
+ """
+ def dispatch(self, request, *args, **kwargs):
+ if not request.user.was_subscribed:
+ raise PermissionDenied
+ return super(WasSuscribed, self).dispatch(request, *args, **kwargs)
+
+
class TabedViewMixin(View):
"""
This view provide the basic functions for displaying tabs in the template
diff --git a/matmat/__init__.py b/matmat/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/matmat/admin.py b/matmat/admin.py
new file mode 100644
index 00000000..8c38f3f3
--- /dev/null
+++ b/matmat/admin.py
@@ -0,0 +1,3 @@
+from django.contrib import admin
+
+# Register your models here.
diff --git a/matmat/migrations/__init__.py b/matmat/migrations/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/matmat/models.py b/matmat/models.py
new file mode 100644
index 00000000..71a83623
--- /dev/null
+++ b/matmat/models.py
@@ -0,0 +1,3 @@
+from django.db import models
+
+# Create your models here.
diff --git a/matmat/templates/matmat/search_form.jinja b/matmat/templates/matmat/search_form.jinja
new file mode 100644
index 00000000..cf0d2862
--- /dev/null
+++ b/matmat/templates/matmat/search_form.jinja
@@ -0,0 +1,50 @@
+{% extends "core/base.jinja" %}
+
+{% block title %}
+{% trans %}Search user{% endtrans %}
+{% endblock %}
+
+{% block content %}
+{% if object_list.exists() %}
+{% trans %}User found{% endtrans %}
+{% for user in object_list %}
+ {{ user }}
+{% endfor %}
+{% endif %}
+{% trans %}Search user{% endtrans %}
+{% trans %}Search by profile{% endtrans %}
+
+{% trans %}Inverted search{% endtrans %}
+
+
+{% trans %}Simple search{% endtrans %}
+ {% csrf_token %}
+
+{% endblock %}
+
+{% block script %}
+{{ super() }}
+{% endblock %}
diff --git a/matmat/tests.py b/matmat/tests.py
new file mode 100644
index 00000000..7ce503c2
--- /dev/null
+++ b/matmat/tests.py
@@ -0,0 +1,3 @@
+from django.test import TestCase
+
+# Create your tests here.
diff --git a/matmat/urls.py b/matmat/urls.py
new file mode 100644
index 00000000..8a637b20
--- /dev/null
+++ b/matmat/urls.py
@@ -0,0 +1,32 @@
+# -*- coding:utf-8 -*
+#
+# Copyright 2017
+# - Sli
+#
+# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
+# http://ae.utbm.fr.
+#
+# This program is free software; you can redistribute it and/or modify it under
+# the terms of the GNU General Public License a 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 General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
+# Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+#
+
+from django.conf.urls import url
+
+from matmat.views import *
+
+urlpatterns = [
+ url(r'^$', SearchFormView.as_view(), name="search"),
+ url(r'^search_reverse$', SearchReverseFormView.as_view(), name="search_reverse"),
+]
diff --git a/matmat/views.py b/matmat/views.py
new file mode 100644
index 00000000..a6e63402
--- /dev/null
+++ b/matmat/views.py
@@ -0,0 +1,146 @@
+# -*- coding:utf-8 -*
+#
+# Copyright 2017
+# - Sli
+#
+# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
+# http://ae.utbm.fr.
+#
+# This program is free software; you can redistribute it and/or modify it under
+# the terms of the GNU General Public License a 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 General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
+# Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+#
+
+from django.shortcuts import render
+
+# Create your views here.
+
+from django.shortcuts import get_object_or_404
+from django.views.generic import ListView
+from django.views.generic.edit import FormView
+from django.core.urlresolvers import reverse_lazy, reverse
+from django.utils.translation import ugettext_lazy as _
+from django.views.generic.detail import SingleObjectMixin
+from django.core.exceptions import PermissionDenied
+from django.db import transaction
+from django.forms import CheckboxSelectMultiple
+from django import forms
+
+from core.models import User
+from core.views import WasSuscribed
+from core.views.forms import SelectDate
+from phonenumber_field.widgets import PhoneNumberInternationalFallbackWidget
+
+# Custom form
+
+
+class SearchForm(forms.ModelForm):
+ class Meta:
+ model = User
+ fields = [
+ 'first_name',
+ 'last_name',
+ 'nick_name',
+ 'sex',
+ 'role',
+ 'department',
+ 'semester',
+ 'promo',
+ 'date_of_birth',
+ 'phone',
+ ]
+ widgets = {
+ 'date_of_birth': SelectDate,
+ 'phone': PhoneNumberInternationalFallbackWidget,
+ # 'sex': CheckboxSelectMultiple,
+ }
+
+ def __init__(self, *args, **kwargs):
+ super(SearchForm, self).__init__(*args, **kwargs)
+ for key in self.fields.keys():
+ self.fields[key].required = False
+ self.fields['sex'].choices.append(("INDIFFERENT", _("Indifferent")))
+
+
+# Views
+
+class SearchFormListView(WasSuscribed, SingleObjectMixin, ListView):
+ model = User
+ template_name = 'matmat/search_form.jinja'
+
+ def dispatch(self, request, *args, **kwargs):
+ self.form_class = kwargs['form']
+ self.reverse = kwargs['reverse']
+ if 'valid_form' in kwargs.keys():
+ self.valid_form = kwargs['valid_form']
+ else:
+ self.valid_form = None
+
+ self.init_query = self.model.objects
+ if not (request.user.is_board_member or request.user.is_root):
+ self.init_query = self.init_query.exclude(is_subscriber_viewable=False)
+
+ return super(SearchFormListView, self).dispatch(request, *args, **kwargs)
+
+ def post(self, request, *args, **kwargs):
+ return self.get(request, *args, **kwargs)
+
+ def get_context_data(self, **kwargs):
+ self.object = None
+ kwargs = super(SearchFormListView, self).get_context_data(**kwargs)
+ kwargs['form'] = self.form_class()
+ return kwargs
+
+ def get_queryset(self):
+ if self.valid_form is not None:
+ if self.reverse:
+ return self.init_query.filter(phone=self.valid_form['phone']).all()
+ else:
+ q = self.init_query
+ # f = self.valid_form
+ return q.all()
+ else:
+ return self.model.objects.none()
+
+
+class SearchFormView(WasSuscribed, FormView):
+ """
+ Allows users to search inside the user list
+ """
+ form_class = SearchForm
+ reverse = False
+
+ def dispatch(self, request, *args, **kwargs):
+ self.init_query = User.objects
+ kwargs['form'] = self.get_form_class()
+ kwargs['reverse'] = self.reverse
+ return super(SearchFormView, self).dispatch(request, *args, **kwargs)
+
+ def get(self, request, *args, **kwargs):
+ view = SearchFormListView.as_view()
+ return view(request, *args, **kwargs)
+
+ def post(self, request, *args, **kwargs):
+ form = self.get_form()
+ view = SearchFormListView.as_view()
+ if form.is_valid():
+ kwargs['valid_form'] = form.clean()
+ return view(request, *args, **kwargs)
+
+ def get_initial(self):
+ return super(SearchFormView, self).get_initial()
+
+
+class SearchReverseFormView(SearchFormView):
+ reverse = True
diff --git a/sith/settings.py b/sith/settings.py
index d12d3a0b..948c6d2d 100644
--- a/sith/settings.py
+++ b/sith/settings.py
@@ -87,6 +87,7 @@ INSTALLED_APPS = (
'forum',
'stock',
'trombi',
+ 'matmat',
)
MIDDLEWARE_CLASSES = (
diff --git a/sith/urls.py b/sith/urls.py
index 9623ca16..2b744c24 100644
--- a/sith/urls.py
+++ b/sith/urls.py
@@ -67,6 +67,7 @@ urlpatterns = [
url(r'^election/', include('election.urls', namespace="election", app_name="election")),
url(r'^forum/', include('forum.urls', namespace="forum", app_name="forum")),
url(r'^trombi/', include('trombi.urls', namespace="trombi", app_name="trombi")),
+ url(r'^matmatronch/', include('matmat.urls', namespace="matmat", app_name="matmat")),
url(r'^admin/', include(admin.site.urls)),
url(r'^ajax_select/', include(ajax_select_urls)),
url(r'^i18n/', include('django.conf.urls.i18n')),