Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 52 additions & 17 deletions src/controller/cb_admin_model_controller.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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) ->
Expand All @@ -22,36 +22,43 @@ 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) ->
model('GET', [ModelName, "1"], 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
0 -> [];
_ -> (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]),
Expand All @@ -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) ->
Expand Down Expand Up @@ -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;
Expand All @@ -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}]};
Expand All @@ -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' ->
Expand All @@ -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()),
Expand Down
12 changes: 10 additions & 2 deletions src/view/model/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@ <h1>Editing {{ record.id }}</h1>
{% endif %}
<form name="edit" action="{% url action="edit" record_id=record.id %}" method="post">
<table>
{% for key, val in record.attributes %}
{% for key, val, datatype in attributes %}
{% ifnotequal key "id" %}
{% if "_time" in key %}
{% if datatype == "datetime" %}
<tr><td>{{ key }}</td><td><input type="radio" name="{{ key }}" value="now" id="{{ key }}_now" /><label for="{{ key }}_now">Now</label>
&nbsp; <input type="radio" name="{{ key }}" value="null" id="{{ key }}_null" checked="checked" /><label for="{{ key }}_null">{% if val %}{{ val|date }} {{ val|time }}{% else %}Null{% endif %}</label>
{% else %}
{% if datatype == "lists" %}
<tr><td><label for="{{ key }}_textarea">{{ key }}</label></td><td><textarea id="{{ key }}_textarea" name="{{ key }}">
{% for elem in val %}{{ elem|escape }},{%endfor %}

</textarea></td></tr>

{% else %}
<tr><td><label for="{{ key }}_textarea">{{ key }}</label></td><td><textarea id="{{ key }}_textarea" name="{{ key }}">{{ val|escape }}</textarea></td></tr>
{% endif %}
{% endif %}
{% endifnotequal %}
{% endfor %}
</table>
Expand Down
8 changes: 8 additions & 0 deletions src/view/model/model.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,19 @@ <h1>Models data management</h1>
{% if datatype == "string" or datatype == "binary" %}
<td id="{{ record_id }}-{{ key }}">{{ val|truncatewords:8 }}</td>
{% else %}
{% if datatype == "lists" %}
<td id="{{ record_id }}-{{ key }}">
{% for elem in val %}
{{elem}} ,
{% endfor %}
</td>
{% else %}
<td id="{{ record_id }}-{{ key }}">{{ val }}</td>
{% endif %}
{% endif %}
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
</tr>
{% endfor %}
Expand Down
10 changes: 9 additions & 1 deletion src/view/model/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@ <h1>Displaying {{ record.id }} (<a href="{% url action="edit" record_id=record.i
{% if datatype == "string" or datatype == "binary" %}
<span id="{{ record.id }}-{{ key }}">{{ val|linebreaksbr }}</span>
{% else %}
{% if datatype == "lists" %}
<span id="{{ record.id }}-{{ key }}">
{% for elem in val %}
{{elem}} ,
{% endfor %}
</span>
{% else %}
<span id="{{ record.id }}-{{ key }}">{{ val }}</span>
{% endif %}
{% endif %}
{% endif %}
<br /><br />
{% endif %}
{% endfor %}
Expand Down Expand Up @@ -62,7 +70,7 @@ <h1>Displaying {{ record.id }} (<a href="{% url action="edit" record_id=record.i
</tr>
</table><br /><br />
{% endif %}
{% endfor %}
{% endfor %}
<form name="delete" action="{% url action="delete" record_id=record.id %}">
<input type="submit" value="Delete {{ record.id }}?" />
</form>
Expand Down