<commit_before>from django.db import models
from django.contrib.auth.models import User
import md5

class SurveyDraft(models.Model):
    '''
    SurveyDrafts belong to a user and contain the minimal representation of
    the draft survey of the user and of the question library.
    '''
    user = models.ForeignKey(User, related_name="survey_drafts")
    name = models.CharField(max_length=255, null=False)
    body = models.TextField()
    description = models.CharField(max_length=255, null=True)
    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)
    in_question_library = models.BooleanField(default=False)

    @property
    def _pyxform_survey(self):
        import pyxform_utils
        survey = pyxform_utils.create_survey_from_csv_text(self.body)
        survey.title = self.name
        return survey

    @property
    def id_string(self):
        # Ideally, we could determine this without needing to load the entire
        # survey into pyxform, but parsing csvs from unk sources can be complicated
        # and this method of finding the id_string is (at least) consistent.
        return self._pyxform_survey.id_string

    def to_xml(self):
        return self._pyxform_survey.to_xml()

    def to_xls(self):
        import pyxform_utils
        return pyxform_utils.convert_csv_to_xls(self.body)

class SurveyPreview(models.Model):
    unique_string = models.CharField(max_length=64, null=False, unique=True)
    csv = models.TextField()
    xml = models.TextField()
    date_created = models.DateTimeField(auto_now_add=True)

    @classmethod
    def _generate_unique_string(kls, csv):
        return md5.new("csv=%s" % csv).hexdigest()

    @classmethod
    def _get_or_create(kls, *args, **kwargs):
        csv = kwargs.get('csv')
        kwargs[u'unique_string'] = kls._generate_unique_string(csv)
        try:
            return kls.objects.get(unique_string=kwargs[u'unique_string'])
        except kls.DoesNotExist, e:
            new_preview = kls(**kwargs)
            new_preview.save()
            return new_preview

    def save(self, *args, **kwargs):
        if self.unique_string in [u'', None]:
            self.unique_string = SurveyPreview._generate_unique_string(self.csv)
        if self.xml in [u'', None]:
            self.xml = create_survey_from_csv_text(self.csv, default_name="SurveyPreview__save").to_xml()
        super(SurveyPreview, self).save(*args, **kwargs)
<commit_msg>Fixes error when generating survey_preview<commit_after>from django.db import models
from django.contrib.auth.models import User
import md5

class SurveyDraft(models.Model):
    '''
    SurveyDrafts belong to a user and contain the minimal representation of
    the draft survey of the user and of the question library.
    '''
    user = models.ForeignKey(User, related_name="survey_drafts")
    name = models.CharField(max_length=255, null=False)
    body = models.TextField()
    description = models.CharField(max_length=255, null=True)
    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)
    in_question_library = models.BooleanField(default=False)

    @property
    def _pyxform_survey(self):
        import pyxform_utils
        survey = pyxform_utils.create_survey_from_csv_text(self.body)
        survey.title = self.name
        return survey

    @property
    def id_string(self):
        # Ideally, we could determine this without needing to load the entire
        # survey into pyxform, but parsing csvs from unk sources can be complicated
        # and this method of finding the id_string is (at least) consistent.
        return self._pyxform_survey.id_string

    def to_xml(self):
        return self._pyxform_survey.to_xml()

    def to_xls(self):
        import pyxform_utils
        return pyxform_utils.convert_csv_to_xls(self.body)

class SurveyPreview(models.Model):
    unique_string = models.CharField(max_length=64, null=False, unique=True)
    csv = models.TextField()
    xml = models.TextField()
    date_created = models.DateTimeField(auto_now_add=True)

    @classmethod
    def _generate_unique_string(kls, csv):
        return md5.new("csv=%s" % csv).hexdigest()

    @classmethod
    def _get_or_create(kls, *args, **kwargs):
        csv = kwargs.get('csv')
        kwargs[u'unique_string'] = kls._generate_unique_string(csv)
        try:
            return kls.objects.get(unique_string=kwargs[u'unique_string'])
        except kls.DoesNotExist, e:
            new_preview = kls(**kwargs)
            new_preview.save()
            return new_preview

    def save(self, *args, **kwargs):
        if self.unique_string in [u'', None]:
            self.unique_string = SurveyPreview._generate_unique_string(self.csv)
        if self.xml in [u'', None]:
            import pyxform_utils
            self.xml = pyxform_utils.create_survey_from_csv_text(self.csv, default_name="SurveyPreview__save").to_xml()
        super(SurveyPreview, self).save(*args, **kwargs)

{'fraction_non_alphanumeric': 0.09323040380047506, 'fraction_numerical': 0.00395882818685669, 'mean_word_length': 3.289473684210526, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '737858', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1527, 'n_words': 350}