Esempio n. 1
0
    def clean_qgis_file(self):
        try:
            qgis_file = self.cleaned_data['qgis_file']

            # validate extension
            file_extension = os.path.splitext(qgis_file.name)[1]
            if file_extension.lower() not in ('.qgs', '.qgz'):
                raise Exception(_("File must have 'qgs' or 'qgz' extension"))

            # for QGIS qgz file format
            if file_extension.lower() == '.qgz':
                zfile = zipfile.ZipFile(qgis_file, 'r')
                for fileinfo in zfile.infolist():
                    if os.path.splitext(
                            fileinfo.filename)[1].lower() == '.qgs':
                        qzfile = fileinfo.filename

                # put qzfile to qgis_file
                qgis_file = ContentFile(zfile.open(qzfile, 'r').read(),
                                        name=qzfile)

                # Update path property for QGIS api
                qgis_file.path = self.cleaned_data['qgis_file'].file.path

            kwargs = {'group': self.group}
            if self.instance.pk:
                kwargs['instance'] = self.instance
            self.qgisProject = QgisProject(qgis_file, **kwargs)
            self.qgisProject.clean()
        except Exception as e:
            raise ValidationError(str(e))
        return qgis_file
Esempio n. 2
0
def get_graph_file(path, name=None):
    """Get graph file in Django object for testing purposes"""
    fname = join(dirname(__file__), path)
    # We must use ContentFile to avoid SuspiciousOperation errors.
    # We read the data outside of django and pass it as a string.
    f = ContentFile(open(fname).read())
    if name is None:
        name = os.path.basename(path)
    f.name = name
    f.path = name
    return f
Esempio n. 3
0
 def handle(self, *args, **options):
     fname = join(dirname(cd20.__file__), 'data', 'karate.gml')
     # Get existing (or new) dataset #10:
     ds = Dataset.objects.get_or_create(id=10)[0]
     if ds.netfile: ds.netfile.delete()
     # We must use ContentFile to avoid SuspiciousOperation errors.
     # We read the data outside of django and pass it as a string.
     f = ContentFile(open(fname).read())
     f.name = 'karate.gml'
     f.path = 'karate.gml'
     ds.set_network(f)
     ds.save()
Esempio n. 4
0
    def clean_qgis_file(self):
        try:
            qgis_file = self.cleaned_data['qgis_file']

            kwargs = {'group': self.group, 'original_name': qgis_file.name}

            # validate extension
            file_extension = os.path.splitext(qgis_file.name)[1]
            if file_extension.lower() not in ('.qgs', '.qgz'):
                raise Exception(_("File must have 'qgs' or 'qgz' extension"))

            # for QGIS qgz file format
            if file_extension.lower() == '.qgz':
                zfile = zipfile.ZipFile(qgis_file, 'r')
                for fileinfo in zfile.infolist():
                    if os.path.splitext(
                            fileinfo.filename)[1].lower() == '.qgs':
                        qzfile = fileinfo.filename

                # put qzfile to qgis_file
                qgis_file = ContentFile(zfile.open(qzfile, 'r').read(),
                                        name=qzfile)

                # Update path property for QGIS api
                qgis_file.path = self.cleaned_data['qgis_file'].file.path

            if self.instance.pk:
                kwargs['instance'] = self.instance

            # Handles authentication before checking the project file
            authentication_id = self.data.get('authentication_id')
            authentication_username = self.data.get('authentication_username')
            authentication_password = self.data.get('authentication_password')
            if authentication_id and authentication_username and authentication_password:
                QgisAuth.objects.create(
                    id=authentication_id,
                    name='Auth config: %s' % authentication_id,
                    config=
                    "{{'password': '******', 'username': '******', 'realm': ''}}"
                    .format(username=authentication_username,
                            password=authentication_password))

            self.qgisProject = QgisProject(qgis_file, **kwargs)
            self.qgisProject.clean()
        except Exception as e:
            raise ValidationError(str(e))
        return qgis_file