def test_uuid_casting(self):
     # As explain by postgres documentation
     # http://www.postgresql.org/docs/9.1/static/datatype-uuid.html
     # an uuid needs to be a sequence of lower-case hexadecimal digits, in
     # several groups separated by hyphens, specifically a group of 8 digits
     # followed by three groups of 4 digits followed by a group of 12 digits
     matcher = re.compile("^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}" "-[\da-f]{12}$")
     field = PostgreSQLUUIDField()
     for value in (str(uuid.uuid4()), uuid.uuid4().urn, uuid.uuid4().hex, uuid.uuid4().int, uuid.uuid4().bytes):
         prepared_value = field.get_db_prep_value(value, None)
         self.assertTrue(matcher.match(prepared_value) is not None, prepared_value)
Exemple #2
0
 def test_uuid_casting(self):
     # As explain by postgres documentation
     # http://www.postgresql.org/docs/9.1/static/datatype-uuid.html
     # an uuid needs to be a sequence of lower-case hexadecimal digits, in
     # several groups separated by hyphens, specifically a group of 8 digits
     # followed by three groups of 4 digits followed by a group of 12 digits
     matcher = re.compile('^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}'
                          '-[\da-f]{12}$')
     field = PostgreSQLUUIDField()
     for value in (str(uuid.uuid4()), uuid.uuid4().urn, uuid.uuid4().hex,
                   uuid.uuid4().int, uuid.uuid4().bytes):
         prepared_value = field.get_db_prep_value(value, None)
         self.assertTrue(matcher.match(prepared_value) is not None,
                         prepared_value)
class ChatMessage(models.Model):
    id = PostgreSQLUUIDField(primary_key=True, auto=True)
    order = models.BigIntegerField()
    document = models.ForeignKey(Document, related_name='messages')
    issuer = models.OneToOneField(ChatMessageIssuer)
    message = models.CharField(max_length=200)
    date = models.DateTimeField(default=datetime.now)
class Podcast(Importable):
    id = PostgreSQLUUIDField(primary_key=True, auto=True)
    creator = models.ForeignKey(settings.AUTH_USER_MODEL,
                                null=True,
                                blank=True)
    title = models.CharField(max_length=150)
    cover = models.ForeignKey(Cover,
                              related_name="podcasts",
                              null=True,
                              blank=True)
    description = models.TextField()
    url = models.URLField()
    feed = models.URLField(null=True, blank=True)
    stream = models.CharField(max_length=100, null=True, blank=True)
    chat = models.CharField(max_length=100, null=True, blank=True)
    type = models.CharField(max_length=3, choices=TYPE_CHOICES)
    deleted = models.BooleanField(default=False)
    approved = models.BooleanField(default=False)
    create_date = models.DateTimeField(default=datetime.now)
    mums = models.ManyToManyField(settings.AUTH_USER_MODEL,
                                  related_name="mum_podcasts")

    def __str__(self):
        return "{} ({})".format(self.title, self.slug)

    @property
    def slug(self):
        try:
            return str(self.slugs.latest())
        except PodcastSlug.DoesNotExist:
            return None

    @property
    def num_episodes(self):
        return self.episodes.count()
class PubBase(models.Model):
    id = PostgreSQLUUIDField(primary_key=True, auto=True)
    create_date = models.DateTimeField(auto_now_add=True)
    comment = models.CharField(max_length=250, blank=True, null=True)

    class Meta:
        abstract = True
class Podcaster(models.Model):
    id = PostgreSQLUUIDField(primary_key=True, auto=True)
    uri = models.URLField(unique=True, db_index=True)
    name = models.CharField(max_length=30)

    def __str__(self):
        return self.name
Exemple #7
0
class EntityBase(TimeStampedModel):
    class Meta(TimeStampedModel.Meta):
        # Cancel the ordering specified in TimeStampedModel because
        # that would incur a performance burden on all Entity queries.
        ordering = []
        abstract = True

    # We can use this as a path to this entity's media files
    uuid = PostgreSQLUUIDField(unique=True)

    # Public URL slug
    slug = AutoSlugField(populate_from='title')

    title = models.CharField(max_length=200)
    brief = models.CharField(max_length=200, blank=True)
    about = models.TextField(blank=True)
    website = models.URLField(blank=True)

    # Image to be displayed in place of the Lady
    picture = models.ImageField(blank=True, upload_to=entityPicturePath)

    # Whether this profile can be viewed by anyone but its editors
    public = models.BooleanField(default=False)

    # Users who manage this entity's profile (i.e. can edit it)
    editors = models.ManyToManyField(User, blank=True)

    # Users who get updates about this entity
    fans = models.ManyToManyField(
        User, related_name='%(class)sFaves', blank=True
    )

    def __str__(self):
        return self.title
class DocumentMeta(models.Model):
    id = PostgreSQLUUIDField(primary_key=True, auto=True)
    shownoters = models.ManyToManyField(Shownoter, blank=True)

    def __str__(self):
        try:
            return "Meta for " + self.document.__str__()
        except:
            return "Meta without document?"
Exemple #9
0
class Ticket(models.Model):
    event = models.ForeignKey(Event, default=get_current_event)
    sale = models.ForeignKey(Sale, null=True)
    type = models.ForeignKey(TicketType)

    barcode = PostgreSQLUUIDField(db_index=True)

    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
    email = models.EmailField(db_index=True)

    title = models.CharField(max_length=255, blank=True)
    organization = models.CharField(max_length=255, blank=True)
    twitter = models.CharField(max_length=127, verbose_name="Twitter Handle", blank=True)
    website = models.CharField(max_length=255, blank=True)

    diet_vegetarian = models.BooleanField(default=False, verbose_name="Vegetarian")
    diet_vegan = models.BooleanField(default=False, verbose_name="Vegan")
    diet_gluten_free = models.BooleanField(default=False, verbose_name="Gluten-free")
    diet_allergies = models.BooleanField(default=False, verbose_name="Allergies")
    diet_allergies_desc = models.TextField(blank=True, verbose_name="Description (allergies)")
    diet_other = models.BooleanField(default=False, verbose_name="Other dietary needs")
    diet_other_desc = models.TextField(blank=True, verbose_name="Description (other dietary needs)")

    attend_day1 = models.BooleanField(default=True, verbose_name="Friday")
    attend_day2 = models.BooleanField(default=True, verbose_name="Saturday")

    lobby_day = models.BooleanField(default=False, verbose_name="Do you plan to attend the Sunlight Network's Lobby Day?")
    ambassador_program = models.CharField(default="no", choices=AMBASSADOR_PROGRAM_CHOICES, max_length=12, verbose_name="Would you like to be part of the TCamp Ambassador Program?")

    subscribe = models.BooleanField(default=False, verbose_name="Please subscribe me to emails from the Sunlight Foundation")

    success = models.BooleanField(default=False, db_index=True)

    checked_in = models.DateTimeField(null=True, default=None)

    objects = TicketManager()

    @property
    def clean_twitter(self):
        if self.twitter and self.twitter.startswith("@"):
            return self.twitter[1:]
        else:
            return self.twitter

    @property
    def short_barcode(self):
        qrcode_uuid = uuid.UUID(self.barcode)
        return shortuuid.encode(qrcode_uuid)

    def __unicode__(self):
        return u" ".join((self.first_name, self.last_name))

    def __str__(self):
        return u" ".join((self.first_name, self.last_name))
Exemple #10
0
class Cover(models.Model):
    id = PostgreSQLUUIDField(primary_key=True, auto=True)
    file = models.ImageField(upload_to=f)
    original_url = models.URLField()
    creator = models.ForeignKey(settings.AUTH_USER_MODEL,
                                null=True,
                                blank=True)
    create_date = models.DateTimeField()

    @staticmethod
    def from_url(creator, url):
        try:
            return Cover.objects.get(original_url=url)
        except Cover.DoesNotExist:
            pass

        img_temp = None

        try:
            cover = Cover(creator=creator,
                          create_date=datetime.now(),
                          original_url=url)

            response = urllib.request.urlopen(url)

            if 'content-length' not in response.headers or int(
                    response.headers['content-length']) > 1000000:
                return None

            data = response.read()

            Image.open(BytesIO(data)).verify()

            img = Image.open(BytesIO(data))
            img = img.resize((150, 150), Image.ANTIALIAS)

            img_temp = NamedTemporaryFile(delete=True)
            ext = url.split('.')[-1].upper()
            if ext == 'JPG':
                ext = 'JPEG'
            img.save(img_temp, format=ext)

            cover.file.save(f(None, url), File(img_temp), save=True)

            return cover
        except:
            return None
        finally:
            if img_temp:
                img_temp.close()

    def __str__(self):
        return "{} ".format(self.original_url or self.file)
Exemple #11
0
class Episode(Importable):
    id = PostgreSQLUUIDField(primary_key=True, auto=True)
    title = models.CharField(max_length=200, null=True, blank=True)
    podcast = models.ForeignKey(Podcast, related_name="episodes")
    cover = models.ForeignKey(Cover,
                              related_name="episodes",
                              null=True,
                              blank=True)
    creator = models.ForeignKey(settings.AUTH_USER_MODEL,
                                null=True,
                                blank=True)
    number = models.CharField(max_length=10, null=True, blank=True)
    episode_url = models.URLField(verbose_name="Episode URL",
                                  null=True,
                                  blank=True)
    date = models.DateTimeField(null=True, blank=True)
    canceled = models.BooleanField(default=False)
    type = models.CharField(max_length=100, choices=TYPE_CHOICES)
    create_date = models.DateTimeField(default=datetime.now)
    stream = models.CharField(max_length=100, null=True, blank=True)
    document = models.OneToOneField(Document,
                                    null=True,
                                    blank=True,
                                    unique=True,
                                    on_delete=models.SET_NULL)

    class Meta:
        unique_together = ('podcast', 'number')
        permissions = (("publish_episode", "publish"), )

    def __str__(self):
        return "{}-{} ({})".format(self.podcast.slug, self.number
                                   or 'NoNumberYet', self.date)

    def save(self, *args, **kwargs):
        if not self.number:
            self.number = None
        super(Episode, self).save(*args, **kwargs)
class DocumentState(models.Model):
    id = PostgreSQLUUIDField(primary_key=True, auto=True)
    date = models.DateTimeField(default=datetime.now)

    def __str__(self):
        return self.id