Commit bb526d9f authored by Stephane Glondu's avatar Stephane Glondu

Move main/featured election handling to Web_persist

parent f41718ae
......@@ -136,12 +136,12 @@ lwt () =
let module W = (val w : WEB_ELECTION) in
if featured then (
let uuid = Uuidm.to_string W.election.e_params.e_uuid in
Web_site.add_featured_election uuid
Web_persist.add_featured_election uuid
) else return ()
)
) !import_dirs
lwt () =
match !main_election_uuid with
| Some uuid -> Web_site.set_main_election uuid
| Some uuid -> Web_persist.set_main_election uuid
| _ -> return ()
open Lwt
open Common
let election_states = Ocsipersist.open_table "election_states"
......@@ -8,3 +9,43 @@ let get_election_state x =
let set_election_state x s =
Ocsipersist.add election_states x s
let store = Ocsipersist.open_store "site"
lwt main_election =
Ocsipersist.make_persistent store "main_election" None
lwt featured =
Ocsipersist.make_persistent store "featured_elections" []
let add_featured_election x =
lwt the_featured = Ocsipersist.get featured in
if List.mem x the_featured then (
return ()
) else (
Ocsipersist.set featured (x :: the_featured)
)
let rec list_remove x = function
| [] -> []
| y :: ys -> if x = y then ys else y :: (list_remove x ys)
let remove_featured_election x =
lwt the_featured = Ocsipersist.get featured in
Ocsipersist.set featured (list_remove x the_featured)
let is_featured_election x =
lwt the_featured = Ocsipersist.get featured in
return (List.mem x the_featured)
let get_featured_elections () =
Ocsipersist.get featured
let get_main_election () =
Ocsipersist.get main_election
let set_main_election x =
Ocsipersist.set main_election (Some x)
let unset_main_election () =
Ocsipersist.set main_election None
val get_election_state : string -> [ `Open | `Closed ] Lwt.t
val set_election_state : string -> [ `Open | `Closed ] -> unit Lwt.t
val get_main_election : unit -> string option Lwt.t
val set_main_election : string -> unit Lwt.t
val unset_main_election : unit -> unit Lwt.t
val add_featured_election : string -> unit Lwt.t
val remove_featured_election : string -> unit Lwt.t
val is_featured_election : string -> bool Lwt.t
val get_featured_elections : unit -> string list Lwt.t
......@@ -32,10 +32,6 @@ open Web_services
let source_file = ref "belenios.tar.gz"
let spool_dir = ref "."
let rec list_remove x = function
| [] -> []
| y :: ys -> if x = y then ys else y :: (list_remove x ys)
let get_single_line x =
match_lwt Lwt_stream.get x with
| None -> return None
......@@ -63,8 +59,6 @@ open Eliom_registration
module LwtRandom = MakeLwtRandom (struct let rng = make_rng () end)
let store = Ocsipersist.open_store "site"
(* Persistent table, used to initialize the server. *)
let election_ptable = Ocsipersist.open_table "site_elections"
......@@ -80,12 +74,6 @@ let election_credtokens = Ocsipersist.open_table "site_credtokens"
(* In-memory table, indexed by UUID, contains closures. *)
let election_table = ref SMap.empty
lwt main_election =
Ocsipersist.make_persistent store "main_election" None
lwt featured =
Ocsipersist.make_persistent store "featured_elections" []
(* The following reference is there to cut a dependency loop:
S.register_election depends on S (via Templates). It will be set
to a proper value once we have called Templates.Make. *)
......@@ -99,34 +87,6 @@ include Web_site_auth
let import_election f = !import_election_ref f
let add_featured_election x =
lwt the_featured = Ocsipersist.get featured in
if List.mem x the_featured then (
return ()
) else if SMap.mem x !election_table then (
Ocsipersist.set featured (x :: the_featured)
) else (
Lwt.fail Not_found
)
let remove_featured_election x =
lwt the_featured = Ocsipersist.get featured in
Ocsipersist.set featured (list_remove x the_featured)
let is_featured_election x =
lwt the_featured = Ocsipersist.get featured in
return (List.mem x the_featured)
let set_main_election x =
if SMap.mem x !election_table then (
Ocsipersist.set main_election (Some x)
) else (
Lwt.fail Not_found
)
let unset_main_election () =
Ocsipersist.set main_election None
let install_authentication xs = !install_authentication_ref xs
module T = Web_templates
......@@ -292,10 +252,10 @@ let () = install_authentication_ref := fun auth_configs ->
let () = Any.register ~service:home
(fun () () ->
Eliom_reference.unset cont >>
match_lwt Ocsipersist.get main_election with
match_lwt Web_persist.get_main_election () with
| None ->
lwt featured =
Ocsipersist.get featured >>=
Web_persist.get_featured_elections () >>=
Lwt_list.map_p (fun x -> return @@ SMap.find x !election_table)
in
T.home ~featured () >>= Html5.send
......@@ -744,8 +704,8 @@ let () =
(fun (uuid, ()) featured ->
let uuid_s = Uuidm.to_string uuid in
lwt () =
if featured then add_featured_election uuid_s
else remove_featured_election uuid_s
if featured then Web_persist.add_featured_election uuid_s
else Web_persist.remove_featured_election uuid_s
in
Redirection.send
(preapply election_admin (uuid, ())))
......@@ -758,7 +718,7 @@ let () =
let w = SMap.find uuid_s !election_table in
let module W = (val w : WEB_ELECTION) in
lwt user = get_user () in
lwt is_featured = is_featured_election uuid_s in
lwt is_featured = Web_persist.is_featured_election uuid_s in
W.Z.admin user is_featured () ())
let () =
......
......@@ -27,9 +27,4 @@ val spool_dir : string ref
val import_election :
election_files -> (module REGISTRABLE_ELECTION) option Lwt.t
val set_main_election : string -> unit Lwt.t
val unset_main_election : unit -> unit Lwt.t
val add_featured_election : string -> unit Lwt.t
val remove_featured_election : string -> unit Lwt.t
val is_featured_election : string -> bool Lwt.t
val install_authentication : auth_config list -> unit
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