<commit_before>from django.db import models
from django.dispatch import receiver
from django.conf import settings
from django.contrib.gis.gdal import DataSource
from django.template.defaultfilters import slugify
import zipfile
import os, errno
import glob

class Layer(models.Model):
    name = models.CharField(max_length=255)
    description = models.TextField(null=True, blank=True)
    slug = models.SlugField(editable=False)
    bbox = models.CharField(max_length=255, null=True, blank=True)
    original = models.FileField(upload_to='uploads', null=True, blank=True,
                                help_text="""Zip file with either geotiff and
                                        projection or shapefiles and friends""")
#    type = models.CharField(max_length=255)
    style = models.TextField(null=True, blank=True)

    def __unicode__(self):
        return self.name

    def save(self, force_insert=False, force_update=False):
        self.slug = slugify(self.name)
        super(Layer, self).save(force_insert, force_update)

def create_folder(path):
    try:
        os.makedirs(path)
    except OSError as exc: # Python >2.5
        if exc.errno == errno.EEXIST and os.path.isdir(path):
            pass
        else: raise

@receiver(models.signals.pre_save, sender=Layer)
def layer_handler(sender, instance, *args, **kwargs):
    """
    Post process the uploaded layer
    Get the bounding box information and save it with the model
    """

    instance.slug = slugify(instance.name)

    # Make a folder with the slug name
    # and create a 'raw' subdirectory to hold the files
    layer_folder = os.path.join(settings.MEDIA_ROOT, 'layers', instance.slug)
    create_folder(layer_folder)
    zip_out = os.path.join(layer_folder, 'raw')<commit_msg>Replaces the old zipfile in /media/uploads with the newly uploaded file with the same filename.<commit_after>from django.core.files.storage import FileSystemStorage
from django.db import models
from django.dispatch import receiver
from django.conf import settings
from django.contrib.gis.gdal import DataSource
from django.template.defaultfilters import slugify
import zipfile
import os, errno
import glob

class OverwriteStorage(FileSystemStorage):

    def get_available_name(self, name):
        if self.exists(name):
            os.remove(os.path.join(settings.MEDIA_ROOT, name))
        return name

class Layer(models.Model):
    name = models.CharField(max_length=255)
    description = models.TextField(null=True, blank=True)
    slug = models.SlugField(editable=False)
    bbox = models.CharField(max_length=255, null=True, blank=True)
    original = models.FileField(storage=OverwriteStorage(),
                                upload_to='uploads', null=True, blank=True,
                                help_text="""Zip file with either geotiff and
                                        projection or shapefiles and friends""")
    style = models.TextField(null=True, blank=True)
    # type = models.CharField(max_length=255)

    def __unicode__(self):
        return self.name

    def save(self, force_insert=False, force_update=False):
        self.slug = slugify(self.name)
        super(Layer, self).save(force_insert, force_update)

def create_folder(path):
    try:
        os.makedirs(path)
    except OSError as exc: # Python >2.5
        if exc.errno == errno.EEXIST and os.path.isdir(path):
            pass
        else: raise

@receiver(models.signals.pre_save, sender=Layer)
def layer_handler(sender, instance, *args, **kwargs):
    """
    Post process the uploaded layer
    Get the bounding box information and save it with the model
    """

    instance.slug = slugify(instance.name)

    # Make a folder with the slug name
    # and create a 'raw' subdirectory to hold the files
    layer_folder = os.path.join(settings.MEDIA_ROOT, 'layers', instance.slug)
    create_folder(layer_folder)
    zip_out = os.path.join(layer_folder, 'raw')
{'fraction_non_alphanumeric': 0.09388480081197666, 'fraction_numerical': 0.005582339507739152, 'mean_word_length': 3.198083067092652, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '757765', 'n_tokens_mistral': 1179, 'n_tokens_neox': 1127, 'n_words': 309}