Skip to content

view.py

Functions and variables used in pipeline/views.py.

generate_colsfields(fields, url_prefix_dict, not_orderable_col=None, not_searchable_col=None)

Generate data to be included in context for datatables.

Example of url_prefix_dict format: api_col_dict = { 'source.name': reverse('vast_pipeline:source_detail', args=[1])[:-2], 'source.run.name': reverse('vast_pipeline:run_detail', args=[1])[:-2] }

Parameters:

Name Type Description Default
fields list

List of fields to include as columns.

required
url_prefix_dict dict

Dict containing the url prefix to form href links in the datatables.

required
not_orderable_col list

List of columns that should be set to be not orderable in the final table.

None
not_searchable_col list

List of columns that should be set to be not searchable in the final table.

None

Returns:

Name Type Description
colsfields list

List containing JSON object.

Source code in vast_pipeline/utils/view.py
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
def generate_colsfields(
    fields: List[str], url_prefix_dict: Dict[str, str],
    not_orderable_col: Optional[List[str]]=None,
    not_searchable_col: Optional[List[str]]=None,
) -> List[Dict[str, Any]]:
    """
    Generate data to be included in context for datatables.

    Example of url_prefix_dict format:
    api_col_dict = {
        'source.name': reverse('vast_pipeline:source_detail', args=[1])[:-2],
        'source.run.name': reverse('vast_pipeline:run_detail', args=[1])[:-2]
    }

    Args:
        fields (list): List of fields to include as columns.
        url_prefix_dict (dict): Dict containing the url prefix to form
            href links in the datatables.
        not_orderable_col (list): List of columns that should be set to
            be not orderable in the final table.
        not_searchable_col (list): List of columns that should be set to
            be not searchable in the final table.

    Returns:
        colsfields (list): List containing JSON object.
    """
    colsfields = []

    if not_orderable_col is None:
        not_orderable_col = []
    if not_searchable_col is None:
        not_searchable_col = []
    for col in fields:
        field2append = {}
        if col == 'name':
            field2append = {
                'data': col, 'render': {
                    'url': {
                        'prefix': url_prefix_dict[col],
                        'col': 'name'
                    }
                }
            }
        elif '.name' in col:
            # this is for nested fields to build a render with column name
            # and id in url. The API results should look like:
            # {... , main_col : {'name': value, 'id': value, ... }}
            main_col = col.rsplit('.', 1)[0]
            field2append = {
                'data': col,
                'render': {
                    'url': {
                        'prefix': url_prefix_dict[col],
                        'col': main_col,
                        'nested': True,
                    }
                }
            }
        elif col == 'n_sibl':
            field2append = {
                'data': col, 'render': {
                    'contains_sibl': {
                        'col': col
                    }
                }
            }
        elif col in FLOAT_FIELDS:
            field2append = {
                'data': col,
                'render': {
                    'float': {
                        'col': col,
                        'precision': FLOAT_FIELDS[col]['precision'],
                        'scale': FLOAT_FIELDS[col]['scale'],
                    }
                }
            }
        else:
            field2append = {'data': col}

        if col in not_orderable_col:
            field2append['orderable'] = False

        if col in not_searchable_col:
            field2append['searchable'] = False

        colsfields.append(field2append)

    return colsfields

get_skyregions_collection(run_id=None)

Produce Sky region geometry shapes JSON object for d3-celestial.

Parameters:

Name Type Description Default
run_id int

Run ID to filter on if not None.

None

Returns:

Name Type Description
skyregions_collection dict

Dictionary representing a JSON obejct

Dict[str, Any]

containing the sky regions.

Source code in vast_pipeline/utils/view.py
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
def get_skyregions_collection(run_id: Optional[int]=None) -> Dict[str, Any]:
    """
    Produce Sky region geometry shapes JSON object for d3-celestial.

    Args:
        run_id (int, optional): Run ID to filter on if not None.

    Returns:
        skyregions_collection (dict): Dictionary representing a JSON obejct
        containing the sky regions.
    """
    skyregions = SkyRegion.objects.all()
    if run_id is not None:
        skyregions = skyregions.filter(run=run_id)

    features = []

    for skr in skyregions:
        ra_fix = 360. if skr.centre_ra > 180. else 0.
        ra = skr.centre_ra - ra_fix
        dec = skr.centre_dec
        width_ra = skr.width_ra / 2.
        width_dec = skr.width_dec / 2.
        id = skr.id
        features.append(
            {
                "type": "Feature",
                "id": f"SkyRegion{id}",
                "properties": {
                    "n": f"{id:02d}",
                    "loc": [ra, dec]
                },
                "geometry": {
                    "type": "MultiLineString",
                    "coordinates": [[
                        [ra+width_ra, dec+width_dec],
                        [ra+width_ra, dec-width_dec],
                        [ra-width_ra, dec-width_dec],
                        [ra-width_ra, dec+width_dec],
                        [ra+width_ra, dec+width_dec]
                    ]]
                }
            }
        )

    skyregions_collection = {
        "type": "FeatureCollection",
        "features" : features
    }

    return skyregions_collection

Last update: March 2, 2022
Created: March 2, 2022