From 47a525e545eb5b927e4aaf9da9224326927b8427 Mon Sep 17 00:00:00 2001 From: himangshu Date: Sat, 31 May 2014 09:00:20 +0530 Subject: [PATCH] Handle list of bitstrings present in model Currently there was no way to edit and update list of strings this has been taken care of by 1. adding a metadata called list fields in the model 2. all list fields are displayed in csv format 3. when editing the list fields will be split by comma and then pushed to db Signed-off-by: himangshu --- src/controller/cb_admin_model_controller.erl | 69 +++++++++++++++----- src/view/model/edit.html | 12 +++- src/view/model/model.html | 8 +++ src/view/model/show.html | 10 ++- 4 files changed, 79 insertions(+), 20 deletions(-) diff --git a/src/controller/cb_admin_model_controller.erl b/src/controller/cb_admin_model_controller.erl index ff819c1..97feaf4 100644 --- a/src/controller/cb_admin_model_controller.erl +++ b/src/controller/cb_admin_model_controller.erl @@ -13,7 +13,7 @@ heartbeat('POST', [WatchName], Authorization) -> watch('POST', [], Authorization) -> TopicString = Req:post_param("topic_string"), - {ok, WatchId} = boss_news:watch(TopicString, fun cb_admin_lib:push_update/3, "admin"++SessionID, 60), + {ok, WatchId} = boss_news:watch(TopicString, fun cb_admin_lib:push_update/3, "admin"++SessionID, 60), {json, [{watch_id, WatchId}]}. events('GET', [Since], Authorization) -> @@ -22,8 +22,8 @@ events('GET', [Since], Authorization) -> {json, [{messages, Messages}, {timestamp, Timestamp}]}. model('GET', [], Authorization) -> - {ok, [{model_section, true}, {records, []}, - {models, boss_web:get_all_models()}, + {ok, [{model_section, true}, {records, []}, + {models, boss_web:get_all_models()}, {this_model, ""}, {topic_string, ""}, {timestamp, now()}]}; model('GET', [ModelName], Authorization) -> @@ -31,13 +31,14 @@ model('GET', [ModelName], Authorization) -> model('GET', [ModelName, PageName], Authorization) -> Page = list_to_integer(PageName), Model = list_to_atom(ModelName), + ModelsPropLists = get_list_elements(Model), RecordCount = boss_db:count(Model), - Records = boss_db:find(Model, [], [{limit, ?RECORDS_PER_PAGE}, + Records = boss_db:find(Model, [], [{limit, ?RECORDS_PER_PAGE}, {offset, (Page - 1) * ?RECORDS_PER_PAGE}, descending]), TopicString = string:join(lists:map(fun(Record) -> Record:id() ++ ".*" end, Records), ", "), AttributesWithDataTypes = lists:map(fun(Record) -> {Record:id(), lists:map(fun({Key, Val}) -> - {Key, Val, boss_db:data_type(Key, Val)} + {Key, Val, proplists:get_value(Key, ModelsPropLists, boss_db:data_type(Key, Val))} end, Record:attributes())} end, Records), AttributeNames = case length(Records) of @@ -45,13 +46,19 @@ model('GET', [ModelName, PageName], Authorization) -> _ -> (lists:nth(1, Records)):attribute_names() end, Pages = lists:seq(1, ((RecordCount-1) div ?RECORDS_PER_PAGE)+1), - {ok, - [{records, AttributesWithDataTypes}, {attribute_names, AttributeNames}, - {models, boss_web:get_all_models()}, {this_model, ModelName}, + {ok, + [{records, AttributesWithDataTypes}, {attribute_names, AttributeNames}, + {models, boss_web:get_all_models()}, {this_model, ModelName}, {pages, Pages}, {this_page, Page}, {model_section, true}, - {topic_string, TopicString}, {timestamp, boss_mq:now("admin"++SessionID)}], + {topic_string, TopicString}, {timestamp, boss_mq:now("admin"++SessionID)}], [{"Cache-Control", "no-cache"}]}. +get_list_elements(Model) -> + ModelAttributes = apply(Model, module_info, [attributes]), + ModelLists = proplists:get_value(list_fields, ModelAttributes, []), + ModelsPropLists = lists:map(fun(K) -> {K, "lists"} end, ModelLists), + ModelsPropLists. + csv('GET', [ModelName], Authorization) -> Model = list_to_atom(ModelName), [First|_] = Records = boss_db:find(Model, [], [descending]), @@ -69,7 +76,7 @@ csv('GET', [ModelName], Authorization) -> [cb_admin_model_lib:encode_csv_value(Val), ","|Acc] end, [], Record:attributes()), "\n"] end, Records), - {output, [FirstLine, RecordLines], [{"Content-Type", "text/csv"}, + {output, [FirstLine, RecordLines], [{"Content-Type", "text/csv"}, {"Content-Disposition", "attachment;filename="++ModelName++".csv"}]}. upload('GET', [ModelName], Authorization) -> @@ -99,17 +106,27 @@ upload('POST', [ModelName], Authorization) -> show('GET', [RecordId], Authorization) -> Record = boss_db:find(RecordId), + Module = element(1, Record), + ModelsPropLists = get_list_elements(Module), AttributesWithDataTypes = lists:map(fun({Key, Val}) -> - {Key, Val, boss_db:data_type(Key, Val)} + {Key, Val, proplists:get_value(Key, ModelsPropLists, boss_db:data_type(Key, Val))} end, Record:attributes()), - {ok, [{'record', Record}, {'attributes', AttributesWithDataTypes}, + {ok, [{'record', Record}, {'attributes', AttributesWithDataTypes}, {'type', boss_db:type(RecordId)}, {timestamp, boss_mq:now("admin"++SessionID)}]}. edit('GET', [RecordId], Authorization) -> Record = boss_db:find(RecordId), - {ok, [{'record', Record}]}; + Module = element(1, Record), + ModelsPropLists = get_list_elements(Module), + AttributesWithDataTypes = lists:map(fun({Key, Val}) -> + {Key, Val, proplists:get_value(Key, ModelsPropLists, boss_db:data_type(Key, Val))} + end, + Record:attributes()), + {ok, [{'record', Record}, {'attributes', AttributesWithDataTypes}]}; edit('POST', [RecordId], Authorization) -> Record = boss_db:find(RecordId), + Module = element(1, Record), + ModelsPropLists = get_list_elements(Module), NewRecord = lists:foldr(fun ('id', Acc) -> Acc; @@ -121,9 +138,19 @@ edit('POST', [RecordId], Authorization) -> case Val of "now" -> Acc:set(Attr, erlang:now()); _ -> Acc end; - false -> Acc:set(Attr, Val) - end - end, Record, Record:attribute_names()), + false -> + case proplists:get_value(Attr, ModelsPropLists, false) of + "lists" -> + Val1 = string:tokens(Val, ","), + lager:info("Tokens are ~p", [Val1]), + {TrimmedVals, _} = lists:split(length(Val1) - 1, Val1), + lager:info("List Val is ~p", [TrimmedVals]), + Acc:set(Attr, lists:map(fun(V) -> list_to_bitstring(string:strip(V)) end, TrimmedVals)); + false -> Acc:set(Attr, Val) + end + end + end, + Record, Record:attribute_names()), case NewRecord:save() of {ok, SavedRecord} -> {redirect, [{action, "show"}, {record_id, RecordId}]}; @@ -142,6 +169,7 @@ create(Method, [RecordType], Authorization) -> case lists:member(RecordType, boss_web:get_all_models()) of true -> Module = list_to_atom(RecordType), + ModelsPropLists = get_list_elements(Module), DummyRecord = boss_record_lib:dummy_record(Module), case Method of 'GET' -> @@ -158,7 +186,14 @@ create(Method, [RecordType], Authorization) -> "now" -> erlang:now(); _ -> "" end; - _ -> Val + _ -> + case proplists:get_value(Attr, ModelsPropLists, false) of + "lists" -> + ValToken = string:tokens(Val, ","), + {TrimmedVals, _} = lists:split(length(ValToken) - 1, ValToken), + lists:map(fun(V) -> list_to_bitstring(string:strip(V)) end, TrimmedVals); + false -> Val + end end, Acc:set(Attr, Val1) end, DummyRecord, DummyRecord:attribute_names()), diff --git a/src/view/model/edit.html b/src/view/model/edit.html index 71fe357..0e69531 100644 --- a/src/view/model/edit.html +++ b/src/view/model/edit.html @@ -12,14 +12,22 @@

Editing {{ record.id }}

{% endif %}
- {% for key, val in record.attributes %} + {% for key, val, datatype in attributes %} {% ifnotequal key "id" %} - {% if "_time" in key %} + {% if datatype == "datetime" %} + {% else %} {% endif %} + {% endif %} {% endifnotequal %} {% endfor %}
{{ key }}   + {% else %} + {% if datatype == "lists" %} +
diff --git a/src/view/model/model.html b/src/view/model/model.html index af5da6c..dd8547f 100644 --- a/src/view/model/model.html +++ b/src/view/model/model.html @@ -60,11 +60,19 @@

Models data management

{% if datatype == "string" or datatype == "binary" %} {{ val|truncatewords:8 }} {% else %} + {% if datatype == "lists" %} + + {% for elem in val %} + {{elem}} , + {% endfor %} + + {% else %} {{ val }} {% endif %} {% endif %} {% endif %} {% endif %} + {% endif %} {% endfor %} {% endfor %} diff --git a/src/view/model/show.html b/src/view/model/show.html index 4d4468e..aeb686f 100644 --- a/src/view/model/show.html +++ b/src/view/model/show.html @@ -31,9 +31,17 @@

Displaying {{ record.id }} ({{ val|linebreaksbr }} {% else %} + {% if datatype == "lists" %} + + {% for elem in val %} + {{elem}} , + {% endfor %} + + {% else %} {{ val }} {% endif %} {% endif %} + {% endif %}

{% endif %} {% endfor %} @@ -62,7 +70,7 @@

Displaying {{ record.id }} (

{% endif %} - {% endfor %} + {% endfor %}