Commit f50b1e47 authored by Stephane Glondu's avatar Stephane Glondu
Browse files

Make templates user-aware

Move user reference to Helios_services so that it can be queried from
templates.
parent 273a2f11
......@@ -6,8 +6,18 @@ let welcome_message = "This is the default message"
let s x = Xml.uri_of_string ("/static/" ^ x)
let format_user u size = Helios_services.([
img
~src:(Printf.ksprintf s "auth/login-icons/%s.png" u.user_type)
~a:[a_style "border:0;"; a_height size]
~alt:u.user_type ();
pcdata " ";
pcdata u.user_name;
])
let base ~title ~header ~content =
html ~a:[a_dir `Ltr; a_xml_lang "en"]
lwt user = Eliom_reference.get Helios_services.user in
Lwt.return (html ~a:[a_dir `Ltr; a_xml_lang "en"]
(head (Eliom_content.Html5.F.title (pcdata (title ^ " - Helios"))) [
link
~rel:[`Stylesheet]
......@@ -38,19 +48,33 @@ let base ~title ~header ~content =
br ();
] @ header);
div ~a:[a_id "contentbody"] content;
div ~a:[a_id "footer"] [
span ~a:[a_style "float:right;"] [ (* footer logo *) ];
(* if user/voter... *)
pcdata "not logged in.";
br ();
a ~service:Helios_services.project_home [
pcdata "About Helios"
] ();
div ~a:[a_id "footer"] (
[span ~a:[a_style "float:right;"] [ (* footer logo *) ]] @
(match user with
| Some (admin_p, user) ->
[pcdata "logged in as "] @ (format_user user 15) @ [
pcdata " [";
a ~service:Helios_services.logout [pcdata "logout"] ();
pcdata "]";
br ()
]
| None ->
[pcdata "not logged in."] @ [
pcdata " [";
a ~service:Helios_services.login [pcdata "log in"] ();
pcdata "]";
br ();
]
) @ [
a ~service:Helios_services.project_home [
pcdata "About Helios | Help!"
] ();
(* footer links *)
br ~a:[a_style "clear:right;"] ();
];
br ~a:[a_style "clear:right;"] ();
]
)
];
])
]))
let not_implemented title = base
~title
......@@ -72,25 +96,11 @@ let login_box auth_systems = List.map
]
) auth_systems
type user = {
user_name : string;
user_type : string;
}
let format_user u size = [
img
~src:(Printf.ksprintf s "auth/login-icons/%s.png" u.user_type)
~a:[a_style "border:0;"; a_height size]
~alt:u.user_type ();
pcdata " ";
pcdata u.user_name;
]
type election = {
election_short_name : string;
election_name : string;
election_description : string;
election_admin : user;
election_admin : Helios_services.user;
}
let format_one_election e =
......@@ -113,12 +123,16 @@ let format_one_featured_election e =
br ();
]
let index ~mystuff ~featured = base
let index ~featured =
lwt user = Eliom_reference.get Helios_services.user in
base
~title:site_title
~header:[h2 [pcdata site_title]]
~content:(
let mystuff_box = match mystuff with
| `User (u, administered, voted) ->
let mystuff = match user with
| Some (admin_p, u) ->
let administered = if admin_p then Some [] else None in
let voted = [] in
let administration_box = match administered with
| Some admin ->
let administered_box = match admin with
......@@ -153,9 +167,9 @@ let index ~mystuff ~featured = base
(format_user u 25)
]
@ administration_box @ recent_votes
| `Auth_systems auth_systems ->
| None ->
[h3 [pcdata "Log In to Start Voting"]]
@ (login_box auth_systems)
@ (login_box Helios_services.auth_systems)
@ [br (); br ()]
in
let featured_box = match featured with
......@@ -169,7 +183,7 @@ let index ~mystuff ~featured = base
h4 [pcdata "no featured elections at the moment"];
]
in ([
div ~a:[a_id "mystuff"] mystuff_box;
div ~a:[a_id "mystuff"] mystuff;
p ~a:[a_style "font-size: 1.4em;"] [pcdata welcome_message];
] @ featured_box @ [
br ~a:[a_style "clear:right;"] ();
......
open Lwt
let user = Eliom_reference.eref
~scope:Eliom_common.session
None
let auth_systems = [
"dummy";
]
let get_featured_elections () =
let open Helios_services in
let open Helios_templates in
return [
{
......@@ -25,28 +18,23 @@ let get_featured_elections () =
let () = Eliom_registration.Html5.register
~service:Helios_services.home
(fun () () ->
lwt user = Eliom_reference.get user in
let mystuff = match user with
| Some (admin_p, u) -> `User (u, (if admin_p then Some [] else None), [])
| None -> `Auth_systems auth_systems
in
lwt featured = get_featured_elections () in
return (Helios_templates.index ~mystuff ~featured))
Helios_templates.index ~featured)
let () = Eliom_registration.Html5.register
~service:Helios_services.elections_administered
(fun () () ->
return (Helios_templates.not_implemented "Administrate elections"))
Helios_templates.not_implemented "Administrate elections")
let () = Eliom_registration.Html5.register
~service:Helios_services.election_new
(fun () () ->
return (Helios_templates.not_implemented "Create election"))
Helios_templates.not_implemented "Create election")
let () = Eliom_registration.Html5.register
~service:Helios_services.election_shortcut
(fun _ () ->
return (Helios_templates.not_implemented "Election shortcut"))
Helios_templates.not_implemented "Election shortcut")
let () = Eliom_registration.Html5.register
~service:Helios_services.login
......@@ -58,12 +46,11 @@ let () = Eliom_registration.Html5.register
~scope:Eliom_common.session
(fun () (user_name, admin_p) ->
let user_type = "dummy" in
Eliom_reference.set user
(Some (admin_p,
Helios_templates.({user_name; user_type}))) >>
Eliom_reference.set Helios_services.user
(Some (admin_p, Helios_services.({user_name; user_type}))) >>
return Helios_services.home)
in
return (Helios_templates.dummy_login ~service))
Helios_templates.dummy_login ~service)
let () = Eliom_registration.Redirection.register
~service:Helios_services.logout
......
......@@ -44,3 +44,16 @@ let perform_login () =
~fallback:login
~post_params:Eliom_parameter.(string "username" ** bool "admin_p")
()
let auth_systems = [
"dummy";
]
type user = {
user_name : string;
user_type : string;
}
let user = Eliom_reference.eref
~scope:Eliom_common.session
(None : (bool * user) option)
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