Skip to content

Commit

Permalink
Only make queries with the 'is_query_live' flag a live query. Also so…
Browse files Browse the repository at this point in the history
…me extra updates to m_rsc_update for later REST API work.

Fixes #344
(cherry picked from commit 3a51c40)

Conflicts:
	src/models/m_rsc_update.erl
  • Loading branch information
mworrell authored and Arjan Scherpenisse committed Oct 29, 2012
1 parent 5b0b29c commit 1b8f5e6
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 47 deletions.
19 changes: 14 additions & 5 deletions modules/mod_admin/templates/_admin_edit_content.query.tpl
Expand Up @@ -15,11 +15,20 @@
<a href="http://zotonic.com/documentation/761/the-query-search-model">documentation on the query arguments</a> on the Zotonic website. _} <a href="http://zotonic.com/documentation/761/the-query-search-model">documentation on the query arguments</a> on the Zotonic website. _}
</div> </div>


<div class="form-item clearfix"> <div class="control-group">
<label for="query">{_ Query _}</label> <label class="control-label" for="query">{_ Query _}</label>
<textarea id="{{ #query }}" name="query" rows="15">{{ r.query }}</textarea> <div class="controls">
{% wire id=#query type="change" postback={query_preview rsc_id=id div_id=#querypreview} delegate="resource_admin_edit" %} <textarea id="{{ #query }}" name="query" rows="15">{{ r.query }}</textarea>
</div> {% wire id=#query type="change" postback={query_preview rsc_id=id div_id=#querypreview} delegate="resource_admin_edit" %}
</div>
</div>

<div class="control-group">
<label class="checkbox">
<input type="checkbox" id="is_query_live" name="is_query_live" {% if r.is_query_live %}checked{% endif %}/>
{_ Live query, send notifications when matching items are updated or inserted. _}
</label>
</div>


<h3>{_ Query preview _}</h3> <h3>{_ Query preview _}</h3>
<div class="query-results" id="{{ #querypreview }}"> <div class="query-results" id="{{ #querypreview }}">
Expand Down
1 change: 1 addition & 0 deletions modules/mod_search/support/search_query.erl
Expand Up @@ -38,6 +38,7 @@ search(Query, Context) ->
case parse_query(Query1, Context, Start) of case parse_query(Query1, Context, Start) of
[] -> #search_result{}; [] -> #search_result{};
#search_result{} = Result -> Result; #search_result{} = Result -> Result;
Start -> #search_result{};
R -> R ->
%% add any sort terms %% add any sort terms
R1 = parse_sort(Query1, R), R1 = parse_sort(Query1, R),
Expand Down
9 changes: 7 additions & 2 deletions modules/mod_search/support/search_query_notify.erl
Expand Up @@ -46,8 +46,13 @@ watches_update(Id, Watches, Context) ->
undefined -> undefined ->
proplists:delete(Id, Watches); proplists:delete(Id, Watches);
Q -> Q ->
Props = search_query:parse_query_text(Q), case z_convert:to_bool(m_rsc:p(Id, is_query_live, Context)) of
[{Id, Props} | proplists:delete(Id, Watches)] true ->
Props = search_query:parse_query_text(Q),
[{Id, Props} | proplists:delete(Id, Watches)];
false ->
proplists:delete(Id, Watches)
end
end. end.


watches_remove(Id, Watches, _Context) -> watches_remove(Id, Watches, _Context) ->
Expand Down
80 changes: 40 additions & 40 deletions src/models/m_rsc_update.erl
Expand Up @@ -149,13 +149,14 @@ update(Id, Props, Options, Context) when is_integer(Id) orelse Id == insert_rsc
EscapeTexts = proplists:get_value(escape_texts, Options, true), EscapeTexts = proplists:get_value(escape_texts, Options, true),
AclCheck = proplists:get_value(acl_check, Options, true), AclCheck = proplists:get_value(acl_check, Options, true),
IsImport = proplists:is_defined(is_import, Options), IsImport = proplists:is_defined(is_import, Options),
% IsTransMerge = proplists:is_defined(is_trans_merge, Options, false),
IsEditable = Id == insert_rsc orelse z_acl:rsc_editable(Id, Context) orelse not(AclCheck), IsEditable = Id == insert_rsc orelse z_acl:rsc_editable(Id, Context) orelse not(AclCheck),


case IsEditable of case IsEditable of
true -> true ->
DateProps = recombine_dates(Props), DateProps = recombine_dates(Props),
TextProps = recombine_languages(DateProps, Context), TextProps = recombine_languages(DateProps, Context),
AtomProps = [ {z_convert:to_atom(P), V} || {P, V} <- TextProps ], AtomProps = [ {map_property_name(P), V} || {P, V} <- TextProps ],
FilteredProps = props_filter(props_trim(AtomProps), [], Context), FilteredProps = props_filter(props_trim(AtomProps), [], Context),
EditableProps = props_filter_protected(FilteredProps), EditableProps = props_filter_protected(FilteredProps),
AclCheckedProps = case z_acl:rsc_update_check(Id, EditableProps, Context) of AclCheckedProps = case z_acl:rsc_update_check(Id, EditableProps, Context) of
Expand Down Expand Up @@ -408,15 +409,14 @@ preflight_check(Id, [_H|T], Context) ->


%% @doc Remove whitespace around some predefined fields %% @doc Remove whitespace around some predefined fields
props_trim(Props) -> props_trim(Props) ->
props_trim(Props, []). [

case is_trimmable(P,V) of
props_trim([], Acc) -> true -> {P, z_string:trim(V)};
lists:reverse(Acc); false -> {P,V}
props_trim([{P,V}=Prop|T], Acc) -> end
case is_trimmable(P,V) of || {P,V} <- Props
true -> props_trim(T, [{P, z_string:trim(V)}|Acc]); ].
false -> props_trim(T, [Prop|Acc])
end.


%% @doc Remove properties the user is not allowed to change and convert some other to the correct data type %% @doc Remove properties the user is not allowed to change and convert some other to the correct data type
%% @spec props_filter(Props1, Acc, Context) -> Props2 %% @spec props_filter(Props1, Acc, Context) -> Props2
Expand Down Expand Up @@ -464,7 +464,6 @@ props_filter([{page_path, Path}|T], Acc, Context) ->
false -> false ->
props_filter(T, Acc, Context) props_filter(T, Acc, Context)
end; end;

props_filter([{slug, undefined}|T], Acc, Context) -> props_filter([{slug, undefined}|T], Acc, Context) ->
props_filter(T, [{slug, []} | Acc], Context); props_filter(T, [{slug, []} | Acc], Context);
props_filter([{slug, <<>>}|T], Acc, Context) -> props_filter([{slug, <<>>}|T], Acc, Context) ->
Expand All @@ -477,7 +476,6 @@ props_filter([{custom_slug, P}|T], Acc, Context) ->
props_filter(T, [{custom_slug, z_convert:to_bool(P)} | Acc], Context); props_filter(T, [{custom_slug, z_convert:to_bool(P)} | Acc], Context);
props_filter([{is_published, P}|T], Acc, Context) -> props_filter([{is_published, P}|T], Acc, Context) ->
props_filter(T, [{is_published, z_convert:to_bool(P)} | Acc], Context); props_filter(T, [{is_published, z_convert:to_bool(P)} | Acc], Context);

props_filter([{is_authoritative, P}|T], Acc, Context) -> props_filter([{is_authoritative, P}|T], Acc, Context) ->
case z_acl:is_allowed(use, mod_admin_config, Context) of case z_acl:is_allowed(use, mod_admin_config, Context) of
true -> true ->
Expand All @@ -487,7 +485,8 @@ props_filter([{is_authoritative, P}|T], Acc, Context) ->
end; end;
props_filter([{is_featured, P}|T], Acc, Context) -> props_filter([{is_featured, P}|T], Acc, Context) ->
props_filter(T, [{is_featured, z_convert:to_bool(P)} | Acc], Context); props_filter(T, [{is_featured, z_convert:to_bool(P)} | Acc], Context);

props_filter([{is_query_live, P}|T], Acc, Context) ->
props_filter(T, [{is_query_live, z_convert:to_bool(P)} | Acc], Context);
props_filter([{visible_for, Vis}|T], Acc, Context) -> props_filter([{visible_for, Vis}|T], Acc, Context) ->
VisibleFor = z_convert:to_integer(Vis), VisibleFor = z_convert:to_integer(Vis),
case VisibleFor of case VisibleFor of
Expand Down Expand Up @@ -547,42 +546,43 @@ props_defaults(Props, _Context) ->
props_filter_protected(Props) -> props_filter_protected(Props) ->
lists:filter(fun({K,_V}) -> not is_protected(K) end, Props). lists:filter(fun({K,_V}) -> not is_protected(K) end, Props).



%% @doc Map property names to an atom, fold pivot and computed fields together for later filtering.
map_property_name(P) when not is_list(P) -> map_property_name(z_convert:to_list(P));
map_property_name("computed_"++_) -> computed_xxx;
map_property_name("pivot_"++_) -> pivot_xxx;
map_property_name(P) when is_list(P) -> erlang:list_to_existing_atom(P).


%% @doc Properties that can't be updated with m_rsc_update:update/3 or m_rsc_update:insert/2 %% @doc Properties that can't be updated with m_rsc_update:update/3 or m_rsc_update:insert/2
is_protected(created) -> true; is_protected(id) -> true;
is_protected(creator_id) -> true; is_protected(created) -> true;
is_protected(id) -> true; is_protected(creator_id) -> true;
is_protected(modified) -> true; is_protected(modified) -> true;
is_protected(modifier_id) -> true; is_protected(modifier_id) -> true;
is_protected(pivot_category_nr) -> true; is_protected(props) -> true;
is_protected(pivot_city) -> true; is_protected(version) -> true;
is_protected(pivot_country) -> true; is_protected(page_url) -> true;
is_protected(pivot_date_end) -> true; is_protected(medium) -> true;
is_protected(pivot_date_end_month_day) -> true; is_protected(pivot_xxx) -> true;
is_protected(pivot_date_start) -> true; is_protected(computed_xxx) -> true;
is_protected(pivot_date_start_month_day) -> true; is_protected(_) -> false.
is_protected(pivot_first_name) -> true;
is_protected(pivot_gender) -> true;
is_protected(pivot_geocode) -> true;
is_protected(pivot_postcode) -> true;
is_protected(pivot_rtsv) -> true;
is_protected(pivot_state) -> true;
is_protected(pivot_street) -> true;
is_protected(pivot_surname) -> true;
is_protected(pivot_title) -> true;
is_protected(pivot_tsv) -> true;
is_protected(props) -> true;
is_protected(version) -> true;
is_protected(_) -> false.




is_trimmable(_, V) when not is_binary(V) and not is_list(V) -> false; is_trimmable(_, V) when not is_binary(V) and not is_list(V) -> false;
is_trimmable(title, _) -> true;
is_trimmable(title_short, _) -> true;
is_trimmable(summary, _) -> true;
is_trimmable(chapeau, _) -> true;
is_trimmable(subtitle, _) -> true;
is_trimmable(email, _) -> true; is_trimmable(email, _) -> true;
is_trimmable(uri, _) -> true;
is_trimmable(website, _) -> true;
is_trimmable(page_path, _) -> true; is_trimmable(page_path, _) -> true;
is_trimmable(name, _) -> true;
is_trimmable(slug, _) -> true; is_trimmable(slug, _) -> true;
is_trimmable(custom_slug, _) -> true; is_trimmable(custom_slug, _) -> true;
is_trimmable(category, _) -> true; is_trimmable(category, _) -> true;
is_trimmable(title, _) -> true;
is_trimmable(title_short, _) -> true;
is_trimmable(_, _) -> false. is_trimmable(_, _) -> false.




Expand Down

0 comments on commit 1b8f5e6

Please sign in to comment.