class GeoConverterFilesForm(forms.Form):
    fileupload = forms.FileField(label='File')
    files_ogrformat = forms.ChoiceField(
        widget=forms.Select,
        label='Export format')

    def __init__(self, post=None, files=None):
        forms.Form.__init__(self, post, files)
        self.fields['files_ogrformat'].choices = [
            ('', '--Choose--')] + [
            (c.name, c.name) for c in OgrFormat.objects.all().order_by('name')
            if c.is_active()]
        self.files = files
        self.post = post class GeoConverterWebservicesForm(forms.Form):
    urlinput = forms.CharField()
    webservices_ogrformat = forms.ChoiceField(
        widget=forms.Select,
        label='Export format')

    def __init__(self, post=None, url=None):
        forms.Form.__init__(self, post, url)
        self.fields['webservices_ogrformat'].choices = [
            ('', '--Choose--')] + [
            (c.name, c.name) for c in OgrFormat.objects.all().order_by('name')
            if c.is_active()]
        self.urlinput = url
        self.post = post def get_job_id_regex():
def get_bounded_job_id_regex():
def get_new_job_identifier_by_client_job_token(session_key, client_job_token):
def get_job_identifier_by_client_job_token(session_key, client_job_token):
def get_job_identifier_by_job_id(session_key, job_id):
def _check_client_job_token(client_job_token):
def _get_random_job_id():
def __get_date_string():
def __get_random_code():
def is_valid_job_id(code):
def split_job_id(job_id):
def join_job_id(*args): def initialize_conversion_job(job_identifier):
def store_file(job_identifier, file_id, file_data):
def remove_file(job_identifier, job_id, file_id):
def process_file(job_identifier, file_id):
def process_archive(
        job_identifier,
        archive_path,
        unpack_base_path,
        unpack_path,
        output_path,
        export_format_name,
        source_srs,
        target_srs,
        simplify_parameter,
        additional_arguments,
        archive_depth=1):
def process_folder(
        job_identifier,
        source_path,
        file_dict,
        destination_path,
        export_format_name,
        source_srs,
        target_srs,
        simplify_parameter,
        additional_arguments,
        archive_depth=1):
def process_webservice_urls(job_identifier):
def create_download_file(job_identifier):
def cleanup():
def cleanup_thread(): class OgrFormat(models.Model):
    name = models.CharField(
        max_length=100, unique=True,
        help_text='Name des Formats, wie er auf der Webseite angezeigt werden soll.')
    ogr_name = models.CharField(
        max_length=100,
        verbose_name='OGR name',
        help_text='Der von OGR verwendete Name.',
        choices=get_ogr_formats_tuple())
    file_extension = models.CharField(
        max_length=100,
        help_text='Die Dateiendung des Formats. Beispiel: gpx (ohne Punkt am Anfang!)')
    output_type = models.CharField(
        max_length=100,
        help_text='Ausgabe dieses OGR-Formats in eine Datei (meistens) oder Ordner (z.B. bei ESRI Shapefile).',
        choices=(('file', 'File'),
                 ('folder', 'Folder')),
        default='file')
    state_all_files = models.BooleanField(
        verbose_name='List all files, separated by commas.',
        help_text='Hat einen Einfluss auf den im Terminal ausgeführten Befehl. Ist die Option aktiviert, sieht der Befehl z.B. so aus: ogr2ogr ... "input.itf,model.ili".')
    is_export_format = models.BooleanField(
        verbose_name='Show as export format.',
        help_text='Bestimmt, ob dieses Format auf der Webseite für Besucher als Exportformat sichtbar ist.')

    def __unicode__(self):
        return self.name

    def is_active(self):
        return self.is_export_format and self.is_writeable()
    is_active.boolean = True

    def is_readable(self):
        supported_formats = ogrinfo.get_supported_formats()

        for ogr_format in supported_formats:
            if ogr_format.name == self.ogr_name and ogr_format.read:
                return True

        return False
    is_readable.boolean = True

    def is_writeable(self):
        supported_formats = ogrinfo.get_supported_formats()

        for ogr_format in supported_formats:
            if ogr_format.name == self.ogr_name and ogr_format.write:
                return True

        return False
    is_writeable.boolean = True

    class Meta:
        verbose_name = 'OGR format'
        verbose_name_plural = 'OGR formats'

    @staticmethod
    def get_formats_information_dictionary():
        formats_information = {}

        for ogr_format in OgrFormat.objects.all():
            name = ogr_format.name
            ogr_name = ogr_format.ogr_name
            extension = ogr_format.file_extension
            is_folder = ogr_format.output_type == 'folder'
            state_all_files = ogr_format.state_all_files
            formats_information[name] = OgrFormatInformation(
                name,
                ogr_name,
                extension,
                is_folder,
                state_all_files,
                [],
                [])

        for additional_ogr_format in AdditionalOgrFormat.objects.all():
            file_extension = additional_ogr_format.file_extension
            is_required = additional_ogr_format.required
            is_multiple = additional_ogr_format.multiple
            formats_information[
                additional_ogr_format.ogr_format.name].additional_files.append(
                AdditionalOgrFormatInformation(
                    file_extension,
                    is_required,
                    is_multiple))

        for additional_shell_parameter in OgrFormatShellParameter.objects.all():
            prefix = additional_shell_parameter.prefix
            parameter_name = additional_shell_parameter.parameter_name
            parameter_value = additional_shell_parameter.parameter_value
            value_quotation_marks = additional_shell_parameter.value_quotation_marks
            use_for_reading = additional_shell_parameter.use_for_reading
            use_for_writing = additional_shell_parameter.use_for_writing
            formats_information[
                additional_shell_parameter.ogr_format.name].additional_parameters.append(
                AdditionalShellParameterInformation(
                    prefix,
                    parameter_name,
                    parameter_value,
                    value_quotation_marks,
                    use_for_reading,
                    use_for_writing))

        return formats_information

    @staticmethod
    def get_format_information_by_name(ogr_format):
        format_dictionary = OgrFormat.get_formats_information_dictionary()
        if ogr_format in format_dictionary:
            return format_dictionary[ogr_format]
        else:
            return None

    @staticmethod
    def get_ogr_name_by_file_extension(file_extension):
        for ogr_format in OgrFormat.objects.all():
            if ogr_format.file_extension.lower() == file_extension.lower():
                return ogr_format.name
        return ''

    @staticmethod
    def contains_extension(file_extension):
        return OgrFormat.get_ogr_name_by_file_extension(file_extension) != '' class GlobalOgrShellParameter(ShellParameter):
    is_active = models.BooleanField(default=True)

    def parameter_string(self):
        prefix = ('' if self.parameter_name == '' else self.prefix)
        value = self.__get_value_string(
            self.parameter_value,
            self.value_quotation_marks,
            self.parameter_name == '')
        parameter = prefix + self.parameter_name + value
        return parameter

    def __get_value_string(self, value, quotation_marks, value_only):
        if value == '':
            newvalue = ''
        else:
            if quotation_marks:
                newvalue = '"' + value + '"'
            else:
                newvalue = str(value)
            if not value_only:
                newvalue = ' ' + newvalue
        return newvalue

    class Meta(ShellParameter.Meta):
        verbose_name = 'Global shell parameter'
        verbose_name_plural = 'Global shell parameters'
        unique_together = (('parameter_name', 'parameter_value'),)

    @staticmethod
    def get_all_parameters():
        return GlobalOgrShellParameter.objects.all()

    @staticmethod
    def get_active_parameters():
        return GlobalOgrShellParameter.objects.filter(is_active=True) class FileMatcher:

    '''
    Saves a list of files by an ID and returns files being matched
    '''

    def __init__(self, file_dict):
        self.__file_dict = file_dict
        self.__file_matches = _get_matches_from_file_list(self.__file_dict)

    def get_file_name(self, file_id):
        file_match = self.get_match(file_id)
        if file_match is not None:
            return file_match.get_file_dict()[file_id]
        else:
            return self.__file_dict[file_id]

    def get_original_file_name(self, file_id):
        return self.__file_dict[file_id]

    def get_match(self, file_id):
        for file_match in self.__file_matches:
            if file_id in file_match.get_file_dict():
                return file_match
        return None

    def get_matches(self):
        return self.__file_matches def get_download_items(session_key):
    def __init__(self, job_identifier):
    def __get_download_item_query_set(self):
        def get_job_identifier(self):
    def add_download_item(self):
    def set_download_caption(self, download_caption):
    def get_download_caption(self):
    def set_is_downloaded(self):
    def get_is_downloaded(self):
    def remove_download_item(self):
    def get_download_file_information(self):
    def get_download_file_size(self):
    def download_file_exists(self):
class DownloadHandler: class JobHandler:

    '''
    Class that manages conversion jobs.
    '''

    def __init__(self, job_identifier):
        if job_identifier is not None:
            self.__job_identifier = job_identifier
        else:
            raise Exception("job_identifier may not be None")

    def __get_conversion_job_query_set(self):
        conversion_jobs = ConversionJob.get_conversion_job(
            self.__job_identifier)

        if len(conversion_jobs) == 0:
            conversion_job = ConversionJob()
            conversion_job.job_identifier = self.__job_identifier
            conversion_job.source_srs = 0
            conversion_job.target_srs = 0
            conversion_job.simplify_parameter = 0
            conversion_job.save()
            conversion_jobs = ConversionJob.get_conversion_job(
                self.__job_identifier)

        if len(conversion_jobs) == 1:
            return conversion_jobs
        else:
            raise Exception("Number of found conversion_jobs must be 1")

    def __get_conversion_job_folder_query_set(self, folder_path):
        conversion_job = self.__get_conversion_job_query_set()[0]
        conversion_job_folders = ConversionJobFolder.get_conversion_job_folder_by_path(
            conversion_job, folder_path)

        if len(conversion_job_folders) == 0:
            conversion_job_folder = ConversionJobFolder()
            conversion_job_folder.conversion_job = conversion_job
            conversion_job_folder.folder_path = folder_path
            conversion_job_folder.save()
            conversion_job_folders = ConversionJobFolder.get_conversion_job_folder_by_path(
                conversion_job, folder_path)

        if len(conversion_job_folders) == 1:
            return conversion_job_folders
        else:
            raise Exception("Number of found conversion_job_folders must be 1")

    def __get_conversion_job_files_query_set(self):
        conversion_job = self.__get_conversion_job_query_set()[0]
        conversion_job_files = ConversionJobFile.get_conversion_job_files_by_conversion_job(
            conversion_job)
        return conversion_job_files

    def __get_conversion_job_files_query_set_from_file_match(self, file_match):
        conversion_job_files = ConversionJobFile.get_conversion_job_files_by_file_match(
            file_match)
        return conversion_job_files

    def __get_conversion_job_urls_query_set(self):
        conversion_job = self.__get_conversion_job_query_set()[0]
        conversion_job_urls = ConversionJobUrl.get_conversion_job_urls(
            conversion_job)
        return conversion_job_urls

    def __get_conversion_job_shell_parameter_query_set(self):
        conversion_job = self.__get_conversion_job_query_set()[0]
        conversion_job_shell_parameters = ConversionJobShellParameter.get_conversion_job_shell_parameters(
            conversion_job)
        return conversion_job_shell_parameters

    def get_job_identifier(self):
        return self.__job_identifier

    def set_export_format_name(self, export_format_name):
        conversion_job = self.__get_conversion_job_query_set()
        conversion_job.update(export_format_name=export_format_name)

    def get_export_format_name(self):
        conversion_job = self.__get_conversion_job_query_set()
        return conversion_job[0].export_format_name

    def set_source_srs(self, source_srs):
        if len(source_srs) <= 8 and source_srs.isdigit():
            conversion_job = self.__get_conversion_job_query_set()
            conversion_job.update(source_srs=int(source_srs))

    def get_source_srs(self):
        conversion_job = self.__get_conversion_job_query_set()
        return conversion_job[0].source_srs

    def set_target_srs(self, target_srs):
        target_srs_string = str(target_srs)
        if len(target_srs_string) <= 8 and target_srs_string.isdigit():
            conversion_job = self.__get_conversion_job_query_set()
            conversion_job.update(target_srs=int(target_srs_string))

    def get_target_srs(self):
        conversion_job = self.__get_conversion_job_query_set()
        return conversion_job[0].target_srs

    def set_simplify_parameter(self, simplify_parameter):
        simplify_parameter_string = str(simplify_parameter)
        try:
            simplify_parameter_float = float(simplify_parameter_string)
        except ValueError:
            simplify_parameter_float = 0
        if simplify_parameter_float < 0:
            simplify_parameter_float = 0
        conversion_job = self.__get_conversion_job_query_set()
        conversion_job.update(simplify_parameter=simplify_parameter_float)

    def get_simplify_parameter(self):
        conversion_job = self.__get_conversion_job_query_set()
        return conversion_job[0].simplify_parameter

    def get_original_file_name(self, file_id):
        file_query_set = self.__get_conversion_job_files_query_set().filter(
            file_id=file_id)
        return file_query_set[0].file_name

    def get_file_name(self, file_id):
        file_query_set = self.__get_conversion_job_files_query_set().filter(
            file_id=file_id)
        return file_query_set[0].matched_file_name

    def add_file_match(
            self,
            source_path,
            file_dict,
            ogr_format_name,
            is_archive,
            is_valid):
        conversion_job_folder = self.__get_conversion_job_folder_query_set(
            source_path)[0]
        conversion_job_file_match = ConversionJobFileMatch()
        conversion_job_file_match.conversion_job_folder = conversion_job_folder
        conversion_job_file_match.identified_format_name = ogr_format_name
        conversion_job_file_match.is_archive = is_archive
        conversion_job_file_match.is_valid = is_valid
        conversion_job_file_match.save()
        for file_id, file_name in file_dict.items():
            conversion_job_file = ConversionJobFile()
            conversion_job_file.conversion_job = self.__get_conversion_job_query_set()[
                0]
            conversion_job_file.file_match = conversion_job_file_match
            conversion_job_file.file_id = file_id
            conversion_job_file.file_name = file_name
            conversion_job_file.matched_file_name = file_name
            conversion_job_file.save()

    def get_files(self, file_id, include_removed_files=False):
        file_query_set = self.__get_conversion_job_files_query_set()
        file_dict = {}
        for file in file_query_set:
            if not file.is_removed or include_removed_files:
                file_dict[file.file_id] = file.file_name
        return file_dict

    def get_file_count(self):
        file_query_set = self.__get_conversion_job_files_query_set()
        return len(file_query_set)

    def get_free_file_id(self):
        file_id_tracker = ConversionJobFileIdTracker()
        file_id_tracker.conversion_job = self.__get_conversion_job_query_set()[
            0]
        file_id_tracker.save()
        return "id" + str(file_id_tracker.file_id)

    def remove_file(self, file_id):
        conversion_job_file = self.__get_conversion_job_files_query_set().filter(
            file_id=file_id)
        conversion_job_file.update(is_removed=True)

    def file_removed(self, file_id):
        conversion_job_file = self.__get_conversion_job_files_query_set().filter(
            file_id=file_id)
        if not conversion_job_file:
            return True
        return conversion_job_file[0].is_removed

    def set_file_uploaded(self, file_id):
        file_query_set = self.__get_conversion_job_files_query_set().filter(
            file_id=file_id)
        file_query_set.update(is_uploaded=True)

    def file_ready_for_process(self, file_id):
        matched_files = self.get_matched_files(file_id)
        if len(matched_files) == 0:
            return False

        is_ready = True
        for matched_file in matched_files:
            if not matched_file.is_uploaded:
                is_ready = False
        return is_ready

    def filematch_try_process(self, file_id):
        file_query_set = self.__get_conversion_job_files_query_set().filter(
            file_id=file_id)
        file_match = file_query_set[0].file_match
        if file_match.in_process_id == '':
            file_match.in_process_id = file_id
            file_match.save(update_fields=('in_process_id',))
            return True
        else:
            return False

    def filematch_in_process(self, file_id):
        file_query_set = self.__get_conversion_job_files_query_set().filter(
            file_id=file_id)
        return file_query_set[0].file_match.in_process_id != ''

    def filematch_processing_id(self, file_id):
        file_query_set = self.__get_conversion_job_files_query_set().filter(
            file_id=file_id)
        return file_query_set[0].file_match.in_process_id

    def set_file_processed(self, file_id):
        file_query_set = self.__get_conversion_job_files_query_set().filter(
            file_id=file_id)
        file_query_set.update(is_processed=True)

    def get_file_processed(self, file_id):
        file_query_set = self.__get_conversion_job_files_query_set().filter(
            file_id=file_id)
        return file_query_set[0].is_processed

    def get_matched_files(self, file_id):
        file_query_set = self.__get_conversion_job_files_query_set().filter(
            file_id=file_id)
        file_match = file_query_set[0].file_match
        matched_file_query_set = self.__get_conversion_job_files_query_set_from_file_match(
            file_match)

        return list(matched_file_query_set)

    def get_unprocessed_files(self):
        unprocessed_files = []

        for file_id, processed in self.__processed_file_dict.items():
            if not processed:
                unprocessed_files.append(file_id)

        return unprocessed_files

    def add_webservice_url(self, url_id, url):
        conversion_job_url = ConversionJobUrl()
        conversion_job_url.conversion_job = self.__get_conversion_job_query_set()[
            0]
        conversion_job_url.url_id = url_id
        conversion_job_url.url = url
        conversion_job_url.save()

    def get_webservice_url(self, url_id):
        url_query_set = self.__get_conversion_job_urls_query_set().filter(
            url_id=url_id)
        if len(url_query_set) == 1:
            return url_query_set[0].url
        else:
            return ''

    def add_webservice_urls(self, url_dict):
        for url_id, url in url_dict:
            self.add_webservice_url(url_id, url)

    def get_webservice_urls(self):
        url_query_set = self.__get_conversion_job_urls_query_set()
        url_dict = {}
        for webservice_url in url_query_set:
            url_dict[webservice_url.url_id] = webservice_url.url
        return url_dict

    def get_webservice_urls_count(self):
        url_query_set = self.__get_conversion_job_urls_query_set()
        return len(url_query_set)

    def remove_webservice_url(self):
        raise Exception('Not implemented')

    def add_shell_parameter_object(self, shell_parameter):
        self.add_shell_parameter(
            shell_parameter.prefix,
            shell_parameter.parameter_name,
            shell_parameter.parameter_value,
            shell_parameter.value_quotation_marks)

    def add_shell_parameter(
            self,
            prefix,
            parameter_name,
            parameter_value,
            value_quotation_marks):
        shell_parameter = ConversionJobShellParameter()
        shell_parameter.conversion_job = self.__get_conversion_job_query_set()[
            0]
        shell_parameter.prefix = prefix
        shell_parameter.parameter_name = parameter_name
        shell_parameter.parameter_value = parameter_value
        shell_parameter.value_quotation_marks = value_quotation_marks
        shell_parameter.save()

    def get_shell_parameters(self):
        shell_parameters_query_set = self.__get_conversion_job_shell_parameter_query_set()
        shell_parameters_list = []
        for shell_parameter in shell_parameters_query_set:
            shell_parameters_list.append(shell_parameter)
        return shell_parameters_list

    def set_upload_folder_path(self, upload_folder_path):
        conversion_job = self.__get_conversion_job_query_set()
        conversion_job.update(upload_folder_path=upload_folder_path)

    def get_upload_folder_path(self):
        conversion_job = self.__get_conversion_job_query_set()
        return conversion_job[0].upload_folder_path

    def set_extract_folder_path(self, extract_folder_path):
        conversion_job = self.__get_conversion_job_query_set()
        conversion_job.update(extract_folder_path=extract_folder_path)

    def get_extract_folder_path(self):
        conversion_job = self.__get_conversion_job_query_set()
        return conversion_job[0].extract_folder_path

    def set_output_folder_path(self, output_folder_path):
        conversion_job = self.__get_conversion_job_query_set()
        conversion_job.update(output_folder_path=output_folder_path)

    def get_output_folder_path(self):
        conversion_job = self.__get_conversion_job_query_set()
        return conversion_job[0].output_folder_path

    def set_download_folder_path(self, download_folder_path):
        conversion_job = self.__get_conversion_job_query_set()
        conversion_job.update(download_folder_path=download_folder_path)

    def get_download_folder_path(self):
        conversion_job = self.__get_conversion_job_query_set()
        return conversion_job[0].download_folder_path class DownloadHandler:

    '''
    Class handling download items
    '''

    def __init__(self, job_identifier):
        if job_identifier is not None:
            self.__job_identifier = job_identifier
        else:
            raise Exception("job_identifier may not be None")

    def __get_download_item_query_set(self):
        download_items = DownloadItem.get_download_item(self.__job_identifier)
        if len(download_items) == 0:
            download_item = DownloadItem()
            download_item.job_identifier = self.__job_identifier
            download_item.download_caption = ''
            download_item.is_downloaded = False
            download_item.save()

            download_items = DownloadItem.get_download_item(
                self.__job_identifier)

        return download_items

        def get_job_identifier(self):
            return self.__job_identifier

    def add_download_item(self):
        download_item = self.__get_download_item_query_set()
        return download_item

    def set_download_caption(self, download_caption):
        download_item = self.__get_download_item_query_set()
        download_item.update(download_caption=download_caption)

    def get_download_caption(self):
        download_item = self.__get_download_item_query_set()
        return download_item[0].download_caption

    def set_is_downloaded(self):
        download_item = self.__get_download_item_query_set()
        download_item.update(is_downloaded=True)

    def get_is_downloaded(self):
        download_item = self.__get_download_item_query_set()
        return download_item[0].is_downloaded

    def remove_download_item(self):
        download_item = self.__get_download_item_query_set()
        download_item.delete()

    def get_download_file_information(self):
        '''
        Returns a download file in a wrapper and its name and size
        '''

        file_path = filemanager.get_download_file_path(
            self.__job_identifier.job_id)

        if self.download_file_exists():
            wrapper = FileWrapper(open(file_path, 'rb'))
            file_name = os.path.split(file_path)[1]
            file_size = os.path.getsize(file_path)
            return wrapper, file_name, file_size
        else:
            return None

    def get_download_file_size(self):
        file_path = filemanager.get_download_file_path(
            self.__job_identifier.job_id)

        if self.download_file_exists():
            return os.path.getsize(file_path)
        else:
            return -1

    def download_file_exists(self):
        return filemanager.download_file_exists(self.__job_identifier.job_id) class LogHandler:

    '''
    Class to read and write log entries.
    '''

    def __init__(self, job_identifier):
        if job_identifier is not None:
            self.__job_identifier = job_identifier
        else:
            raise Exception("job_identifier may not be None")

    def __get_log_entry_query_set(self):
        log_entries = LogEntry.get_log_entry(
            self.__job_identifier.session_key,
            self.__job_identifier.job_id)

        if len(log_entries) == 0:
            # Creat log entry if it doesen't exist yet
            log_entry = LogEntry()
            log_entry.session_key = self.__job_identifier.session_key
            log_entry.job_id = self.__job_identifier.job_id
            log_entry.client_job_token = self.__job_identifier.client_job_token
            log_entry.start_time = datetimehandler.get_default_datetime()
            log_entry.end_time = datetimehandler.get_default_datetime()
            log_entry.download_file_size = -1
            log_entry.save()
            log_entries = LogEntry.get_log_entry(
                self.__job_identifier.session_key,
                self.__job_identifier.job_id)

        if len(log_entries) == 1:
            return log_entries
        else:
            raise Exception("Number of found log_entries must be 1")

    def __get_ogr_log_entry_query_set(self):
        return OGRlogEntry.get_ogr_log_entry(
            self.__get_log_entry_query_set()[0])

    def get_job_identifier(self):
        return self.__job_identifier

    def set_client_ip(self, client_ip):
        log_entry = self.__get_log_entry_query_set()
        log_entry.update(client_ip=client_ip)

    def get_client_ip(self):
        log_entry = self.__get_log_entry_query_set()
        return log_entry[0].client_ip

    def set_client_language(self, client_language):
        log_entry = self.__get_log_entry_query_set()
        log_entry.update(client_language=client_language)

    def get_client_language(self):
        log_entry = self.__get_log_entry_query_set()
        return log_entry[0].client_language

    def set_client_user_agent(self, client_user_agent):
        log_entry = self.__get_log_entry_query_set()
        log_entry.update(client_user_agent=client_user_agent)

    def get_client_user_agent(self):
        log_entry = self.__get_log_entry_query_set()
        return log_entry[0].client_user_agent

    def set_input_type(self, input_type):
        log_entry = self.__get_log_entry_query_set()
        log_entry.update(input_type=input_type)

    def get_input_type(self):
        log_entry = self.__get_log_entry_query_set()
        return log_entry[0].input_type

    def set_start_time(self):
        log_entry = self.__get_log_entry_query_set()
        log_entry.update(start_time=datetimehandler.get_now())

    def get_start_time(self):
        log_entry = self.__get_log_entry_query_set()
        return log_entry[0].start_time

    def set_end_time(self):
        log_entry = self.__get_log_entry_query_set()
        log_entry.update(end_time=datetimehandler.get_now())

    def get_end_time(self):
        log_entry = self.__get_log_entry_query_set()
        return log_entry[0].end_time

    def set_export_format_name(self, export_format):
        log_entry = self.__get_log_entry_query_set()
        log_entry.update(export_format=export_format)

    def get_export_format_name(self):
        log_entry = self.__get_log_entry_query_set()
        return log_entry[0].export_format

    def set_source_srs(self, source_srs):
        log_entry = self.__get_log_entry_query_set()
        log_entry.update(source_srs=source_srs)

    def get_source_srs(self):
        log_entry = self.__get_log_entry_query_set()
        return log_entry[0].source_srs

    def set_target_srs(self, target_srs):
        log_entry = self.__get_log_entry_query_set()
        log_entry.update(target_srs=target_srs)

    def get_target_srs(self):
        log_entry = self.__get_log_entry_query_set()
        return log_entry[0].target_srs

    def set_simplify_parameter(self, simplify_parameter):
        log_entry = self.__get_log_entry_query_set()
        log_entry.update(simplify_parameter=simplify_parameter)

    def get_simplify_parameter(self):
        log_entry = self.__get_log_entry_query_set()
        return log_entry[0].simplify_parameter

    def set_download_file_size(self, file_size):
        log_entry = self.__get_log_entry_query_set()
        log_entry.update(download_file_size=file_size)

    def get_download_file_size(self):
        log_entry = self.__get_log_entry_query_set()
        return log_entry[0].download_file_size

    def set_file_downloaded(self):
        log_entry = self.__get_log_entry_query_set()
        log_entry.update(file_downloaded=True)

    def get_file_downloaded(self):
        log_entry = self.__get_log_entry_query_set()
        return log_entry[0].file_downloaded

    def set_has_download_file(self, has_download_file):
        log_entry = self.__get_log_entry_query_set()
        log_entry.update(has_download_file=has_download_file)

    def get_has_download_file(self):
        log_entry = self.__get_log_entry_query_set()
        return log_entry[0].has_download_file

    def set_all_files_converted(self):
        all_files_converted = True
        for ogr_log_entry in self.__get_ogr_log_entry_query_set():
            if not ogr_log_entry.has_output_file:
                all_files_converted = False
                break
        all_files_converted = all_files_converted and len(
            self.__get_ogr_log_entry_query_set()) > 0

        log_entry = self.__get_log_entry_query_set()
        log_entry.update(all_files_converted=all_files_converted)

    def get_all_files_converted(self):
        log_entry = self.__get_log_entry_query_set()
        return log_entry[0].all_files_converted

    def set_has_no_error(self):
        has_no_error = True
        for ogr_log_entry in self.__get_ogr_log_entry_query_set():
            if ogr_log_entry.error_message != '':
                has_no_error = False
                break
        has_no_error = has_no_error and len(
            self.__get_ogr_log_entry_query_set()) > 0

        log_entry = self.__get_log_entry_query_set()
        log_entry.update(has_no_error=has_no_error)

    def get_has_no_error(self):
        log_entry = self.__get_log_entry_query_set()
        return log_entry[0].has_no_error

    def add_ogr_log_entry(
            self,
            sub_path,
            input_string,
            ogr_command,
            ogr_error_message,
            has_output_file):
        command_max_length = OGRlogEntry._meta.get_field('command').max_length
        if len(ogr_command) > command_max_length:
            ogr_command = ogr_command[0:command_max_length - 7] + ' [...]'

        error_message_max_length = OGRlogEntry._meta.get_field(
            'error_message').max_length
        if len(ogr_error_message) > error_message_max_length:
            split_position1 = error_message_max_length // 3 * 2 - 10
            split_position2 = error_message_max_length - split_position1 - 20
            ogr_error_message = ogr_error_message[
                0:split_position1] + '...\n\n[...]\n\n...' + ogr_error_message[-split_position2:]

        log_entry = self.__get_log_entry_query_set()[0]

        ogr_log_entry = OGRlogEntry()
        ogr_log_entry.log_entry = log_entry
        ogr_log_entry.sub_path = sub_path
        ogr_log_entry.input = input_string
        ogr_log_entry.command = ogr_command
        ogr_log_entry.error_message = ogr_error_message
        ogr_log_entry.has_output_file = has_output_file
        ogr_log_entry.save()

    def get_ogr_log_entries(self):
        return self.__get_ogr_log_entry_query_set() def get_session(session_key):
def is_valid_key(session_key): ROOT_DIR = environ.Env().path(
    'APP_DIR', default=str(
        environ.Path(
            os.path.dirname(__file__)) - 1))
APPS_DIR = ROOT_DIR.path('OGRgeoConverter')
DATA_DIR = environ.Env().path('DATA_DIR', default='/data')
OUTPUT_DIR = environ.Env().path('OUTPUT_DIR', default='/output')
STATICFILES_DIR = environ.Env().path('STATICFILES_DIR', default='/staticfiles')
METADATA = get_metadata()
DJANGO_APPS = [
    # Default Django apps:
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    # 'django.contrib.sites',
    'django.contrib.messages',
#     'whitenoise.runserver_nostatic',
    'django.contrib.staticfiles',

    # Useful template tags:
    'django.contrib.humanize',

    # Admin
    'django.contrib.admin',
]
THIRD_PARTY_APPS = []
LOCAL_APPS = [
    'OGRgeoConverter',
    # 'version',
]
INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS
MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
#    'whitenoise.middleware.WhiteNoiseMiddleware',
)
MIGRATION_MODULES = {
    # 'sites': 'geoconverter.contrib.sites.migrations',
}
DEBUG = env("DJANGO_DEBUG")
TEMPLATE_DEBUG = DEBUG
FIXTURE_DIRS = [
    str(APPS_DIR('fixtures')),
]
EMAIL_BACKEND = env("DJANGO_EMAIL_BACKEND")
EMAIL_HOST = env("DJANGO_EMAIL_HOST")
EMAIL_HOST_PASSWORD = env("DJANGO_EMAIL_HOST_PASSWORD")
EMAIL_HOST_USER = env("DJANGO_EMAIL_HOST_USER")
EMAIL_PORT = env("DJANGO_EMAIL_PORT")
EMAIL_SUBJECT_PREFIX = env("DJANGO_EMAIL_SUBJECT_PREFIX")
EMAIL_TIMEOUT = env("DJANGO_EMAIL_TIMEOUT")
DEFAULT_FROM_EMAIL = env("DJANGO_DEFAULT_FROM_EMAIL")
SERVER_EMAIL = env("DJANGO_SERVER_EMAIL")
EMAIL_USE_TLS = env('DJANGO_EMAIL_USE_TLS')
EMAIL_USE_SSL = env('DJANGO_EMAIL_USE_SSL')
EMAIL_FILE_PATH = str(env("DJANGO_EMAIL_FILE_PATH"))
ADMINS = env(
    'DJANGO_ADMINS',
    cast=[
        lambda x: tuple(
            x.split(';'))],
    parse_default=True)
MANAGERS = ADMINS
DATABASES = {
    'default': env.db("DJANGO_DB_DEFAULT_URL"),
    'sessions_db': env.db("DJANGO_DB_SESSIONS_URL"),
    'ogrgeoconverter_db': env.db("DJANGO_DB_CONVERTER_URL"),
    'ogrgeoconverter_log_db': env.db("DJANGO_DB_LOGS_URL"),
    'ogrgeoconverter_conversion_jobs_db': env.db("DJANGO_DB_JOBS_URL"),
}
DATABASE_ROUTERS = ['GeoConverter.database.DatabaseRouter']
TIME_ZONE = 'Europe/Zurich'
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
USE_I18N = True
USE_L10N = True
USE_TZ = True
TEMPLATE_DIRS = (
    str(ROOT_DIR.path('templates')),
)
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)
STATIC_ROOT = env('DJANGO_STATIC_ROOT')
STATIC_URL = env('DJANGO_STATIC_URL')
STATICFILES_DIRS = env.list('DJANGO_STATICFILES_DIRS')
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATICFILES_STORAGE = env('DJANGO_STATICFILES_STORAGE')
MEDIA_ROOT = env("DJANGO_MEDIA_ROOT")
PRIVATE_MEDIA_ROOT = env("DJANGO_PRIVATE_MEDIA_ROOT")
MEDIA_URL = ''
ROOT_URLCONF = 'GeoConverter.urls'
WSGI_APPLICATION = 'GeoConverter.wsgi.application'
AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
)
CACHES = {
    'default': env.cache('DJANGO_CACHE_DEFAULT_URL'),
}
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s '
                      '%(process)d %(thread)d %(message)s'
        },
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'console': {
            'level': 'WARNING',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    },
    'root': {
        'level': 'WARNING',
        'handlers': ['console'],
    },
}
SECRET_KEY = env("DJANGO_SECRET_KEY")
ALLOWED_HOSTS = env(
    'DJANGO_ALLOWED_HOSTS',
    cast=tolist_with_env_lookup,
    parse_default=True)
X_FRAME_OPTIONS = env("DJANGO_X_FRAME_OPTIONS")
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
def tolist_with_env_lookup(x):
import json
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.http import HttpResponse
from django.http import HttpResponseServerError
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.core.files.uploadedfile import SimpleUploadedFile
from django.conf.global_settings import LANGUAGES
from django.utils import translation
from OGRgeoConverter.forms import GeoConverterFilesForm, GeoConverterWebservicesForm
from OGRgeoConverter.jobs import jobidentification
from OGRgeoConverter.jobs import jobprocessing
from OGRgeoConverter.models import OgrFormat
from OGRgeoConverter.models import GlobalOgrShellParameter
from OGRgeoConverter.geoconverter.filematching import FileMatcher
from OGRgeoConverter.modelhandlers import downloadhandler
from OGRgeoConverter.modelhandlers.jobhandler import JobHandler
from OGRgeoConverter.modelhandlers.downloadhandler import DownloadHandler
from OGRgeoConverter.modelhandlers.loghandler import LogHandler
from OGRgeoConverter.geoconverter import sessionhandler
from GeoConverter import settings

        for global_shell_parameter in GlobalOgrShellParameter.get_active_parameters():
            job_handler.add_shell_parameter_object(global_shell_parameter)

        job_handler.set_export_format_name(export_format_name)
        job_handler.set_source_srs(source_srs)
        job_handler.set_target_srs(target_srs)
        job_handler.set_simplify_parameter(simplify_parameter)

        download_handler.set_download_caption(download_name)

        log_handler.set_start_time()
        log_handler.set_client_ip(client_ip)
        log_handler.set_client_language(client_language)
        log_handler.set_client_user_agent(client_user_agent)
        log_handler.set_input_type('files')
        log_handler.set_export_format_name(
            job_handler.get_export_format_name())
        log_handler.set_source_srs(job_handler.get_source_srs())
        log_handler.set_target_srs(job_handler.get_target_srs())
        log_handler.set_simplify_parameter(
            job_handler.get_simplify_parameter())

        jobprocessing.initialize_conversion_job(job_identifier)

        return HttpResponse('success')
    else:
        return redirect_to_main_page(request)


@csrf_exempt
def process_upload(request, client_job_token):
    file_id = request.GET['qqfileid']
    file_data = SimpleUploadedFile(request.GET['qqfile'], request.body)

    job_identifier = jobidentification.get_job_identifier_by_client_job_token(
        request.session.session_key,
        client_job_token)

    jobprocessing.store_file(job_identifier, file_id, file_data)
    jobprocessing.process_file(job_identifier, file_id)

    response_data = {}
    response_data['success'] = True

    # mimetype should be "application/json" but than IE offers the response as
    # download...
    return HttpResponse(json.dumps(response_data), mimetype="text/plain")


def remove_file(request, client_job_token, file_id):
    if request.method == 'POST':
        job_identifier = jobidentification.get_job_identifier_by_client_job_token(
            request.session.session_key, client_job_token)
        if job_identifier is not None:
            job_handler = JobHandler(job_identifier)
            job_handler.remove_file(file_id)
        return HttpResponse('success')
    else:
        return redirect_to_main_page(request)


def finish_conversion_job(request, client_job_token):
    if request.method == 'POST':
        job_identifier = jobidentification.get_job_identifier_by_client_job_token(
            request.session.session_key, client_job_token)

        jobprocessing.create_download_file(job_identifier)

        log_handler = LogHandler(job_identifier)
        download_handler = DownloadHandler(job_identifier)

        download_handler.add_download_item()

        log_handler.set_end_time()
        log_handler.set_download_file_size(
            download_handler.get_download_file_size() /
            1024)
        log_handler.set_has_download_file(
            download_handler.download_file_exists())
        log_handler.set_all_files_converted()
        log_handler.set_has_no_error()

        jobprocessing.cleanup()

        response_data = {}
        response_data['job_id'] = job_identifier.job_id
        response_data['successful'] = download_handler.download_file_exists()
        return HttpResponse(json.dumps(response_data), mimetype="text/plain")
    return HttpResponse('success')


def download_output_file(request, *job_id_args):
    session_key = request.session.session_key
    job_id = jobidentification.join_job_id(*job_id_args)
    job_identifier = jobidentification.get_job_identifier_by_job_id(
        session_key,
        job_id)

    download_handler = DownloadHandler(job_identifier)
    log_hander = LogHandler(job_identifier)

    download_file_information = download_handler.get_download_file_information()

    if download_file_information is not None:
        file, file_name, file_size = download_file_information

        response = HttpResponse(file, content_type='application/zip')
        response['Content-Disposition'] = 'attachment; filename=' + file_name
        response['Content-Length'] = file_size

        download_handler.set_is_downloaded()
        log_hander.set_file_downloaded()

        return response
    else:
        return HttpResponseServerError('Error: file does not exist!')


def remove_download_item(request, *job_id_args):

    job_id = jobidentification.join_job_id(*job_id_args)