<commit_before>from datetime import datetime
import pytz

from django.utils.translation import ugettext_lazy as _
from django.db import models
from django.contrib.auth.models import User
from django.contrib import admin
from django.core.urlresolvers import reverse

from gonzo.utils import slugify
from gonzo.hunt import utils
from gonzo.hunt.thumbs import ImageWithThumbsField

def to_json_time(d):
    return d.replace(tzinfo=pytz.utc).isoformat()


class Hunt(models.Model):
    """
    The model for a hunt.

    start_time is inclusive, but end_time and vote_end_time are exclusive
    """
    # Owner of the hunt
    owner            = models.ForeignKey(User)
    # Three word phrase (crazy model submarine)
    phrase           = models.CharField(max_length=128)
    # The hunt slug (based on the phrase)
    slug             = models.SlugField(unique=True,max_length=200)
    # The hunt tag (to be used as hashtag)
    tag              = models.CharField(max_length=64)
    start_time       = models.DateTimeField()
    end_time         = models.DateTimeField()
    vote_end_time    = models.DateTimeField()
    max_submissions  = models.PositiveIntegerField(null=True,blank=True)

    def clean(self):
        from django.core.exceptions import ValidationError
        if self.start_time >= self.end_time:
            raise ValidationError("Start time must be earlier than end time")
        if self.end_time > self.vote_end_time:
            raise ValidationError("End time must be earlier (or equal to) end time")
        super(Hunt,self).clean()

    def __unicode__(self):
        return self.slug

    class Meta:
        ordering = ["-start_time"]

    def save(self, **kwargs):
        self.clean()
        self.slug = slugify(Hunt, self.phrase, exclude_pk=self.id)
        return super(Hunt,self).save(**kwargs)

    class State:
        FUTURE = "FUTURE"
        CURRENT = "CURRENT"
        VOTING = "VOTING"
        FINISHED = "FINISHED"

    def get_state(self):
        """
        Returns one of the following strings: FUTURE, CURRENT, VOTING, FINISHED
        For hunts where end_time == vote_end_time, there is no "VOTING" state.
        """
        now = datetime.utcnow()
        # Note start time is inclusive, but end time is exclusive.
        if now >= self.vote_end_time:
            return Hunt.State.FINISHED
        elif now >= self.end_time:
            return Hunt.State.VOTING
        elif now >= self.start_time:
            return Hunt.State.CURRENT
        else:
            return Hunt.State.FUTURE

    def _get_url(self,viewname):
        return (viewname, (), { 'slug' : self.slug })

    @models.permalink
    def get_absolute_url(self):
        return self._get_url('hunt')
    @models.permalink
    def get_api_url(self):
        return self._get_url('api-hunt')
    @models.permalink
    def get_ballot_url(self):
        return self._get_url('api-hunt-ballot')
    @models.permalink
    def get_submission_url(self):
        return self._get_url('api-photo-index')
    @models.permalink
    def get_comments_url(self):
        return self._get_url('api-hunt-comment-index')

    def get_thumbnail_url(self):
        try:
            return self.submission_set.filter(is_removed=False)[0].photo.url_240x180
        except IndexError:
            return None

    def to_dict(self,request):
        json = { 'owner': self.owner.username,
                'phrase': self.phrase,
                'slug': self.slug,
                'tag': self.tag,
                'start_time': to_json_time(self.start_time),
                'end_time': to_json_time(self.end_time),
                'vote_end_time': to_json_time(self.vote_end_time),
                'url': request.build_absolute_uri(self.get_absolute_url()),
                'submissions': request.build_absolute_uri(self.get_submission_url()),
                'ballot': request.build_absolute_uri(self.get_ballot_url()),
                'comments': request.build_absolute_uri(self.get_comments_url()) }
        thumb = self.get_thumbnail_url()
        if thumb:
            json['thumbnail'] = request.build_absolute_uri(thumb)
        return json

class Submission(models.Model):
    hunt            = models.ForeignKey(Hunt)
    time            = models.DateTimeField(default=datetime.utcnow)
    # The URL to the photo
    photo           = ImageWithThumbsField(upload_to="photos",
                                        max_length=256,
                                        height_field='photo_height',
                                        width_field='photo_width',
                                        sizes=((240,180),))
    photo_width     = models.IntegerField()
    photo_height    = models.IntegerField()

    # Location of submission
    latitude        = models.FloatField(null=True,blank=True)
    longitude       = models.FloatField(null=True,blank=True)

    # Null means this comes from an unauthenticated user
    user            = models.ForeignKey(User,null=True)
    # For anonymous entries, this will keep track of from where we found it.
    # This takes a URL form like twitter:<username> or facebook:<profile_id>
    anon_source     = models.CharField(max_length=64,null=True,blank=True)
    ip_address      = models.IPAddressField(blank=True)
    # Where this was submitted, e.g., "Web" "Twitter", "1hph for Android"
    via             = models.CharField(max_length=32)
    is_removed      = models.BooleanField(default=False)

    class Meta:
        ordering = ["-time"]

    def __unicode__(self):<commit_msg>Save some comments for later<commit_after>from datetime import datetime
import pytz

from django.utils.translation import ugettext_lazy as _
from django.db import models
from django.contrib.auth.models import User
from django.contrib import admin
from django.core.urlresolvers import reverse

from gonzo.utils import slugify
from gonzo.hunt import utils
from gonzo.hunt.thumbs import ImageWithThumbsField

def to_json_time(d):
    return d.replace(tzinfo=pytz.utc).isoformat()


class Hunt(models.Model):
    """
    The model for a hunt.

    start_time is inclusive, but end_time and vote_end_time are exclusive
    """
    # Owner of the hunt
    owner            = models.ForeignKey(User)
    # Three word phrase (crazy model submarine)
    phrase           = models.CharField(max_length=128)
    # The hunt slug (based on the phrase)
    slug             = models.SlugField(unique=True,max_length=200)
    # The hunt tag (to be used as hashtag)
    tag              = models.CharField(max_length=64)
    start_time       = models.DateTimeField()
    end_time         = models.DateTimeField()
    vote_end_time    = models.DateTimeField()
    max_submissions  = models.PositiveIntegerField(null=True,blank=True)

    def clean(self):
        from django.core.exceptions import ValidationError
        if self.start_time >= self.end_time:
            raise ValidationError("Start time must be earlier than end time")
        if self.end_time > self.vote_end_time:
            raise ValidationError("End time must be earlier (or equal to) end time")
        super(Hunt,self).clean()

    def __unicode__(self):
        return self.slug

    class Meta:
        ordering = ["-start_time"]

    def save(self, **kwargs):
        self.clean()
        self.slug = slugify(Hunt, self.phrase, exclude_pk=self.id)
        return super(Hunt,self).save(**kwargs)

    class State:
        FUTURE = "FUTURE"
        CURRENT = "CURRENT"
        VOTING = "VOTING"
        FINISHED = "FINISHED"

    def get_state(self):
        """
        Returns one of the following strings: FUTURE, CURRENT, VOTING, FINISHED
        For hunts where end_time == vote_end_time, there is no "VOTING" state.
        """
        now = datetime.utcnow()
        # Note start time is inclusive, but end time is exclusive.
        if now >= self.vote_end_time:
            return Hunt.State.FINISHED
        elif now >= self.end_time:
            return Hunt.State.VOTING
        elif now >= self.start_time:
            return Hunt.State.CURRENT
        else:
            return Hunt.State.FUTURE

    def _get_url(self,viewname):
        return (viewname, (), { 'slug' : self.slug })

    @models.permalink
    def get_absolute_url(self):
        return self._get_url('hunt')
    @models.permalink
    def get_api_url(self):
        return self._get_url('api-hunt')
    @models.permalink
    def get_ballot_url(self):
        return self._get_url('api-hunt-ballot')
    @models.permalink
    def get_submission_url(self):
        return self._get_url('api-photo-index')
    @models.permalink
    def get_comments_url(self):
        return self._get_url('api-hunt-comment-index')

    def get_thumbnail_url(self):
        try:
            return self.submission_set.filter(is_removed=False)[0].photo.url_240x180
        except IndexError:
            return None

    def to_dict(self,request):
        json = { 'owner': self.owner.username,
                'phrase': self.phrase,
                'slug': self.slug,
                'tag': self.tag,
                'start_time': to_json_time(self.start_time),
                'end_time': to_json_time(self.end_time),
                'vote_end_time': to_json_time(self.vote_end_time),
                'url': request.build_absolute_uri(self.get_absolute_url()),
                'submissions': request.build_absolute_uri(self.get_submission_url()),
                'ballot': request.build_absolute_uri(self.get_ballot_url()),
                'comments': request.build_absolute_uri(self.get_comments_url()) }
        thumb = self.get_thumbnail_url()
        if thumb:
            json['thumbnail'] = request.build_absolute_uri(thumb)
        return json

class Submission(models.Model):
    hunt            = models.ForeignKey(Hunt)
    time            = models.DateTimeField(default=datetime.utcnow)
    # The URL to the photo
    photo           = ImageWithThumbsField(upload_to="photos",
                                        max_length=256,
                                        height_field='photo_height',
                                        width_field='photo_width',
                                        sizes=((240,180),))
    photo_width     = models.IntegerField()
    photo_height    = models.IntegerField()

    # Location of submission
    latitude        = models.FloatField(null=True,blank=True)
    longitude       = models.FloatField(null=True,blank=True)

    # Null means this comes from an unauthenticated user
    user            = models.ForeignKey(User,null=True)
    # For anonymous entries, this will keep track of from where we found it.
    # This takes a URL form like twitter:<username> or facebook:<profile_id>
    anon_source     = models.CharField(max_length=64,null=True,blank=True)
    ip_address      = models.IPAddressField(blank=True)
    # Where this was submitted, e.g., "Web" "Twitter", "1hph for Android"
    via             = models.CharField(max_length=32)
    is_removed      = models.BooleanField(default=False)
    # TODO: To add when we get around to updating the model:
    #remove_reason = models.CharField(max_length=32)
    #description   = models.TextField()
    # for right now, we will generate 5 character paths.
    #shortened_path = models.CharField(unique=True,max_length=6,default=shorten_id)
    # The MD5 sum will be used to see if there are any duplicates in the hunt
    #md5sum        = models.CharField(max_length=32)

    class Meta:
        ordering = ["-time"]

    def __unicode__(self):
{'fraction_non_alphanumeric': 0.09490760822416934, 'fraction_numerical': 0.0057256875162661576, 'mean_word_length': 2.3846153846153846, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3907591', 'n_tokens_mistral': 3475, 'n_tokens_neox': 3339, 'n_words': 787}