Commit 3e3cd726 authored by c24b's avatar c24b

Lang persistency Ok with user API params

parent bb91a93b
......@@ -21,7 +21,7 @@ class User(Base):
is_active = Column(Boolean(), default=True)
last_login = Column(DateTime(timezone=False))
date_joined = Column(DateTime(timezone=False), default=datetime.now)
def contacts(self):
"""get all contacts in relation with the user"""
Friend = aliased(User)
......@@ -61,7 +61,9 @@ class User(Base):
"""check if a given node is owned by the user"""
return (node.user_id == self.id) or \
node.id in (contact.id for contact in self.contacts())
def get_params(self, username=None):
print(self.__dict__.items())
return self.hyperdata
class Contact(Base):
__tablename__ = 'contacts'
......
......@@ -91,6 +91,6 @@ urlpatterns = [ url(r'^nodes$' , nodes.NodeListResource.as_view()
, url(r'^ngramlists/maplist$' , ngramlists.MapListGlance.as_view() )
# fast access to maplist, similarly formatted for termtable
, url(r'^users/$', users.UserItem.as_view())
, url(r'^user/parameters/$', users.UserParameters.as_view())
]
......@@ -6,8 +6,17 @@ from gargantext.util.toolchain import *
import copy
from gargantext.util.db import session
class UserItem(APIView):
class UserParameters(APIView):
'''API endpoint that represent the parameters of the user'''
def get(self, request):
node_user = session.query(Node).filter(Node.user_id == request.user.id, Node.typename== "USER").first()
if node_user is None:
return Response({"detail":"Not Found"}, status=HTTP_404)
else:
#context = format_response(node_user, )
return Response(node_user.hyperdata)
def put(self, request):
if request.user.id is None:
raise TypeError("This API request must come from an authenticated user.")
......@@ -17,9 +26,14 @@ class UserItem(APIView):
node_user = session.query(Node).filter(Node.user_id == user.id, Node.typename== "USER").first()
if node_user is None:
return Response({"detail":"Not Allowed"}, status=HTTP_401_UNAUTHORIZED)
for k, v in request.data.items():
setattr(node_user.hyperdata, key, val)
for k, v in request.data.items():
node_user.hyperdata[k] = v
# setattr(node_user.hyperdata, k, v)
# print(node_user.hyperdata)
# node_user.save_hyperdata()
session.add(node_user)
session.commit()
return Response({"detail":"Updated user parameters"}, status=HTTP_202_ACCEPTED)
node_user = session.query(Node).filter(Node.user_id == user.id, Node.typename== "USER").first()
print(node_user.hyperdata)
return Response({"detail":"Updated user parameters", "hyperdata": node_user.hyperdata}, status=HTTP_202_ACCEPTED)
......@@ -4,6 +4,7 @@ from django.views.generic import FormView
from django.shortcuts import redirect
from gargantext.models.users import User
from django import forms
from gargantext.models import Node, User
from gargantext.views.pages.projects import overview
......@@ -21,6 +22,25 @@ class LoginView(FormView):
if user is not None and user.is_active:
login(self.request, user)
node_user = session.query(Node).filter(Node.user_id == user.id, Node.typename== "USER").first()
if "language" not in node_user["hyperdata"].keys():
node_user.hyperdata["language"] = "fr"
session.add(node_user)
session.commit()
#user hasn't been found inside Node table
#create it from auth table => node table
if node_user is None:
node_user = Node(
typename = 'USER',
#in node = > name
#in user = > username
name = user.name,
user_id = user.id,
)
node_user.hyperdata = {"language":"fr"}
session.add(node_user)
session.commit()
return super(LoginView, self).form_valid(form)
else:
return self.form_invalid(form)
......
......@@ -5,7 +5,7 @@ from gargantext.models import *
from gargantext.constants import *
from gargantext.settings import *
from datetime import datetime
from .main import get_node_user
from .main import get_user_params
from gargantext.constants import USER_LANG
def _get_user_project_corpus(request, project_id, corpus_id):
......
......@@ -5,35 +5,23 @@ import datetime
from gargantext.util.generators import paragraphs, credits
from gargantext.constants import USER_LANG
def get_node_user(user):
#load user parameters from User(Node).hyperdata from request.user or cache.
node_user = session.query(Node).filter(Node.user_id == user.id, Node.typename== "USER").first()
if node_user is None:
try:
node_user = Node(
user_id = user.id,
typename = 'USER',
name = user.name,
)
except AttributeError:
node_user = Node(
user_id = user.id,
typename = 'USER',
name = "Anne Aunime",
)
#default language for now is 'fr'
node_user.hyperdata.language = "fr"
session.add(node_user)
session.commit()
return node_user
def get_user_node(user):
if user.is_authenticated:
node_user = session.query(Node).filter(Node.user_id == user.id, Node.typename== "USER").first()
return node_user
else:
return None
def get_user_params(user):
node_user = get_user_node(user)
if node_user is not None:
return node_user.hyperdata
return {}
def home(request):
'''Home describes the platform.
A video draws the narratives.
If not logged a project test is shown.
'''
user_node = get_node_user(request.user)
return render(
template_name = 'pages/main/home.html',
request = request,
......@@ -44,15 +32,15 @@ def home(request):
'paragraph_gargantua': paragraphs.gargantua(),
'paragraph_lorem' : paragraphs.lorem(),
'paragraph_tutoreil': paragraphs.tutoreil(),
'user_parameters': user_node.hyperdata,
'languages': USER_LANG,
'user_parameters': get_user_params(request.user)
},
)
def about(request):
'''About Gargantext, its team and sponsors
'''
user_node = get_node_user(request.user)
return render(
template_name = 'pages/main/about.html',
request = request,
......@@ -63,7 +51,7 @@ def about(request):
'institutions': credits.institutions(),
'labos': credits.labs(),
'grants': credits.grants(),
'user_parameters': user_node.hyperdata,
'user_parameters': get_user_params(request.user),
'languages': USER_LANG,
},
)
......@@ -87,6 +75,6 @@ def maintenance(request):
context = {
'user': request.user,
'date': datetime.datetime.now(),
'user_parameters': user_node.hyperdata
'user_parameters': get_user_params(request.user)
},
)
......@@ -4,7 +4,7 @@ from gargantext.util.db_cache import cache
from gargantext.util.files import upload
from gargantext.models import *
from gargantext.constants import *
from .main import get_node_user
from .main import get_user_params
from gargantext.util.scheduling import scheduled
from gargantext.util.toolchain import parse_extract_indexhyperdata
......@@ -18,7 +18,7 @@ def get_node_user(user):
if node_user is None:
node_user = Node(user_id = user.id,
typename = 'USER',
username = user.name,
name = user.name,
)
#default language for now is 'fr'
node_user.hyperdata["language"] = "fr"
......@@ -67,7 +67,7 @@ def overview(request):
# projects owned by the user
'number': user_projects.count(),
'projects': user_projects,
"user_parameters":node_user.hyperdata,
'user_parameters': get_user_params(request.user),
'languages': USER_LANG,
# projects owned by the user's contacts
'common_users': (contact for contact, projects in contacts_projects),
......@@ -190,7 +190,7 @@ def project(request, project_id):
'form': NewCorpusForm,
'user': request.user,
"user_parameters":node_user.hyperdata,
'user_parameters': get_user_params(request.user),
'languages': USER_LANG,
'date': datetime.now(),
'project': project,
......@@ -211,7 +211,7 @@ def project(request, project_id):
template_name = 'pages/projects/project.html',
request = request,
context = {
"user_parameters":node_user.hyperdata,
'user_parameters': get_user_params(request.user),
"languages": USER_LANG,
'form': NewCorpusForm,
'user': request.user,
......
......@@ -4,7 +4,7 @@ from gargantext.util.db_cache import cache
from gargantext.models import Node
from gargantext.constants import get_resource
from gargantext.constants import USER_LANG
from .main import get_node_user
from .main import get_user_params
from datetime import datetime
......@@ -53,7 +53,7 @@ def ngramtable(request, project_id, corpus_id):
'importroute': "/api/ngramlists/import?onto_corpus=%i"% corpus.id,
'corporainfos' : corpora_infos,
#user params
'user_parameters': user_node.hyperdata,
'user_parameters': get_user_params(request.user),
'languages': USER_LANG
},
)
......@@ -6,7 +6,7 @@ from gargantext.constants import *
from gargantext.settings import *
from gargantext.constants import USER_LANG
from datetime import datetime
from gargantext.views.pages.main import get_node_user
from gargantext.views.pages.main import get_user_params
@requires_auth
def explorer(request, project_id, corpus_id):
......@@ -48,7 +48,7 @@ def explorer(request, project_id, corpus_id):
'corpus' : corpus ,
'maplist_id': maplist_id ,
'view' : 'graph' ,
'user_parameters': node_user.hyperdata,
'user_parameters': get_user_params(request.user),
'languages': USER_LANG
},
)
......
......@@ -180,11 +180,9 @@ lang = $("a#lang").data("lang")
loadHelp(lang)
//change lang
$("a.new_lang").on("click", function(){
//close all popo
//close all popover while changing lang
$('.popover').popover('hide');
new_lang = $(this).data("lang")
//new_lang.setAttr("data-lang", lang)
loadHelp(new_lang);
//current lang in tab
$("a#lang").attr("data-lang", new_lang);
......@@ -203,27 +201,28 @@ $("a.new_lang").on("click", function(){
label = $("a.new_lang > span")
label.text(lang)
// console.log("AJAX")
// $.ajax({
// url: '/api/users/?'+jQuery.param({"default_language":new_lang}),
// type: 'PUT',
// beforeSend: function(xhr) {
// xhr.setRequestHeader("X-CSRFToken", getCookie("csrftoken"));
// },
// success: function(response) {
// console.log(response);
// console.log("EDIT SUCCESS!");
// $("a#lang").attr("data-lang", new_lang);
$.ajax({
url: '/api/user/parameters/',
type: 'PUT',
data: {"language":new_lang},
beforeSend: function(xhr) {
xhr.setRequestHeader("X-CSRFToken", getCookie("csrftoken"));
},
success: function(response) {
console.log(response);
console.log("EDIT SUCCESS!");
//$("a#lang").attr("data-lang", new_lang);
// console.log("a#lang")
// //$("a#lang").next("img").attr({"value": new_lang, "src":"/static/img/"+new_lang+".png")
// //$("a.new_lang").attr("data-lang", new_lang);
// //$("a.new_lang").next("img").attr({"value": lang, "src":"/static/img/"+lang+".png")
// //window.location.reload()
// },
// error: function(xhr) {
// console.log("EDIT FAIL!")
//
// },
// });
},
error: function(xhr) {
console.log("EDIT FAIL!")
},
});
});
......@@ -244,7 +243,7 @@ function loadHelp(lang){
info = help[div_id]
console.log(lang)
console.log(info[lang]["content"])
help_btn = '<span class="glyphicon glyphicon-question-sign help-btn" tab-index=0 data-container="body" data-toggle="popover" data-placement="'+info[lang]["placement"]+'" title="'+info[lang]["title"]+'" data-content="'+info[lang]["content"]+'"></span>'
help_btn = '<span class="glyphicon glyphicon-question-sign help-btn" data-html="true" tab-index=0 data-container="body" data-toggle="popover" data-placement="'+info[lang]["placement"]+'" title="'+info[lang]["title"]+'" data-content="'+info[lang]["content"]+'"></span>'
if (info["position"] == "inside"){
......
......@@ -120,8 +120,6 @@
<a class="new_lang" data-lang="{{lang}}"><img value="{{user_parameters.language}}" src="/static/img/{{lang}}.png" width="25%"/> <span>{{lang}}</span> </a></li>
{% endif %}
{% endfor %}
</ul>
</li>
{% endif %}
......
......@@ -57,7 +57,7 @@
<center>
<a data-toggle="modal" href="#addcorpus" >
<a data-toggle="modal" href="#addcorpus" >
<button
type="button"
class="btn btn-primary btn-lg"
......@@ -68,7 +68,7 @@
<span class="glyphicon glyphicon-plus" aria-hidden="true" ></span>
Add a corpus
</button>
<p class="help" id="corpus-btn"></p>
<p class="help" id="corpus-btn"></p>
</a>
</center>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment