示例#1
0
def add_map_revision(oramap_path,
                     user,
                     parser,
                     game_mod,
                     info,
                     policy_options,
                     posted_date,
                     revision=1,
                     previous_revision_id=0):
    """ Parse and save a given oramap into the database.
        The input file is not modified, and must be cleaned up afterwards by the caller.
        Returns a Maps model or raises an InvalidMapException on error
    """
    print('Running --map-hash')
    hash_retcode, map_hash = utility.run_utility_command(
        parser, game_mod, ['--map-hash', oramap_path])

    if hash_retcode != 0 or not map_hash:
        raise InvalidMapException('Hash calculation failed.')

    # Require unique map hashes - allowing maps to have multiple RC entries
    # makes it difficult to return consistent data through the map API
    if Maps.objects.filter(map_hash=map_hash).exists():
        raise InvalidMapException("Map has already been uploaded.")

    # Parse metadata
    print('Parsing map.yaml metadata')
    metadata = utility.parse_map_metadata(oramap_path)
    if not metadata:
        misc.send_email_to_admin_OnMapFail(oramap_path)
        raise InvalidMapException('Unable to parse map metadata.')

    if int(metadata['mapformat']) < 10:
        raise InvalidMapException(
            'Unable to import maps older than map format 10.')

    # Only attempt to parse rules for the default mods
    # This will be fixed properly once we move to mod-based parsing
    is_known_mod = metadata['game_mod'] in ['ra', 'cnc', 'd2k', 'ts']

    if is_known_mod:
        rules_retcode, custom_rules = utility.run_utility_command(
            parser, metadata['game_mod'], ['--map-rules', oramap_path])

        if rules_retcode != 0:
            misc.send_email_to_admin_OnMapFail(oramap_path)
            raise InvalidMapException('Failed to parse custom rules.')

        # TODO: Check against the game's Ruleset.DefinesUnsafeCustomRules code instead of line count
        advanced = len(custom_rules.split("\n")) > 8
        base64_rules = base64.b64encode(custom_rules.encode()).decode()
    else:
        base64_rules = ''
        advanced = False

    expect_metadata_keys = [
        'title', 'author', 'categories', 'players', 'game_mod', 'width',
        'height', 'bounds', 'mapformat', 'spawnpoints', 'tileset',
        'base64_players', 'lua'
    ]

    keyargs = {}
    for key in expect_metadata_keys:
        if key not in metadata:
            raise InvalidMapException('Map metadata missing required key: ' +
                                      key + '.')
        keyargs[key] = metadata[key]

    # Add record to Database
    item = Maps(
        map_hash=map_hash,
        revision=revision,
        pre_rev=previous_revision_id,
        next_rev=0,
        posted=posted_date,
        viewed=0,
        base64_rules=base64_rules,
        parser=parser,
        user=user,
        info=info,
        downloading=True,
        requires_upgrade=not is_known_mod,
        advanced_map=advanced,
        policy_cc=policy_options['cc'],
        policy_commercial=policy_options['commercial'],
        policy_adaptations=policy_options['adaptations'],
        description='',  # Obsolete field
        map_type='',  # Obsolete field
        shellmap=False,  # Obsolete field
        legacy_map=False,  # Obsolete field
        **keyargs)
    item.save()

    # Copy the updated map to its new data location
    item_path = os.path.join(settings.BASE_DIR, 'openra', 'data', 'maps',
                             str(item.id))
    item_content_path = os.path.join(item_path, 'content')
    if not os.path.exists(item_content_path):
        os.makedirs(item_content_path)

    item_map_path = os.path.join(item_path, os.path.basename(oramap_path))
    shutil.copy(oramap_path, item_map_path)

    if previous_revision_id:
        previous_item = Maps.objects.get(id=previous_revision_id)
        previous_item.next_rev = item.id
        previous_item.save()

    # Extract the oramap contents
    # TODO: Why do we need this?
    with zipfile.ZipFile(item_map_path, mode='a') as oramap:
        try:
            oramap.extractall(item_content_path)
        except Exception:
            pass

    if is_known_mod:
        print('Running --check-yaml')
        lint_retcode, lint_output = utility.run_utility_command(
            parser, item.game_mod, ['--check-yaml', item_map_path])

        if lint_output:
            Lints(
                item_type='maps',
                map_id=item.id,
                version_tag=parser,
                pass_status=lint_retcode == 0,
                lint_output=lint_output,
                posted=timezone.now(),
            ).save()

        if lint_retcode == 0:
            item.requires_upgrade = False
            item.save()

    print("--- New revision: %s" % item.id)
    return item
示例#2
0
def process_upload(user_id, file, post, revision=1, previous_revision=0):
    """Upload a new revision of a map
       Returns the updated map revision or raises an InvalidMapException on error
    """
    parser = settings.OPENRA_VERSIONS[0]
    if post.get("parser", None) is not None:
        if post['parser'] not in settings.OPENRA_VERSIONS:
            raise InvalidMapException('Invalid parser.')
        parser = post['parser']

    user = User.objects.get(pk=user_id)

    # Check whether we can upload a new revision
    # and propagate the license info
    policy = {'cc': False, 'commercial': False, 'adaptations': ''}

    if previous_revision:
        query = Maps.objects.filter(id=previous_revision, user_id=user.id)
        if not query:
            raise InvalidMapException('Map is owned by another user.')

        previous_item = query[0]
        if previous_item.next_rev != 0:
            raise InvalidMapException('Map already has a later revision.')

        policy['cc'] = previous_item.policy_cc
        policy['commercial'] = previous_item.policy_commercial
        policy['adaptations'] = previous_item.policy_adaptations
    else:
        if post['policy_cc'] == 'cc_yes':
            policy['cc'] = True
            if post['commercial'] == "com_yes":
                policy['commercial'] = True
            if post['adaptations'] == "adapt_yes":
                policy['adaptations'] = "yes"
            elif post['adaptations'] == "adapt_no":
                policy['adaptations'] = "no"
            else:
                policy['adaptations'] = "yes and shared alike"

    # Create a temporary working directory and copy the oramap to it
    # Directory is automatically deleted when exiting the context manager
    with tempfile.TemporaryDirectory() as working_path:
        print('Temporary working directory: {}'.format(working_path))

        # Django's get_valid_filename makes the name safe
        upload_path = os.path.join(working_path, get_valid_filename(file.name))
        upload_ext = os.path.splitext(upload_path)[1].lower()
        with open(upload_path, 'wb+') as destination:
            for chunk in file.chunks():
                destination.write(chunk)

        mime_retcode, mime_type = utility.detect_mimetype(upload_path)
        if mime_retcode != 0 or not (
            (mime_type == 'application/zip' and upload_ext == '.oramap') or
            (mime_type == 'text/plain' and upload_ext in ['.mpr', '.ini'])):
            raise InvalidMapException('Unsupported file type: ' + mime_type)

        # TODO: Support importing from cnc/d2k/ts.
        # Note that cnc maps contain *two* files which must both be present
        if mime_type == 'text/plain':
            import_path = os.path.splitext(upload_path)[0] + '.oramap'

            # Ignore command output and retcode, we know it worked if an oramap is saved
            utility.run_utility_command(parser,
                                        'ra', ['--import-ra-map', upload_path],
                                        cwd=working_path)

            if os.path.exists(import_path):
                upload_path = import_path
            else:
                misc.send_email_to_admin_OnMapFail(upload_path)
                raise InvalidMapException('Failed to import legacy map.')

        # TODO: Replace hardcoded RA with the parser mod when that rewrite happens
        return add_map_revision(upload_path, user, parser,
                                'ra', post['info'].strip(), policy,
                                timezone.now(), revision, previous_revision)
示例#3
0
    def ProcessUploading(self, user_id, f, post, rev=1, pre_r=0):

        parser_to_db = list(reversed(list(settings.OPENRA_VERSIONS.values())))[
            0]  # default parser = the latest
        parser = settings.OPENRA_ROOT_PATH + parser_to_db

        if post.get("parser", None) is not None:
            parser_to_db = post['parser']
            parser = settings.OPENRA_ROOT_PATH + parser_to_db
            if 'git' in parser:
                parser = settings.OPENRA_BLEED_PARSER

        if pre_r != 0:
            mapObject = Maps.objects.filter(id=pre_r, user_id=user_id)
            if not mapObject:
                return 'Failed. You do not own map for which you want to upload a new revision.'
            if mapObject[0].next_rev != 0:
                return 'Failed. Unable to upload a new revision for map which already has one.'
            previous_policy_cc = mapObject[0].policy_cc
            previous_policy_commercial = mapObject[0].policy_commercial
            previous_policy_adaptations = mapObject[0].policy_adaptations
        tempname = '/tmp/openramap.oramap'
        with open(tempname, 'wb+') as destination:
            for chunk in f.chunks():
                destination.write(chunk)

        command = 'file -b --mime-type %s' % tempname
        proc = Popen(command.split(), stdout=PIPE).communicate()
        mimetype = proc[0].decode().strip()
        if not (mimetype == 'application/zip'
                and os.path.splitext(f.name)[1].lower() == '.oramap'):
            if not (mimetype == 'text/plain' and os.path.splitext(
                    f.name)[1].lower() in ['.mpr', '.ini']):
                return 'Failed. Unsupported file type.'

        name = f.name
        badChars = ": ; < > @ $ # & ( ) % '".split()
        for badchar in badChars:
            name = name.replace(badchar, "_")
        name = name.replace(" ", "_")
        # There can be weird chars still, if so: stop uploading
        findBadChars = re.findall(r'(\W+)', name)
        for bc in findBadChars:
            if bc not in ['.', '-']:
                return 'Failed. Your filename is bogus; rename and try again.'

        if mimetype == 'text/plain':
            if not self.LegacyImport(tempname, parser):
                misc.send_email_to_admin_OnMapFail(tempname)
                return 'Failed to import legacy map.'
            shutil.move(parser + "/" + self.legacy_name, tempname)
            name = os.path.splitext(name)[0] + '.oramap'
            self.legacy_map = True

        # Check if user has already uploaded the same map
        self.GetHash(tempname, parser)
        if 'Converted' in self.maphash and 'to MapFormat' in self.maphash:
            misc.send_email_to_admin_OnMapFail(tempname)
            return 'Failed to upload with this parser. MapFormat does not match. Try to upgrade your map or use different parser.'

        userObject = User.objects.get(pk=user_id)
        try:
            hashExists = Maps.objects.get(user_id=userObject.id,
                                          map_hash=self.maphash)
            self.UID = str(hashExists.id)
            return "Failed. You've already uploaded this map."
        except:
            pass  # all good

        # Read Yaml
        read_yaml_response = utility.ReadYaml(False, tempname)
        resp_map_data = read_yaml_response['response']
        if read_yaml_response['error']:
            misc.send_email_to_admin_OnMapFail(tempname)
            return resp_map_data

        # Read Rules
        base64_rules = {}
        base64_rules['data'] = ''
        base64_rules['advanced'] = resp_map_data['advanced']
        if int(resp_map_data['mapformat']) >= 10:
            base64_rules = utility.ReadRules(False, tempname, parser,
                                             resp_map_data['game_mod'])
            if (base64_rules['error']):
                print(base64_rules['response'])
        if base64_rules['advanced']:
            resp_map_data['advanced'] = True

        # Define license information
        cc = False
        commercial = False
        adaptations = ""
        if pre_r == 0:
            if post['policy_cc'] == 'cc_yes':
                cc = True
                if post['commercial'] == "com_yes":
                    commercial = True
                if post['adaptations'] == "adapt_yes":
                    adaptations = "yes"
                elif post['adaptations'] == "adapt_no":
                    adaptations = "no"
                else:
                    adaptations = "yes and shared alike"
        else:
            cc = previous_policy_cc
            commercial = previous_policy_commercial
            adaptations = previous_policy_adaptations

        # Add record to Database
        transac = Maps(
            user=userObject,
            title=resp_map_data['title'],
            description=resp_map_data['description'],
            info=post['info'].strip(),
            author=resp_map_data['author'],
            map_type=resp_map_data['map_type'],
            categories=resp_map_data['categories'],
            players=resp_map_data['players'],
            game_mod=resp_map_data['game_mod'],
            map_hash=self.maphash.strip(),
            width=resp_map_data['width'],
            height=resp_map_data['height'],
            bounds=resp_map_data['bounds'],
            mapformat=resp_map_data['mapformat'],
            spawnpoints=resp_map_data['spawnpoints'],
            tileset=resp_map_data['tileset'],
            shellmap=resp_map_data['shellmap'],
            base64_rules=base64_rules['data'],
            base64_players=resp_map_data['base64_players'],
            legacy_map=self.legacy_map,
            revision=rev,
            pre_rev=pre_r,
            next_rev=0,
            downloading=True,
            requires_upgrade=True,
            advanced_map=resp_map_data['advanced'],
            lua=resp_map_data['lua'],
            posted=timezone.now(),
            viewed=0,
            policy_cc=cc,
            policy_commercial=commercial,
            policy_adaptations=adaptations,
            parser=parser_to_db,
        )
        transac.save()
        self.UID = str(transac.id)

        self.map_full_path_directory = self.currentDirectory + __name__.split(
            '.')[0] + '/data/maps/' + self.UID + '/'

        try:
            if not os.path.exists(self.map_full_path_directory):
                os.makedirs(self.map_full_path_directory + 'content')
        except Exception as e:
            print("Failed to create directory for new map",
                  self.map_full_path_directory)
            transac.delete()  # Remove failed map from DB before raise
            raise

        if pre_r != 0:
            Maps.objects.filter(id=pre_r).update(next_rev=transac.id)

        self.map_full_path_filename = self.map_full_path_directory + name
        self.preview_filename = os.path.splitext(name)[0] + ".png"

        shutil.move(tempname, self.map_full_path_filename)

        self.UnzipMap()

        lint_check_response = utility.LintCheck(transac,
                                                self.map_full_path_filename,
                                                parser)
        if lint_check_response['error'] is False and lint_check_response[
                'response'] == 'pass_for_requested_parser':
            self.LintPassed = True

        if self.LintPassed:
            Maps.objects.filter(id=transac.id).update(requires_upgrade=False)
        else:
            Maps.objects.filter(id=transac.id).update(requires_upgrade=True)

        if int(resp_map_data['mapformat']) < 10:
            self.GenerateMinimap(resp_map_data['game_mod'], parser)

        #shp = multiprocessing.Process(target=self.GenerateSHPpreview, args=(resp_map_data['game_mod'], parser,), name='shppreview')
        #shp.start()
        print("--- New map: %s" % self.UID)
        return False  # no errors
示例#4
0
def add_map_revision(oramap_path, user,
                     parser, game_mod,
                     info, policy_options, posted_date,
                     revision=1, previous_revision_id=0):
    """ Parse and save a given oramap into the database.
        The input file is not modified, and must be cleaned up afterwards by the caller.
        Returns a Maps model or raises an InvalidMapException on error
    """
    print('Running --map-hash')
    hash_retcode, map_hash = utility.run_utility_command(parser, game_mod, [
        '--map-hash', oramap_path])

    if hash_retcode != 0 or not map_hash:
        raise InvalidMapException('Hash calculation failed.')

    # Require unique map hashes - allowing maps to have multiple RC entries
    # makes it difficult to return consistent data through the map API
    if Maps.objects.filter(map_hash=map_hash).exists():
        raise InvalidMapException("Map has already been uploaded.")

    # Parse metadata
    print('Parsing map.yaml metadata')
    metadata = utility.parse_map_metadata(oramap_path)
    if not metadata:
        misc.send_email_to_admin_OnMapFail(oramap_path)
        raise InvalidMapException('Unable to parse map metadata.')

    if int(metadata['mapformat']) < 10:
        raise InvalidMapException('Unable to import maps older than map format 10.')

    # Only attempt to parse rules for the default mods
    # This will be fixed properly once we move to mod-based parsing
    is_known_mod = metadata['game_mod'] in ['ra', 'cnc', 'd2k', 'ts']

    if is_known_mod:
        rules_retcode, custom_rules = utility.run_utility_command(parser, metadata['game_mod'], [
            '--map-rules', oramap_path])

        if rules_retcode != 0:
            misc.send_email_to_admin_OnMapFail(oramap_path)
            raise InvalidMapException('Failed to parse custom rules.')

        # TODO: Check against the game's Ruleset.DefinesUnsafeCustomRules code instead of line count
        advanced = len(custom_rules.split("\n")) > 8
        base64_rules = base64.b64encode(custom_rules.encode()).decode()
    else:
        base64_rules = ''
        advanced = False

    expect_metadata_keys = [
        'title', 'author', 'categories', 'players', 'game_mod',
        'width', 'height', 'bounds', 'mapformat', 'spawnpoints',
        'tileset', 'base64_players', 'lua'
    ]

    keyargs = {}
    for key in expect_metadata_keys:
        if key not in metadata:
            raise InvalidMapException('Map metadata missing required key: ' + key + '.')
        keyargs[key] = metadata[key]

    # Add record to Database
    item = Maps(
        map_hash=map_hash,
        revision=revision,
        pre_rev=previous_revision_id,
        next_rev=0,
        posted=posted_date,
        viewed=0,
        base64_rules=base64_rules,
        parser=parser,
        user=user,
        info=info,
        downloading=True,
        requires_upgrade=not is_known_mod,
        advanced_map=advanced,
        policy_cc=policy_options['cc'],
        policy_commercial=policy_options['commercial'],
        policy_adaptations=policy_options['adaptations'],
        description='', # Obsolete field
        map_type='', # Obsolete field
        shellmap=False, # Obsolete field
        legacy_map=False, # Obsolete field
        **keyargs
    )
    item.save()

    # Copy the updated map to its new data location
    item_path = os.path.join(settings.BASE_DIR, 'openra', 'data', 'maps', str(item.id))
    item_content_path = os.path.join(item_path, 'content')
    if not os.path.exists(item_content_path):
        os.makedirs(item_content_path)

    item_map_path = os.path.join(item_path, os.path.basename(oramap_path))
    shutil.copy(oramap_path, item_map_path)

    if previous_revision_id:
        previous_item = Maps.objects.get(id=previous_revision_id)
        previous_item.next_rev = item.id
        previous_item.save()

    # Extract the oramap contents
    # TODO: Why do we need this?
    with zipfile.ZipFile(item_map_path, mode='a') as oramap:
        try:
            oramap.extractall(item_content_path)
        except Exception:
            pass

    if is_known_mod:
        print('Running --check-yaml')
        lint_retcode, lint_output = utility.run_utility_command(parser, item.game_mod, [
            '--check-yaml',
            item_map_path
        ])

        if lint_output:
            Lints(
                item_type='maps',
                map_id=item.id,
                version_tag=parser,
                pass_status=lint_retcode == 0,
                lint_output=lint_output,
                posted=timezone.now(),
            ).save()

        if lint_retcode == 0:
            item.requires_upgrade = False
            item.save()

    print("--- New revision: %s" % item.id)
    return item
示例#5
0
def process_upload(user_id, file, post, revision=1, previous_revision=0):
    """Upload a new revision of a map
       Returns the updated map revision or raises an InvalidMapException on error
    """
    parser = settings.OPENRA_VERSIONS[0]
    if post.get("parser", None) is not None:
        if post['parser'] not in settings.OPENRA_VERSIONS:
            raise InvalidMapException('Invalid parser.')
        parser = post['parser']

    user = User.objects.get(pk=user_id)

    # Check whether we can upload a new revision
    # and propagate the license info
    policy = {
        'cc': False,
        'commercial': False,
        'adaptations': ''
    }

    if previous_revision:
        query = Maps.objects.filter(id=previous_revision, user_id=user.id)
        if not query:
            raise InvalidMapException('Map is owned by another user.')

        previous_item = query[0]
        if previous_item.next_rev != 0:
            raise InvalidMapException('Map already has a later revision.')

        policy['cc'] = previous_item.policy_cc
        policy['commercial'] = previous_item.policy_commercial
        policy['adaptations'] = previous_item.policy_adaptations
    else:
        if post['policy_cc'] == 'cc_yes':
            policy['cc'] = True
            if post['commercial'] == "com_yes":
                policy['commercial'] = True
            if post['adaptations'] == "adapt_yes":
                policy['adaptations'] = "yes"
            elif post['adaptations'] == "adapt_no":
                policy['adaptations'] = "no"
            else:
                policy['adaptations'] = "yes and shared alike"

    # Create a temporary working directory and copy the oramap to it
    # Directory is automatically deleted when exiting the context manager
    with tempfile.TemporaryDirectory() as working_path:
        print('Temporary working directory: {}'.format(working_path))

        # Django's get_valid_filename makes the name safe
        upload_path = os.path.join(working_path, get_valid_filename(file.name))
        upload_ext = os.path.splitext(upload_path)[1].lower()
        with open(upload_path, 'wb+') as destination:
            for chunk in file.chunks():
                destination.write(chunk)

        mime_retcode, mime_type = utility.detect_mimetype(upload_path)
        if mime_retcode != 0 or not (
                (mime_type == 'application/zip' and upload_ext == '.oramap') or
                (mime_type == 'text/plain' and upload_ext in ['.mpr', '.ini'])):
            raise InvalidMapException('Unsupported file type: ' + mime_type)

        # TODO: Support importing from cnc/d2k/ts.
        # Note that cnc maps contain *two* files which must both be present
        if mime_type == 'text/plain':
            import_path = os.path.splitext(upload_path)[0] + '.oramap'

            # Ignore command output and retcode, we know it worked if an oramap is saved
            utility.run_utility_command(parser, 'ra', [
                '--import-ra-map', upload_path], cwd=working_path)

            if os.path.exists(import_path):
                upload_path = import_path
            else:
                misc.send_email_to_admin_OnMapFail(upload_path)
                raise InvalidMapException('Failed to import legacy map.')

        # TODO: Replace hardcoded RA with the parser mod when that rewrite happens
        return add_map_revision(upload_path, user, parser, 'ra',
                                post['info'].strip(), policy, timezone.now(),
                                revision, previous_revision)
示例#6
0
    def ProcessUploading(self, user_id, f, post, rev=1, pre_r=0):

        parser_to_db = list(reversed(list(settings.OPENRA_VERSIONS.values())))[0]  # default parser = the latest
        parser = settings.OPENRA_ROOT_PATH + parser_to_db

        if post.get("parser", None) is not None:
            parser_to_db = post['parser']
            parser = settings.OPENRA_ROOT_PATH + parser_to_db
            if 'git' in parser:
                parser = settings.OPENRA_BLEED_PARSER

        if pre_r != 0:
            mapObject = Maps.objects.filter(id=pre_r, user_id=user_id)
            if not mapObject:
                return 'Failed. You do not own map for which you want to upload a new revision.'
            if mapObject[0].next_rev != 0:
                return 'Failed. Unable to upload a new revision for map which already has one.'
            previous_policy_cc = mapObject[0].policy_cc
            previous_policy_commercial = mapObject[0].policy_commercial
            previous_policy_adaptations = mapObject[0].policy_adaptations
        tempname = '/tmp/openramap.oramap'
        with open(tempname, 'wb+') as destination:
            for chunk in f.chunks():
                destination.write(chunk)

        command = 'file -b --mime-type %s' % tempname
        proc = Popen(command.split(), stdout=PIPE).communicate()
        mimetype = proc[0].decode().strip()
        if not (mimetype == 'application/zip' and os.path.splitext(f.name)[1].lower() == '.oramap'):
            if not (mimetype == 'text/plain' and os.path.splitext(f.name)[1].lower() in ['.mpr', '.ini']):
                return 'Failed. Unsupported file type.'

        name = f.name
        badChars = ": ; < > @ $ # & ( ) % '".split()
        for badchar in badChars:
            name = name.replace(badchar, "_")
        name = name.replace(" ", "_")
        # There can be weird chars still, if so: stop uploading
        findBadChars = re.findall(r'(\W+)', name)
        for bc in findBadChars:
            if bc not in ['.', '-']:
                return 'Failed. Your filename is bogus; rename and try again.'

        if mimetype == 'text/plain':
            if not self.LegacyImport(tempname, parser):
                misc.send_email_to_admin_OnMapFail(tempname)
                return 'Failed to import legacy map.'
            shutil.move(parser + "/" + self.legacy_name, tempname)
            name = os.path.splitext(name)[0] + '.oramap'
            self.legacy_map = True

        # Check if user has already uploaded the same map
        self.GetHash(tempname, parser)
        if 'Converted' in self.maphash and 'to MapFormat' in self.maphash:
            misc.send_email_to_admin_OnMapFail(tempname)
            return 'Failed to upload with this parser. MapFormat does not match. Try to upgrade your map or use different parser.'

        userObject = User.objects.get(pk=user_id)
        try:
            hashExists = Maps.objects.get(user_id=userObject.id, map_hash=self.maphash)
            self.UID = str(hashExists.id)
            return "Failed. You've already uploaded this map."
        except:
            pass   # all good

        # Read Yaml
        read_yaml_response = utility.ReadYaml(False, tempname)
        resp_map_data = read_yaml_response['response']
        if read_yaml_response['error']:
            misc.send_email_to_admin_OnMapFail(tempname)
            return resp_map_data

        # Read Rules
        base64_rules = {}
        base64_rules['data'] = ''
        base64_rules['advanced'] = resp_map_data['advanced']
        if int(resp_map_data['mapformat']) >= 10:
            base64_rules = utility.ReadRules(False, tempname, parser, resp_map_data['game_mod'])
            if (base64_rules['error']):
                print(base64_rules['response'])
        if base64_rules['advanced']:
            resp_map_data['advanced'] = True

        # Define license information
        cc = False
        commercial = False
        adaptations = ""
        if pre_r == 0:
            if post['policy_cc'] == 'cc_yes':
                cc = True
                if post['commercial'] == "com_yes":
                    commercial = True
                if post['adaptations'] == "adapt_yes":
                    adaptations = "yes"
                elif post['adaptations'] == "adapt_no":
                    adaptations = "no"
                else:
                    adaptations = "yes and shared alike"
        else:
            cc = previous_policy_cc
            commercial = previous_policy_commercial
            adaptations = previous_policy_adaptations


        # Add record to Database
        transac = Maps(
            user=userObject,
            title=resp_map_data['title'],
            description=resp_map_data['description'],
            info=post['info'].strip(),
            author=resp_map_data['author'],
            map_type=resp_map_data['map_type'],
            categories=resp_map_data['categories'],
            players=resp_map_data['players'],
            game_mod=resp_map_data['game_mod'],
            map_hash=self.maphash.strip(),
            width=resp_map_data['width'],
            height=resp_map_data['height'],
            bounds=resp_map_data['bounds'],
            mapformat=resp_map_data['mapformat'],
            spawnpoints=resp_map_data['spawnpoints'],
            tileset=resp_map_data['tileset'],
            shellmap=resp_map_data['shellmap'],
            base64_rules=base64_rules['data'],
            base64_players=resp_map_data['base64_players'],
            legacy_map=self.legacy_map,
            revision=rev,
            pre_rev=pre_r,
            next_rev=0,
            downloading=True,
            requires_upgrade=True,
            advanced_map=resp_map_data['advanced'],
            lua=resp_map_data['lua'],
            posted=timezone.now(),
            viewed=0,
            policy_cc=cc,
            policy_commercial=commercial,
            policy_adaptations=adaptations,
            parser=parser_to_db,
        )
        transac.save()
        self.UID = str(transac.id)

        self.map_full_path_directory = self.currentDirectory + __name__.split('.')[0] + '/data/maps/' + self.UID + '/'

        try:
            if not os.path.exists(self.map_full_path_directory):
                os.makedirs(self.map_full_path_directory + 'content')
        except Exception as e:
            print("Failed to create directory for new map", self.map_full_path_directory)
            transac.delete() # Remove failed map from DB before raise
            raise

        if pre_r != 0:
            Maps.objects.filter(id=pre_r).update(next_rev=transac.id)

        self.map_full_path_filename = self.map_full_path_directory + name
        self.preview_filename = os.path.splitext(name)[0] + ".png"

        shutil.move(tempname, self.map_full_path_filename)

        self.UnzipMap()

        lint_check_response = utility.LintCheck(transac, self.map_full_path_filename, parser)
        if lint_check_response['error'] is False and lint_check_response['response'] == 'pass_for_requested_parser':
            self.LintPassed = True

        if self.LintPassed:
            Maps.objects.filter(id=transac.id).update(requires_upgrade=False)
        else:
            Maps.objects.filter(id=transac.id).update(requires_upgrade=True)

        if int(resp_map_data['mapformat']) < 10:
            self.GenerateMinimap(resp_map_data['game_mod'], parser)

        #shp = multiprocessing.Process(target=self.GenerateSHPpreview, args=(resp_map_data['game_mod'], parser,), name='shppreview')
        #shp.start()
        print("--- New map: %s" % self.UID)
        return False  # no errors