Commit 82f5679b authored by Stephane Glondu's avatar Stephane Glondu
Browse files

Get election data from Ocsigen's configuration file

Use more abstract datatypes where possible:
 * Helios_datatypes_t.election
 * Uuidm.t
parent b3eed6a9
......@@ -108,19 +108,15 @@ type question = {
question : string;
}
type election = {
election_uuid : string;
election_short_name : string;
election_name : string;
election_description : string;
type election_extradata = {
election : Z.t Helios_datatypes_t.election;
election_admin : Helios_services.user;
election_questions : string list;
election_trustees : string list;
election_state : [`Finished of question list | `Stopped | `Started];
}
let format_one_election e =
li [pcdata e.election_name]
li [pcdata e.election.e_name]
let format_one_featured_election e =
[
......@@ -128,13 +124,13 @@ let format_one_featured_election e =
a
~service:(Eliom_service.preapply
Helios_services.election_shortcut
e.election_short_name)
e.election.e_short_name)
~a:[a_style "font-size: 1.4em;"]
[pcdata e.election_name] ();
[pcdata e.election.e_name] ();
pcdata " by ";
] @ format_user e.election_admin 15 @ [
br ();
pcdata e.election_description;
pcdata e.election.e_description;
]);
br ();
]
......@@ -246,20 +242,20 @@ let election_view ~election =
br ();
br ();
br ~a:[a_style "clear: left;"] ();
div ~a:[a_style "margin-bottom: 25px;margin-left: 15px; border-left: 1px solid #aaa; padding-left: 5px; font-size:1.3em;"] [pcdata election.election_description];
div ~a:[a_style "margin-bottom: 25px;margin-left: 15px; border-left: 1px solid #aaa; padding-left: 5px; font-size:1.3em;"] [pcdata election.election.e_description];
p ~a:[a_style "text-align: center; font-size: 1.5em;"] [
a ~service:(Eliom_service.preapply Helios_services.election_questions election.election_uuid) [
a ~service:(Eliom_service.preapply Helios_services.election_questions election.election.e_uuid) [
pcdata "questions (";
pcdata (string_of_int (List.length election.election_questions));
pcdata (string_of_int (Array.length election.election.e_questions));
pcdata ")";
] ();
(* FIXME: space (&nbsp) breaks the output *)
pcdata " | ";
a ~service:(Eliom_service.preapply Helios_services.election_voters election.election_uuid) [
a ~service:(Eliom_service.preapply Helios_services.election_voters election.election.e_uuid) [
pcdata "voters & ballots"
] ();
pcdata " | ";
a ~service:(Eliom_service.preapply Helios_services.election_trustees election.election_uuid) [
a ~service:(Eliom_service.preapply Helios_services.election_trustees election.election.e_uuid) [
pcdata "trustees (";
pcdata (string_of_int (List.length election.election_trustees));
pcdata ")";
......@@ -314,4 +310,4 @@ let election_view ~election =
]
)
in
base ~title:election.election_name ~header:[] ~content
base ~title:election.election.e_name ~header:[] ~content
open StdExtra
open Helios_datatypes_t
open Lwt
let election_file =
let res = ref None in
let open Ocsigen_extensions.Configuration in
Eliom_config.parse_config [
element
~name:"elections"
~obligatory:true
~attributes:[
attribute ~name:"file" ~obligatory:true (fun s -> res := Some s)
]
()
];
match !res with
| Some s -> s
| None -> raise (Ocsigen_extensions.Error_in_config_file
"could not find elections in configuration file")
let raw_elections =
Ocsigen_messages.debug
(fun () -> "Loading elections from " ^ election_file ^ "...");
Lwt_io.lines_of_file election_file |>
Lwt_stream.filter (fun s -> s <> "") |>
Lwt_stream.to_list |> Lwt_main.run |>
List.map (Helios_datatypes_j.election_of_string Core_datatypes_j.read_number)
let get_raw_election_by_uuid x =
List.find (fun e -> Uuidm.equal e.e_uuid x) raw_elections
let test_uuid =
match Uuidm.of_string "94c1a03e-1c48-11e2-8866-3cd92b7981b8" with
| Some u -> u
| None -> assert false
let elections =
let open Helios_services in
let open Helios_templates in
[
{
election_uuid = "94c1a03e-1c48-11e2-8866-3cd92b7981b8";
election_short_name = "editor";
election_name = "Best editor";
election_description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
election = get_raw_election_by_uuid test_uuid;
election_admin = {
user_name = "admin";
user_type = "dummy";
};
election_questions = [];
election_trustees = [];
election_state = `Finished [
{
......@@ -50,11 +81,11 @@ let get_featured_elections () =
let get_election_by_name x =
let open Helios_templates in
wrap2 List.find (fun e -> e.election_short_name = x) elections
wrap2 List.find (fun e -> e.election.e_short_name = x) elections
let get_election_by_uuid x =
let open Helios_templates in
wrap2 List.find (fun e -> e.election_uuid = x) elections
wrap2 List.find (fun e -> Uuidm.equal e.election.e_uuid x) elections
let () = Eliom_registration.Html5.register
~service:Helios_services.home
......@@ -79,7 +110,7 @@ let () = Eliom_registration.Redirection.register
lwt e = get_election_by_name name in
return (Eliom_service.preapply
Helios_services.election_view
e.Helios_templates.election_uuid)
e.Helios_templates.election.e_uuid)
with Not_found ->
raise_lwt Eliom_common.Eliom_404)
......
......@@ -58,22 +58,29 @@ let user = Eliom_reference.eref
~scope:Eliom_common.session
(None : (bool * user) option)
let uuid = Eliom_parameter.user_type
(fun x -> match Uuidm.of_string x with
| Some x -> x
| None -> invalid_arg "uuid")
Uuidm.to_string
"uuid"
let election_view = service
~path:["elections"; "view"]
~get_params:(string "uuid")
~get_params:uuid
()
let election_questions = service
~path:["elections"; "questions"]
~get_params:(string "uuid")
~get_params:uuid
()
let election_voters = service
~path:["elections"; "voters"]
~get_params:(string "uuid")
~get_params:uuid
()
let election_trustees = service
~path:["elections"; "trustees"]
~get_params:(string "uuid")
~get_params:uuid
()
StdExtra
Core_datatypes_j
Helios_datatypes_j
Helios_services
Helios_templates
Helios_registration
......@@ -21,11 +21,17 @@
<extension findlib-package="eliom.server"/>
<extension findlib-package="zarith"/>
<extension findlib-package="uuidm"/>
<extension findlib-package="atdgen"/>
<host charset="utf-8" hostfilter="*" defaulthostname="localhost">
<site path="static" charset="utf-8">
<static dir="_SRCDIR_/helios/static" />
</site>
<eliom module="_build/src/server.cma" />
<eliom module="_build/src/server.cma">
<elections file="tests/data/favorite-editor/election.json"/>
</eliom>
</host>
</server>
......
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