14.3.199. crate_anon.crateweb.research.views
crate_anon/crateweb/research/views.py
Copyright (C) 2015, University of Cambridge, Department of Psychiatry. Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
This file is part of CRATE.
CRATE is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
CRATE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with CRATE. If not, see <https://www.gnu.org/licenses/>.
CRATE views on the research database.
- class crate_anon.crateweb.research.views.NlpSourceResult(fieldnames: List[str] | None = None, results: Sequence[Sequence[Any]] | None = None, sql: str | None = None, error: str | None = None)[source]
Serves as the return value for
get_source_results()
.- __init__(fieldnames: List[str] | None = None, results: Sequence[Sequence[Any]] | None = None, sql: str | None = None, error: str | None = None) None [source]
- Parameters:
fieldnames – fieldnames in source record
results – source record result
sql – SQL used in getting source text
error – error message
- crate_anon.crateweb.research.views.all_text_from_pid(request: HttpRequest) HttpResponse [source]
Picks a database, then redirects to
all_text_from_pid_with_db()
.- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.all_text_from_pid_with_db(request: HttpRequest, dbname: str) HttpResponse [source]
Clinician view to look up a patient’s RID from their PID and display text from any field.
- Parameters:
request – the
django.http.request.HttpRequest
dbname – name of the research database to use
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.archive_attachment(request: HttpRequest) HttpResponseBase [source]
Serve a binary file from the archive.
The patient_id is not required to find the file – but is used for the audit trail. (It would be possible for the end user to look up a file by name via a faked patient_id. However, this could be established from the audit log.)
- Parameters:
request – the Django
HttpRequest
object
- crate_anon.crateweb.research.views.archive_static(request: HttpRequest) HttpResponseBase [source]
Serve a static file from the archive.
- Parameters:
request – the Django
HttpRequest
object
- crate_anon.crateweb.research.views.archive_template(request: HttpRequest) HttpResponse [source]
Provides the views for the configurable “archive” system.
- Parameters:
request – the Django
HttpRequest
object- Returns:
a Django
HttpResponse
.
Note:
The archive template name is in
request.GET
; this allows the use of special (e.g. filename) characters.The patient ID is also in
request.GET
; this allows the optional use of more complex strings, e.g. JSON, to represent multiple ID numbers for use with several databases.Additional arguments are also in
request.GET
.To create a URL within a Django template, one would use e.g.
<a href="{% url 'archive' patient_id template_name %}">link text</a>
(the
'archive'
bit being configured incrate_anon.crateweb.config.urls
).But to create a URL within a Mako template, there are several ways… for example, in CamCOPS via Pyramid, we use
request.route_url(...)
. We can make this simple by passing patient-specific function to the context; see the Python Mako context.If we use DMP, it will add a
request.dmp
object; see https://doconix.github.io/django-mako-plus/topics_variables.html. (We won’t use DMP.)
- crate_anon.crateweb.research.views.common_find_text(request: HttpRequest, dbinfo: SingleResearchDatabase, form_class: Type[SQLHelperFindAnywhereForm], default_values: Dict[str, Any], permit_pid_search: bool, html_filename: str) HttpResponse [source]
Finds and displays text anywhere in the database(s), via a
UNION
query.- Parameters:
request – the
django.http.request.HttpRequest
dbinfo – a
crate_anon.crateweb.research.research_db_info.SingleResearchDatabase
detailing the research database to useform_class – form class to use to specify search options;
crate_anon.crateweb.research.forms.SQLHelperTextAnywhereForm
or a subclass of it (like crate_anon.crateweb.research.forms.ClinicianAllTextFromPidForm)default_values – default values to be passed to the form (see
form_class
)permit_pid_search – allow the user to search by PID/MPID (for clinicians)?
html_filename – Django HTML template filename to capture search options
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.datetime_iso_for_filename() str [source]
Returns a date/time as a string formatted for filenames.
- crate_anon.crateweb.research.views.drugmatch(drug_type: str, colname: str) str [source]
Returns SQL to check for the presence of any drug of type ‘drug_type’ anywhere in a field.
- Parameters:
drug_type – drug type to look for
colname – name of the column
- crate_anon.crateweb.research.views.edit_display(request: HttpRequest, query_id: str) HttpResponse [source]
Edit the ‘display’ attribute of the selected query by choosing a list of columns to show when the results are displayed.
- crate_anon.crateweb.research.views.generic_error(request: HttpRequest, error: str) HttpResponse [source]
Returns a generic error response.
- Parameters:
request – the
django.http.request.HttpRequest
error – the error text
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.get_all_pes(request: HttpRequest) QuerySet [source]
Return all Patient Explorers for the current user.
- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.db.models.QuerySet
forcrate_anon.crateweb.research.models.PatientExplorer
objects- Return type:
request
- crate_anon.crateweb.research.views.get_all_queries(request: HttpRequest) QuerySet [source]
Return all database queries for the current user.
- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.db.models.QuerySet
forcrate_anon.crateweb.research.models.Query
objects- Return type:
request
- crate_anon.crateweb.research.views.get_all_sitewide_queries() QuerySet [source]
Returns all site-wide queries.
- Returns:
a
django.db.models.QuerySet
forcrate_anon.crateweb.research.models.SitewideQuery
objects- Return type:
request
- crate_anon.crateweb.research.views.get_highlight_descriptions(highlight_dict: Dict[int, List[Highlight]]) List[str] [source]
Returns a list of length up to
N_CSS_HIGHLIGHT_CLASSES
of HTML elements illustrating the highlights.- Parameters:
highlight_dict – a dictionary mapping highlight colour to all the
crate_anon.crateweb.research.models.Highlight
objects that use it (e.g.:2
maps to highlight objects for all the separate pieces of text to be highlighted in colour 2)- Returns:
HTML describing the highlights
- Return type:
str
- crate_anon.crateweb.research.views.get_identical_pes(request: HttpRequest, pmq: PatientMultiQuery) List[PatientExplorer] [source]
Return all Patient Explorers for the current user whose query is identical to the query specified.
- Parameters:
request – the
django.http.request.HttpRequest
pmq – a
crate_anon.crateweb.research.models.PatientMultiQuery
- Returns:
a list of
crate_anon.crateweb.research.models.PatientExplorer
objects
- crate_anon.crateweb.research.views.get_identical_queries(request: HttpRequest, sql: str, sitewide: bool = False) List[Query] [source]
Returns all queries that are identical to the SQL provided.
This saves us creating a new query when one exists already that’s identical.
We check by hash.
- Parameters:
request – the
django.http.request.HttpRequest
sql – SQL text
sitewide – check sitewide, rather than user-specific, queries?
- Returns:
- Return type:
list
- crate_anon.crateweb.research.views.highlight_activate(request: HttpRequest, highlight_id: str) HttpResponse [source]
Activate a highlight.
- Parameters:
request – the
django.http.request.HttpRequest
highlight_id – string form of the integer PK for
crate_anon.crateweb.research.models.Highlight
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.highlight_deactivate(request: HttpRequest, highlight_id: str) HttpResponse [source]
Deactivate a highlight.
- Parameters:
request – the
django.http.request.HttpRequest
highlight_id – string form of the integer PK for
crate_anon.crateweb.research.models.Highlight
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.highlight_delete(request: HttpRequest, highlight_id: str) HttpResponse [source]
Delete a highlight.
- Parameters:
request – the
django.http.request.HttpRequest
highlight_id – string form of the integer PK for
crate_anon.crateweb.research.models.Highlight
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.highlight_edit_select(request: HttpRequest) HttpResponse [source]
Edit or activate highlighting (which will apply to any queries that the user runs).
- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.launch_archive(request: HttpRequest) HttpResponse [source]
Takes the submitted
patient_id
(fromrequest.POST
) and launches the archive’s root page for that patient.- Parameters:
request – the Django
HttpRequest
object
- crate_anon.crateweb.research.views.local_structure_help(request: HttpRequest) HttpResponse [source]
Serves a locally specifed help page.
- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.no_query_selected(request: HttpRequest) HttpResponse [source]
View to say “no query selected” when one should have been.
- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.parse_privileged_sql(request: HttpRequest, sql: str) List[Any] [source]
Parses clinicians’ queries to find rid from pid.
SQL should be, e.g.: ‘SELECT * FROM tablename WHERE ~pid:dbname = pidnumber’ or ‘SELECT * FROM tablename WHERE ~mpid:dbname IN (value1, value2, …)’ where dbname has the secret lookup table the user wants to use.
- Parameters:
request – the
django.http.request.HttpRequest
sql – SQL text
- Returns:
[bool, str] where the bool is success (0) or failure (1). If success, the str is the new sql and if failure it’s the error message.
- crate_anon.crateweb.research.views.pe_activate(request: HttpRequest, pe_id: str) HttpResponse [source]
Activate one of the user’s Patient Explorers.
- Parameters:
request – the
django.http.request.HttpRequest
pe_id – string form of the integer PK of a
crate_anon.crateweb.research.models.PatientExplorer
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.pe_build(request: HttpRequest) HttpResponse [source]
View to build/edit a Patient Explorer (see
crate_anon.crateweb.research.models.PatientExplorer
).- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.pe_choose(request: HttpRequest) HttpResponse [source]
Choose one of the user’s Patient Explorers (see
crate_anon.crateweb.research.models.PatientExplorer
).- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.pe_data_finder_excel(request: HttpRequest, pe_id: str) HttpResponse [source]
Serves the data finder view of a
crate_anon.crateweb.research.models.PatientExplorer
(seepe_data_finder_results()
) as an Excel XLSX file.- Parameters:
request – the
django.http.request.HttpRequest
pe_id – string form of the integer PK of a
crate_anon.crateweb.research.models.PatientExplorer
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.pe_data_finder_results(request: HttpRequest, pe_id: str) HttpResponse [source]
Shows the data finder view of a
crate_anon.crateweb.research.models.PatientExplorer
. This counts records for each table (by patient), without showing all the data.- Parameters:
request – the
django.http.request.HttpRequest
pe_id – string form of the integer PK of a
crate_anon.crateweb.research.models.PatientExplorer
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.pe_delete(request: HttpRequest, pe_id: str) HttpResponse [source]
Delete one of the user’s Patient Explorers.
- Parameters:
request – the
django.http.request.HttpRequest
pe_id – string form of the integer PK of a
crate_anon.crateweb.research.models.PatientExplorer
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.pe_edit(request: HttpRequest, pe_id: str) HttpResponse [source]
Edit one of the user’s Patient Explorers.
- Parameters:
request – the
django.http.request.HttpRequest
pe_id – string form of the integer PK of a
crate_anon.crateweb.research.models.PatientExplorer
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.pe_excel(request: HttpRequest, pe_id: str) HttpResponse [source]
Return the results of a
crate_anon.crateweb.research.models.PatientExplorer
as an Excel XLSX file.- Parameters:
request – the
django.http.request.HttpRequest
pe_id – string form of the integer PK of a
crate_anon.crateweb.research.models.PatientExplorer
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.pe_monster_results(request: HttpRequest, pe_id: str) HttpResponse [source]
Shows the monster data view of a
crate_anon.crateweb.research.models.PatientExplorer
. This performs aSELECT(*)
for all rows retrieved by the PatientExplorer.- Parameters:
request – the
django.http.request.HttpRequest
pe_id – string form of the integer PK of a
crate_anon.crateweb.research.models.PatientExplorer
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.pe_one_table(request: HttpRequest, pe_id: str, schema: str, table: str, db: str = '') HttpResponse [source]
Shows the single table view of a
crate_anon.crateweb.research.models.PatientExplorer
. This shows results from a single table.- Parameters:
request – the
django.http.request.HttpRequest
pe_id – string form of the integer PK of a
crate_anon.crateweb.research.models.PatientExplorer
schema – name of the table’s schema
table – name of the table
db – name of the table’s database (above the schema level), if appliable
- Returns:
a
django.http.response.HttpResponse
Todo
pe_one_table
Might it be better to feed the resulting query back into the main Query system, allowing users to turn columns on/off, etc.?
At present it forces
query_id
toNone
and this is detected byquery_result.html
.
- crate_anon.crateweb.research.views.pe_results(request: HttpRequest, pe_id: str) HttpResponse [source]
Show the results of a Patient Explorer, in paginated tabular form.
- Parameters:
request – the
django.http.request.HttpRequest
pe_id – string form of the integer PK of a
crate_anon.crateweb.research.models.PatientExplorer
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.pe_submit(request: HttpRequest, pmq: PatientMultiQuery, run: bool = False) HttpResponse [source]
Save a
crate_anon.crateweb.research.models.PatientMultiQuery
as acrate_anon.crateweb.research.models.PatientExplorer
for the current user.- Parameters:
request – the
django.http.request.HttpRequest
pmq – a
crate_anon.crateweb.research.models.PatientMultiQuery
run – run and show results? Otherwise, save and return to the choice view
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.pe_table_browser(request: HttpRequest, pe_id: str) HttpResponse [source]
Shows the table browser view of a
crate_anon.crateweb.research.models.PatientExplorer
. This shows a list of all tables in the database, with hyperlinks to a single-table Patient Explorer view for each.- Parameters:
request – the
django.http.request.HttpRequest
pe_id – string form of the integer PK of a
crate_anon.crateweb.research.models.PatientExplorer
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.pe_tsv_zip(request: HttpRequest, pe_id: str) HttpResponse [source]
Return the results of a
crate_anon.crateweb.research.models.PatientExplorer
as a ZIP file of TSV files.- Parameters:
request – the
django.http.request.HttpRequest
pe_id – string form of the integer PK of a
crate_anon.crateweb.research.models.PatientExplorer
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.pid_rid_lookup(request: HttpRequest, with_db_url_name: str, html_filename: str) HttpResponse [source]
Common functionality for :func`pidlookup`,
ridlookup()
.Provides a view/form allowing the user to choose a database, if more than one is possible, and then redirect to another view once we have that database choice.
- Parameters:
request – the
django.http.request.HttpRequest
with_db_url_name – URL name to redirect to, passed as a parameter to
django.urls.reverse()
html_filename – Django HTML template filename
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.pid_rid_lookup_with_db(request: HttpRequest, dbname: str, form_html_filename: str, formclass: Any, result_html_filename: str) HttpResponse [source]
Common functionality for
pidlookup_with_db()
,ridlookup_with_db()
.- Parameters:
request – the
django.http.request.HttpRequest
dbname – name of the research database to use
form_html_filename – Django HTML template filename to ask for PID/RID/etc. details
formclass – form class to use for requesting PID/RID/etc.
result_html_filename – Django HTML template filename to display results
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.pidlookup(request: HttpRequest) HttpResponse [source]
Look up PID information from RID information.
- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.pidlookup_with_db(request: HttpRequest, dbname: str) HttpResponse [source]
Look up PID information from RID information, for a specific database.
- Parameters:
request – the
django.http.request.HttpRequest
dbname – name of the research database to use
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.query_activate(request: HttpRequest, query_id: str) HttpResponse [source]
Activate the specified query for the current user.
- Parameters:
request – the
django.http.request.HttpRequest
query_id – string form of the integer PK of the
crate_anon.crateweb.research.models.Query
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.query_add_sitewide(request: HttpRequest) HttpResponse [source]
Superuser view to add or edit sitewide queries and their descriptions.
- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.query_build(request: HttpRequest) HttpResponse [source]
Assisted query builder, based on the data structure read from the research database.
- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.query_context(request: HttpRequest) Dict[str, Any] [source]
Query context dictionary used for (nearly?) every request.
- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a dictionary with core information about the request, like the currently selected query/Patient Explorer ID for the user.
- Return type:
dict
Notes:
Try to minimize SQL here, as these calls will be used for EVERY request.
This problem can be circumvented with a per-request cache; see https://stackoverflow.com/questions/3151469/per-request-cache-in-django
- crate_anon.crateweb.research.views.query_count(request: HttpRequest, query_id: str) HttpResponse [source]
View
COUNT(*)
from the specific query.- Parameters:
request – the
django.http.request.HttpRequest
query_id – string form of the integer PK of the
crate_anon.crateweb.research.models.Query
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.query_count_current(request: HttpRequest) HttpResponse [source]
View
COUNT(*)
from the user’s current query.- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.query_delete(request: HttpRequest, query_id: str) HttpResponse [source]
Delete (or hide if required for audit purposes) the specified query for the current user.
- Parameters:
request – the
django.http.request.HttpRequest
query_id – string form of the integer PK of the
crate_anon.crateweb.research.models.Query
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.query_edit_select(request: HttpRequest) HttpResponse [source]
View to edit SQL for the current
SELECT
query (and/or run it).- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.query_excel(request: HttpRequest, query_id: str) HttpResponse [source]
Serves an XLSX (Excel) file with the results of the specified query.
- Parameters:
request – the
django.http.request.HttpRequest
query_id – string form of the integer PK of the
crate_anon.crateweb.research.models.Query
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.query_results(request: HttpRequest, query_id: str) HttpResponse [source]
View the results of chosen query, in conventional tabular format.
- Parameters:
request – the
django.http.request.HttpRequest
query_id – string form of the integer PK of the
crate_anon.crateweb.research.models.Query
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.query_results_recordwise(request: HttpRequest, query_id: str) HttpResponse [source]
View results of chosen query, in recordwise tabular format.
- Parameters:
request – the
django.http.request.HttpRequest
query_id – string form of the integer PK of the
crate_anon.crateweb.research.models.Query
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.query_submit(request: HttpRequest, sql: str, run: bool = False, filter_display: bool = False) HttpResponse [source]
Ancillary function to add a query, and redirect to the editing or run page.
- Parameters:
request – the
django.http.request.HttpRequest
sql – SQL text
run – execute the query and show the results? Otherwise, save the query and return to the editing page
filter_display – after saving the query, redirect to the filter page?
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.query_tsv(request: HttpRequest, query_id: str) HttpResponse [source]
Download TSV of the specified query.
- Parameters:
request – the
django.http.request.HttpRequest
query_id – string form of the integer PK of the
crate_anon.crateweb.research.models.Query
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.render_bad_pe(request: HttpRequest, pe: PatientExplorer, exception: Exception) HttpResponse [source]
A view saying “your Patient Explorer failed”.
- Parameters:
request – the
django.http.request.HttpRequest
pe – the
crate_anon.crateweb.research.models.PatientExplorer
that went wrongexception – the Python exception that resulted, which may have had extra information attached via
cardinal_pythonlib.exceptions.add_info_to_exception()
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.render_bad_query(request: HttpRequest, query: Query, exception: Exception) HttpResponse [source]
A view saying “your query failed”. This is the normal thing to see if the user has entered bad SQL.
- Parameters:
request – the
django.http.request.HttpRequest
query – the
crate_anon.crateweb.research.models.Query
that went wrongexception – the Python exception that resulted, which may have had extra information attached via
cardinal_pythonlib.exceptions.add_info_to_exception()
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.render_bad_query_id(request: HttpRequest, query_id: str) HttpResponse [source]
A view saying “bad query ID”.
- Parameters:
request – the
django.http.request.HttpRequest
query_id – the query ID that was bad
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.render_lookup(request: HttpRequest, dbinfo: SingleResearchDatabase, result_html_filename: str, trids: List[int] | None = None, rids: List[str] | None = None, mrids: List[str] | None = None, pids: List[int] | None = None, mpids: List[int] | None = None) HttpResponse [source]
Shows the output of a PID/RID lookup.
- Parameters:
request – the
django.http.request.HttpRequest
dbinfo – a
crate_anon.crateweb.research.research_db_info.SingleResearchDatabase
detailing the research database to useresult_html_filename – Django HTML template filename to display results
trids – list of TRIDs to look up from
rids – list of RIDs to look up from
mrids – list of MRIDs to look up from
pids – list of PIDs to look up from
mpids – list of MPIDs to look up from
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.render_missing_pe(request: HttpRequest) HttpResponse [source]
Tell the user that there’s no Patient Explorer selected, when there should have been.
- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.render_missing_query(request: HttpRequest) HttpResponse [source]
A view saying “missing query”.
- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.render_resultcount(request: HttpRequest, query: Query) HttpResponse [source]
Displays the number of rows that a given query will fetch.
- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.render_resultset(request: HttpRequest, query: Query, highlights: Iterable[Highlight], collapse_at_len: int | None = None, collapse_at_n_lines: int | None = None, line_length: int | None = None, ditto: bool = True, ditto_html: str = '″') HttpResponse [source]
Show the results of a user’s query in paginated, tabular format.
- Parameters:
request – the
django.http.request.HttpRequest
query – a
crate_anon.crateweb.research.models.Query
to executehighlights – an iterable of
crate_anon.crateweb.research.models.Highlight
objects to apply colourful highlighting to the resultscollapse_at_len – if specified, the string length beyond which the cell will be collapsed
collapse_at_n_lines – if specified, the number of lines beyond which the cell will be collapsed
line_length – if specified, the line length to word-wrap at
ditto – whether to replace cells that are identical to the cell immediately above with ditto marks
ditto_html – the HTML string to use as a ditto mark
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.render_resultset_recordwise(request: HttpRequest, query: Query, highlights: Iterable[Highlight], collapse_at_len: int | None = None, collapse_at_n_lines: int | None = None, line_length: int | None = None) HttpResponse [source]
Show the results of a user’s query in recordwise format.
- Parameters:
request – the
django.http.request.HttpRequest
query – a
crate_anon.crateweb.research.models.Query
to executehighlights – an iterable of
crate_anon.crateweb.research.models.Highlight
objects to apply colourful highlighting to the resultscollapse_at_len – if specified, the string length beyond which the cell will be collapsed
collapse_at_n_lines – if specified, the number of lines beyond which the cell will be collapsed
line_length – if specified, the line length to word-wrap at
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.resultset_html_table(fieldnames: List[str], rows: List[List[Any]], element_counter: HtmlElementCounter, start_index: int = 0, highlight_dict: Dict[int, List[Highlight]] | None = None, collapse_at_len: int | None = None, collapse_at_n_lines: int | None = None, line_length: int | None = None, ditto: bool = True, ditto_html: str = '″', no_ditto_cols: List[int] | None = None, null: str = '<i>NULL</i>') str [source]
Returns an HTML table representing a set of results from a query. Its columns are the database columns; its rows are the database rows.
- Parameters:
fieldnames – list of column names
rows – list of rows (each row being a list of values in the same order as
fieldnames
)element_counter – a
crate_anon.crateweb.research.html_functions.HtmlElementCounter
, which will be modifiedstart_index – the zero-based index of the first row in this table (used for pagination, when the second and subsequent tables don’t start with the first row of the result set)
highlight_dict – an optional dictionary mapping highlight colour to all the
crate_anon.crateweb.research.models.Highlight
objects that use it (e.g.:2
maps to highlight objects for all the separate pieces of text to be highlighted in colour 2)collapse_at_len – if specified, the string length beyond which the cell will be collapsed
collapse_at_n_lines – if specified, the number of lines beyond which the cell will be collapsed
line_length – if specified, the line length to word-wrap at
ditto – whether to replace cells that are identical to the cell immediately above with ditto marks
ditto_html – the HTML string to use as a ditto mark
no_ditto_cols – column indexes (zero-based) for which ditto marks should never be used
null – the HTML string to use for database
NULL
(PythonNone
) values
- Returns:
HTML
- Return type:
str
- crate_anon.crateweb.research.views.ridlookup(request: HttpRequest) HttpResponse [source]
Look up RID information from PID information.
- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.ridlookup_with_db(request: HttpRequest, dbname: str) HttpResponse [source]
Look up RID information from PID information, for a specific database.
- Parameters:
request – the
django.http.request.HttpRequest
dbname – name of the research database to use
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.save_display(request: HttpRequest, query_id: str) HttpResponse [source]
Save changes to the ‘display’ attribute of the selected query, and to the value of ‘no_null’.
- crate_anon.crateweb.research.views.show_sitewide_queries(request: HttpRequest) HttpResponse [source]
View to show all site-wide queries.
- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.single_record_html_table(fieldnames: List[str], record: List[Any], element_counter: HtmlElementCounter, highlight_dict: Dict[int, List[Highlight]] | None = None, collapse_at_len: int | None = None, collapse_at_n_lines: int | None = None, line_length: int | None = None) str [source]
Returns an HTML table representing a set of results from a query, in recordwise format. It has two columns, effectively “database column” and “value”; its rows are the database columns; it displays a single database result row.
- Parameters:
fieldnames – list of column names
record – a single result row, i.e. a list of values in the same order as
fieldnames
element_counter – a
crate_anon.crateweb.research.html_functions.HtmlElementCounter
, which will be modifiedhighlight_dict – an optional dictionary mapping highlight colour to all the
crate_anon.crateweb.research.models.Highlight
objects that use it (e.g.:2
maps to highlight objects for all the separate pieces of text to be highlighted in colour 2)collapse_at_len – if specified, the string length beyond which the cell will be collapsed
collapse_at_n_lines – if specified, the number of lines beyond which the cell will be collapsed
line_length – if specified, the line length to word-wrap at
- Returns:
HTML
- Return type:
str
- crate_anon.crateweb.research.views.sitewide_query_delete(request: HttpRequest, query_id: str) HttpResponse [source]
Delete a site-wide query.
- Parameters:
request – the
django.http.request.HttpRequest
query_id – string form of the integer PK of the
crate_anon.crateweb.research.models.SitewideQuery
- Returns:
a
django.http.response.HttpResponse
Note:
When sitewide queries are used, their SQL is added to the user’s personal libraries. All auditing therefore relates to users’ personal query libraries. Sitewide queries cannot be executed “standalone”.
As a result, we use a raw
crate_anon.crateweb.research.models.SitewideQuery.delete`()
, rather than the system used bycrate_anon.crateweb.research.models.Query.delete_if_permitted()
.
- crate_anon.crateweb.research.views.sitewide_query_process(request: HttpRequest, query_id: str) HttpResponse [source]
Takes a sitewide query ID and receives (through
POST
) replacements for the placeholders. Then adds the code to user’s personal library or adds and runs it.- Parameters:
request – the
django.http.request.HttpRequest
query_id – string form of the integer PK of the
crate_anon.crateweb.research.models.SitewideQuery
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.source_info(request: HttpRequest, srcdb: str, srctable: str, srcfield: str, srcpkfield: str, srcpkval: str | None, srcpkstr: str | None) HttpResponse [source]
Show source information for a record in a CRATE NLP table.
- Parameters:
request – the
django.http.request.HttpRequest
srcdb – source database as given by the nlp record
srctable – source table
srcfield – source field
srcpkfield – the fieldname in the source table which contains the primary key
srcpkval – primary key value in source table - may be ‘None’
srcpkstr – primary key string in source table - may be ‘None’
- Returns:
a
django.http.response.HttpResponse
Only one of srcpkval and srcpkstr should be ‘None’, and this will be given as a string because it’s through a URL link.
- crate_anon.crateweb.research.views.sqlhelper_drug_type(request: HttpRequest) HttpResponse [source]
Picks a database, then redirects to
sqlhelper_drug_type_with_db()
.- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.sqlhelper_drug_type_with_db(request: HttpRequest, dbname: str) HttpResponse [source]
Finds drugs of a given type anywhere in the database(s) via a
UNION
query.- Parameters:
request – the
django.http.request.HttpRequest
dbname – name of the research database to use
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.sqlhelper_text_anywhere(request: HttpRequest) HttpResponse [source]
Picks a database, then redirects to
sqlhelper_text_anywhere_with_db()
.- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.sqlhelper_text_anywhere_with_db(request: HttpRequest, dbname: str) HttpResponse [source]
Finds text anywhere in the database(s) via a
UNION
query.- Parameters:
request – the
django.http.request.HttpRequest
dbname – name of the research database to use
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.structure_excel(request: HttpRequest) HttpResponse [source]
Serves the table structure of the research database(s) as an Excel XLSX file.
- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.structure_table_long(request: HttpRequest) HttpResponse [source]
Shows the table structure of the research database(s) in long format.
- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.structure_table_paginated(request: HttpRequest) HttpResponse [source]
Shows the table structure of the research database(s) in paginated format.
- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.structure_tree(request: HttpRequest) HttpResponse [source]
Shows an expand-and-collapse tree view of the table structure of the research database(s).
- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.structure_tsv(request: HttpRequest) HttpResponse [source]
Serves the table structure of the research database(s) as TSV.
- Parameters:
request – the
django.http.request.HttpRequest
- Returns:
a
django.http.response.HttpResponse
- crate_anon.crateweb.research.views.textfinder_sql(patient_id_fieldname: str, min_length: int, use_fulltext_index: bool, include_content: bool, include_datetime: bool, fragment: str = '', drug_type: str = '', patient_id_value: int | str | None = None, extra_fieldname: str | None = None, extra_value: int | str | None = None) str [source]
Returns SQL to find the text in
fragment
across all tables that contain the field indicated bypatient_id_fieldname
, where the length of the text field is at leastmin_length
.- Parameters:
patient_id_fieldname – field (column) name across all tables that contains the patient ID; any tables that don’t contain this column will be ignored
fragment – fragment of text to find (e.g. “paracetamol”)
drug_type – type of drug to find any example of
min_length – text fields must be at least this large to bother searching; use this option to exclude e.g.
VARCHAR(1)
columns from the searchuse_fulltext_index – use database full-text indexing?
include_content – include the text fields in the output?
include_datetime – include the date/time of each record, if known (see
crate_anon.crateweb.research.research_db_info.ResearchDatabaseInfo.get_default_date_column()
patient_id_value – specify this to restrict to a single patient; the value of the patient ID column (see
patient_id_fieldname
) to restrict toextra_fieldname –
extra_value –
- Returns:
SQL query
- Return type:
str
- Raises:
ValueError –
- crate_anon.crateweb.research.views.textmatch(column_name: str, fragment: str, as_fulltext: bool, dialect: str = 'mysql') str [source]
Returns SQL to check for the presence of text anywhere in a field.
- Parameters:
column_name – name of the column
fragment – piece of text to look for
as_fulltext – use a FULLTEXT search if the database dialect supports it
dialect – dialect name (
mysql
,mssql
are known)
- Returns:
SQL fragment like:
column LIKE '%fragment%'
(ANSI SQL)MATCH(column) AGAINST ('fragment')
(MySQL full-text)CONTAINS(column, 'fragment')
(Microsoft SQL Server full-text)
- Return type:
str