accurator/commit

User related functions are now sepperated in Accurator user file.

authorChris Dijkshoorn
Sun Mar 8 13:13:44 2015 +0000
committerChris Dijkshoorn
Sun Mar 8 13:13:44 2015 +0000
commit4b57344bb8cb6524abdc869204f1af505e27f670
treeac78337e4658df33fca613195e3ed0dafa709afb
parent76bbb81473f9cca1e1d83cf8e71db4fed6668362
Diff style: patch stat
diff --git a/api/accurator.pl b/api/accurator.pl
index 6179659..825c6c6 100644
--- a/api/accurator.pl
+++ b/api/accurator.pl
@@ -3,12 +3,13 @@
 /** <module> Accurator
 */
 
+:- use_module(library(accurator/accurator_user)).
+:- use_module(library(accurator/annotate_page)).
 :- use_module(library(accurator/domain)).
 :- use_module(library(accurator/expertise)).
-:- use_module(library(accurator/ui_elements)).
-:- use_module(library(accurator/annotate_page)).
 :- use_module(library(accurator/recommendation/strategy_random)).
 :- use_module(library(accurator/recommendation/strategy_expertise)).
+:- use_module(library(accurator/ui_elements)).
 :- use_module(library(semweb/rdf_db)).
 :- use_module(library(http/http_dispatch)).
 :- use_module(library(http/http_server_files)).
@@ -75,25 +76,6 @@ get_parameters_elements(Request, Options) :-
 	]),
     Options = [ui(UI), locale(Locale), type(Type)].
 
-%%	domain_settings_api(+Request)
-%
-%	Retrieves the settings specific to a domain.
-domains_api(Request) :-
-    get_parameters_domain(Request, Options),
-	get_domain_settings(Dic, Options),
-	reply_json_dict(Dic).
-
-%%	get_parameters_domain(+Request, -Options)
-%
-%	Retrieves an option list of parameters from the url.
-get_parameters_domain(Request, Options) :-
-    http_parameters(Request,
-        [domain(Domain,
-		    [description('The domain'),
-			 optional(true)])]),
-    Options = [domain(Domain)].
-
-
 %%	recently_annotated_api(+Request)
 %
 %	Retrieves a list of artworks the user recently annotated.
@@ -110,21 +92,23 @@ get_parameters_annotated(Request, Options) :-
         [user(User, [description('The user'), optional(false)])]),
     Options = [user(User)].
 
-%%	get_annotated(-Dic, +Options)
+%%	domain_settings_api(+Request)
 %
-%	Query for a list of artworks the user recently annotated.
-get_annotated(Dic, Options) :-
-	option(user(User), Options),
-	setof(Uri, Annotation^User^
-		  (	  rdf(Annotation, oa:annotatedBy, User),
-			  rdf(Annotation, oa:hasTarget, Uri),
-			  rdf(Uri, rdf:type, edm:'ProvidedCHO')),
-		  Uris),
-	!,
-	Dic = artworks{uris:Uris}.
+%	Retrieves the settings specific to a domain.
+domains_api(Request) :-
+    get_parameters_domain(Request, Options),
+	get_domain_settings(Dic, Options),
+	reply_json_dict(Dic).
 
-get_annotated(Dic, _Options) :-
-	Dic = artworks{uris:[]}.
+%%	get_parameters_domain(+Request, -Options)
+%
+%	Retrieves an option list of parameters from the url.
+get_parameters_domain(Request, Options) :-
+    http_parameters(Request,
+        [domain(Domain,
+		    [description('The domain'),
+			 optional(true)])]),
+    Options = [domain(Domain)].
 
 %%	expertise_topics_api(+Request)
 %
@@ -140,21 +124,21 @@ expertise_topics_api(Request) :-
 get_parameters_expertise(Request, Options) :-
     http_parameters(Request,
         [locale(Locale,
-				[description('Locale of language elements to retrieve'),
-				 optional(false)]),
+		    [description('Locale of language elements to retrieve'),
+			 optional(false)]),
 		taxonomy(Taxonomy,
-				[description('Domain specific taxonomy.'),
-				 optional(false)]),
+			[description('Domain specific taxonomy.'),
+			 optional(false)]),
 		number_of_topics(NumberOfTopicsString,
-				[description('The maximum number of topics to retrive'),
-				 optional(false)]),
+			[description('The maximum number of topics to retrive'),
+			 optional(false)]),
 		top_concept(TopConcept,
-				[description('The top concept to start searching form.'),
-				 optional(false)]),
+			[description('The top concept to start searching form.'),
+			 optional(false)]),
 		number_of_children_shown(NumberOfChildrenString,
-				[description('The number of child concepts shown.'),
-				 optional(true),
-				 default(3)])]),
+			[description('The number of child concepts shown.'),
+			 optional(true),
+			 default(3)])]),
     atom_number(NumberOfTopicsString, NumberOfTopics),
 	atom_number(NumberOfChildrenString, NumberOfChildren),
 	Options = [locale(Locale), taxonomy(Taxonomy),
@@ -186,68 +170,6 @@ expertise_values_api(Request) :-
 	reply_html_page(/,
 					title('Assert expertise values'),
 						h1('Successfully asserted expertise values')).
-%%	register_user(+Request)
-%
-%	Register a user using information within a json object.
-register_user(Request) :-
-	http_read_json_dict(Request, JsonIn),
-	atom_string(User, JsonIn.user),
-	(   current_user(User)
-	->  throw(error(permission_error(create, user, User),
-			context(_, 'User already exists')))
-	;   atom_string(JsonIn.password, Password),
-		password_hash(Password, Hash),
-		atom_string(RealName, JsonIn.name),
-			Allow = [ read(_,_), write(_,annotate) ],
-		user_add(User, [realname(RealName), password(Hash), allow(Allow)]),
-		reply_html_page(/,
-					title('Register user'),
-						h1('Successfully registered user'))
-	).
-
-%%	get_user(+Request)
-%
-%	Get the id of a user.
-get_user(_Request) :-
-	logged_on(User),
-	user_property(User, realname(RealName)),
-	reply_json_dict(user{user:User, real_name:RealName}).
-
-%%	get_user_settings(+Request)
-%
-%	Return saved domain and locale of user.
-get_user_settings(_Request) :-
-	logged_on(User),
-	get_domain(User, Domain),
-	get_locale(User, Locale),
-	reply_json_dict(settings{locale:Locale, domain:Domain}).
-
-get_domain(User, Domain) :-
-	user_property(User, domain(Domain)), !.
-get_domain(_User, "").
-
-get_locale(User, Locale) :-
-	user_property(User, locale(Locale)), !.
-get_locale(_User, "").
-
-%%	save_additional_info(+Request)
-%
-%	Saves the additional information about a user.
-save_user_info(Request) :-
-	http_read_json_dict(Request, Info0),
-	atom_string(User, Info0.user),
-	del_dict(user, Info0, _Value, Info),
-	dict_pairs(Info, elements, InfoPairs),
-	save_info_pairs(User, InfoPairs),
-	reply_html_page(/,
-					title('Save additional info'),
-						h1('Additional info successfuly saved.')).
-
-save_info_pairs(_User, []).
-save_info_pairs(User, [Property-Value|Pairs]) :-
-	InfoAtom =.. [Property, Value],
-	set_user_property(User, InfoAtom),
-	save_info_pairs(User, Pairs).
 
 %%  recommendation_api(+Request)
 %
@@ -265,10 +187,10 @@ get_recommendation_parameters(Request, Options) :-
 	logged_on(User),
     http_parameters(Request,
         [strategy(Strategy,
-			  [default(random),
-			   oneof([random, expertise])]),
+		    [default(random),
+			 oneof([random, expertise])]),
 		 target(Target,
-			  [default('http://www.europeana.eu/schemas/edm/ProvidedCHO')])
+			[default('http://www.europeana.eu/schemas/edm/ProvidedCHO')])
 		]),
     Options = [strategy(Strategy), user(User),
 			   target(Target)].
diff --git a/lib/accurator/accurator_user.pl b/lib/accurator/accurator_user.pl
new file mode 100644
index 0000000..4713f47
--- /dev/null
+++ b/lib/accurator/accurator_user.pl
@@ -0,0 +1,96 @@
+:- module(accurator_user, [get_annotated/2,
+				 get_domain/2,
+				 get_locale/2,
+			     register_user/1,
+				 get_user/1,
+				 get_user_settings/1,
+				 save_user_info/1]).
+
+/** <module> Domain
+*/
+:- use_module(library(semweb/rdf_db)).
+:- use_module(library(http/http_json)).
+:- use_module(library(http/html_write)).
+:- use_module(user(user_db)).
+
+:- rdf_register_prefix(edm, 'http://www.europeana.eu/schemas/edm/').
+:- rdf_register_prefix(oa, 'http://www.w3.org/ns/oa#').
+
+%%	get_annotated(-Dic, +Options)
+%
+%	Query for a list of artworks the user recently annotated.
+get_annotated(Dic, Options) :-
+	option(user(User), Options),
+	setof(Uri, Annotation^User^
+		  (	  rdf(Annotation, oa:annotatedBy, User),
+			  rdf(Annotation, oa:hasTarget, Uri),
+			  rdf(Uri, rdf:type, edm:'ProvidedCHO')),
+		  Uris),
+	!,
+	Dic = artworks{uris:Uris}.
+
+get_annotated(Dic, _Options) :-
+	Dic = artworks{uris:[]}.
+
+%%	register_user(+Request)
+%
+%	Register a user using information within a json object.
+register_user(Request) :-
+	http_read_json_dict(Request, JsonIn),
+	atom_string(User, JsonIn.user),
+	(   current_user(User)
+	->  throw(error(permission_error(create, user, User),
+			context(_, 'User already exists')))
+	;   atom_string(JsonIn.password, Password),
+		password_hash(Password, Hash),
+		atom_string(RealName, JsonIn.name),
+			Allow = [ read(_,_), write(_,annotate) ],
+		user_add(User, [realname(RealName), password(Hash), allow(Allow)]),
+		reply_html_page(/,
+					title('Register user'),
+						h1('Successfully registered user'))
+	).
+
+%%	get_user(+Request)
+%
+%	Get the id of a user.
+get_user(_Request) :-
+	logged_on(User),
+	user_property(User, realname(RealName)),
+	reply_json_dict(user{user:User, real_name:RealName}).
+
+%%	get_user_settings(+Request)
+%
+%	Return saved domain and locale of user.
+get_user_settings(_Request) :-
+	logged_on(User),
+	get_domain(User, Domain),
+	get_locale(User, Locale),
+	reply_json_dict(settings{locale:Locale, domain:Domain}).
+
+get_domain(User, Domain) :-
+	user_property(User, domain(Domain)), !.
+get_domain(_User, "").
+
+get_locale(User, Locale) :-
+	user_property(User, locale(Locale)), !.
+get_locale(_User, "").
+
+%%	save_additional_info(+Request)
+%
+%	Saves the additional information about a user.
+save_user_info(Request) :-
+	http_read_json_dict(Request, Info0),
+	atom_string(User, Info0.user),
+	del_dict(user, Info0, _Value, Info),
+	dict_pairs(Info, elements, InfoPairs),
+	save_info_pairs(User, InfoPairs),
+	reply_html_page(/,
+					title('Save additional info'),
+						h1('Additional info successfuly saved.')).
+
+save_info_pairs(_User, []).
+save_info_pairs(User, [Property-Value|Pairs]) :-
+	InfoAtom =.. [Property, Value],
+	set_user_property(User, InfoAtom),
+	save_info_pairs(User, Pairs).