Exemple #1
0
def tableSpeciesObsJson(request):
    """ """
    # Check if pagination.
    pagination_page = request.GET.get('page', None)
    pagination_size = request.GET.get('per_page', 100)  # Default 100.
    #
    header_language = request.GET.get('header_language', 'darwin_core')
    data_header = sharkdata_core.SpeciesObsUtils().getHeaders()
    translated_headers = sharkdata_core.SpeciesObsUtils().translateHeaders(
        data_header, language=header_language)
    #
    db_filter_dict = {}
    url_param_list = []
    forms.parse_filter_params(request.GET, db_filter_dict, url_param_list)
    #
    # Only show ACTIVE rows, if not all status is requested.
    if request.GET.get('view_deleted', 'false') != 'true':
        db_filter_dict['status__iexact'] = 'ACTIVE'
    #
    data_rows = models.SpeciesObs.objects.values_list(*data_header).filter(
        **db_filter_dict)
    #
    if pagination_page:
        pag = paginator.Paginator(data_rows, pagination_size)
        try:
            data_rows = pag.page(pagination_page)
        except paginator.EmptyPage:
            # If page is out of range, return header only.
            data_rows = []
    #
    response = HttpResponse(content_type='application/json; charset=utf8')
    response[
        'Content-Disposition'] = 'attachment; filename=species_observations.json'
    response.write('{')
    if pagination_page and pag:
        response.write('"page": ' + str(pagination_page) + ', ')
        response.write('"pages": ' + str(pag.num_pages) + ', ')
        response.write('"per_page": ' + str(pagination_size) + ', ')
        response.write('"total": ' + str(pag.count) + ', ')
    response.write('"header": ["')
    response.write('", "'.join(translated_headers) + '"], ')  # Tab separated.
    response.write('"rows": [')
    row_delimiter = ''
    for row in data_rows:
        response.write(row_delimiter + '["' + '", "'.join(row) + '"]')
        row_delimiter = ', '
    response.write(']')
    response.write('}')

    return response
 def deleteSpeciesObsInThread(self, user):
     """ """
     logfile_name = sharkdata_core.SharkdataAdminUtils().log_create(
         command='Delete species observations', user=user)
     try:
         # Check if thread is running.
         if self._delete_obs_thread:
             if self._delete_obs_thread.is_alive():
                 error_message = '"Delete species observations" is already running. Please try again later.'
                 sharkdata_core.SharkdataAdminUtils().log_write(
                     logfile_name, log_row=error_message)
                 sharkdata_core.SharkdataAdminUtils().log_close(
                     logfile_name, new_status='FAILED')
                 #
                 return error_message
         # Use a thread to relese the user. Log file closed in thread.
         self._delete_obs_thread = threading.Thread(
             target=sharkdata_core.SpeciesObsUtils().deleteSpeciesObs,
             args=(
                 logfile_name,
                 user,
             ))
         self._delete_obs_thread.start()
     except Exception as e:
         error_message = 'Can\'t delete species observations.' + '\nException: ' + str(
             e) + '\n'
         sharkdata_core.SharkdataAdminUtils().log_write(
             logfile_name, log_row=error_message)
         sharkdata_core.SharkdataAdminUtils().log_close(logfile_name,
                                                        new_status='FAILED')
     #
     return None  # No error message.
Exemple #3
0
def tableSpeciesObsText(request):
    """ """
    # Check if pagination.
    pagination_page = request.GET.get('page', None)
    pagination_size = request.GET.get('per_page', 100)  # Default 100.
    #
    header_language = request.GET.get('header_language', 'darwin_core')
    data_header = sharkdata_core.SpeciesObsUtils().getHeaders()
    translated_headers = sharkdata_core.SpeciesObsUtils().translateHeaders(
        data_header, language=header_language)
    #
    db_filter_dict = {}
    url_param_list = []
    forms.parse_filter_params(request.GET, db_filter_dict, url_param_list)
    #
    # Only show ACTIVE rows, if not all status is requested.
    if request.GET.get('view_deleted', 'false') != 'true':
        db_filter_dict['status__iexact'] = 'ACTIVE'
    #
    data_rows = models.SpeciesObs.objects.values_list(*data_header).filter(
        **db_filter_dict)
    #
    if pagination_page:
        pag = paginator.Paginator(data_rows, pagination_size)
        try:
            data_rows = pag.page(pagination_page)
        except paginator.EmptyPage:
            # If page is out of range, return header only.
            data_rows = []
    #
    response = HttpResponse(content_type='text/plain; charset=utf8')
    response[
        'Content-Disposition'] = 'attachment; filename=species_observations.txt'
    response.write('\t'.join(translated_headers) + '\r\n')  # Tab separated.
    for row in data_rows:
        response.write('\t'.join(row) + '\r\n')  # Tab separated.
    return response
Exemple #4
0
def listSpeciesObs(request):
    """ """
    error_message = None  # initially.
    #
    header_language = request.GET.get('header_language', 'darwin_core')
    data_header = sharkdata_core.SpeciesObsUtils().getHeaders()
    translated_headers = sharkdata_core.SpeciesObsUtils().translateHeaders(
        data_header, language=header_language)
    #
    data_rows = None
    #
    if request.method == "GET":
        form = forms.SpeciesObsFilterForm()
        contextinstance = {
            'form': form,
            'data_header': None,
            'data_rows': None,
            'url_table': None,
            'error_message': error_message
        }
        contextinstance.update(csrf(request))
        return render(request, "list_speciesobs.html", contextinstance)
    elif request.method == "POST":
        if request.POST['confirm'] == "get_data":
            form = forms.SpeciesObsFilterForm(request.POST)
            #
            db_filter_dict = {}
            url_param_list = []
            forms.parse_filter_params(request.POST, db_filter_dict,
                                      url_param_list)
            #
            data_rows = []
            # Check parameters to avoid too long queries.
            class_param = ''
            order_param = ''
            species_param = ''
            scientific_name_param = ''
            if 'class' in request.POST:
                class_param = request.POST['class']
            if 'order' in request.POST:
                order_param = request.POST['order']
            if 'genus' in request.POST:
                genus_param = request.POST['genus']


#             if 'species' in request.POST:
#                 species_param = request.POST['species']
            if 'scientific_name' in request.POST:
                scientific_name_param = request.POST['scientific_name']
            # Check for empty or '-'.
            if ((class_param not in ['All', '-', ''])
                    or (order_param not in ['All', '-', ''])
                    or (genus_param not in ['All', '-', '']) or
                    #                 (species_param not in ['All', '-', '']) or
                (scientific_name_param not in ['All', '-', ''])):
                #
                # Only show ACTIVE rows as a part of the HTML page.
                db_filter_dict['status__iexact'] = 'ACTIVE'
                data_rows = models.SpeciesObs.objects.values_list(
                    *data_header).filter(**db_filter_dict)
                #
                if not data_rows:
                    error_message = 'No data found. Please try again...'

            else:
                error_message = 'At least one of Scientific name, Class, Order or Genus must be selected. Please select one and try again...'
            #
            contextinstance = {
                'form': form,
                'data_header': data_header,
                'data_rows': data_rows,
                'url_table': None,
                'error_message': error_message
            }
            contextinstance.update(csrf(request))
            return render(request, "list_speciesobs.html", contextinstance)
        #
        if request.POST['confirm'] == "view_url":
            #
            db_filter_dict = {}
            url_param_list = []
            forms.parse_filter_params(request.POST, db_filter_dict,
                                      url_param_list)
            url_params = ''
            if url_param_list:
                url_params += '?'
                url_params += '&'.join(url_param_list)
            #
            url_table = []
            if '?' in url_params:
                url_table.append('/speciesobs/table.txt/' + url_params +
                                 '&page=1&per_page=10')
                url_table.append('/speciesobs/table.json/' + url_params +
                                 '&page=1&per_page=10')
                url_table.append('/speciesobs/table.json/' + url_params +
                                 '&page=1&per_page=10&view_deleted=true')
            else:
                url_table.append('/speciesobs/table.txt/' + url_params +
                                 '?page=1&per_page=10')
                url_table.append('/speciesobs/table.json/' + url_params +
                                 '?page=1&per_page=10')
                url_table.append('/speciesobs/table.json/' + url_params +
                                 '?page=1&per_page=10&view_deleted=true')
            url_table.append('---')
            url_table.append('/speciesobs/table.txt/' + url_params)
            url_table.append('/speciesobs/table.json/' + url_params)
            #             url_table.append('/speciesobs/positions.kml/' + url_params)
            #             url_table.append('/speciesobs/year_info.kml/' + url_params)
            #             url_table.append('/speciesobs/map/' + url_params)

            #             url_table.append('http://maps.google.se/?q=http://sharkdata.se/speciesobs/positions.kml/' + url_params)
            #             url_table.append('http://maps.google.se/?q=http://sharkdata.se/speciesobs/year_info.kml/' + url_params)
            #
            #             url_table.append('---')
            #             url_table.append('For development (from http://test.sharkdata.se):')
            #             url_table.append('http://maps.google.se/?q=http://test.sharkdata.se/speciesobs/positions.kml/' + url_params)
            #             url_table.append('http://maps.google.se/?q=http://test.sharkdata.se/speciesobs/year_info.kml/' + url_params)
            #
            form = forms.SpeciesObsFilterForm(request.POST)
            contextinstance = {
                'form': form,
                'data_header': None,
                'data_rows': None,
                'url_table': url_table,
                'error_message': error_message
            }
            contextinstance.update(csrf(request))
            return render(request, "list_speciesobs.html", contextinstance)
    #
    return HttpResponseRedirect("/speciesobs")