<commit_before>import datetime
import re
import urllib
import urllib2
import Image
import StringIO

from django.db import models
from django.contrib import admin
from django.contrib.sites.models import Site
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
from django.forms.fields import slug_re
from django.template import mark_safe

import feedparser
import vidscraper


# the difference between unapproved and rejected is that unapproved simply
# hasn't been looked at by an administrator yet.
VIDEO_STATUS_UNAPPROVED = FEED_STATUS_UNAPPROVED =0
VIDEO_STATUS_ACTIVE = FEED_STATUS_ACTIVE = 1
VIDEO_STATUS_REJECTED = FEED_STATUS_REJECTED = 2

VIDEO_STATUSES = FEED_STATUSES = (
    (VIDEO_STATUS_UNAPPROVED, 'Unapproved'),
    (VIDEO_STATUS_ACTIVE, 'Active'),
    (VIDEO_STATUS_REJECTED, 'Rejected'))

SITE_STATUS_DISABLED = 0
SITE_STATUS_ACTIVE = 1

SITE_STATUSES = (
    (SITE_STATUS_DISABLED, 'Disabled'),
    (SITE_STATUS_ACTIVE, 'Active'))

VIDEO_THUMB_SIZES = [
    (375, 295), # featured on frontpage
    (140, 110)]

VIDEO_SERVICE_REGEXES = (
    ('YouTube', r'http://gdata\.youtube\.com/feeds/base/videos/-/'),
    ('YouTube', r'http://(www\.)?youtube\.com/'),
    ('blip.tv', r'http://(.+\.)?blip\.tv/'),
    ('Vimeo', r'http://(www\.)?vimeo\.com/'))

class Error(Exception): pass
class CannotOpenImageUrl(Error): pass


class OpenIdUser(models.Model):
    """
    Custom openid user authentication model.  Presently does not match
    up to Django's contrib.auth.models.User model, probably should be
    adjusted to do so eventually.

    Login and registration functionality provided in localtv.openid
    and its submodules.

    Fields:
      - url: URL that this user is identified by
      - user: the Django User object that this is a valid login for
    """
    user = models.OneToOneField('auth.User')
    url = models.URLField(verify_exists=False, unique=True)

    def __unicode__(self):
        return "%s <%s>" % (self.user.username, self.user.email)


class SiteLocation(models.Model):
    """
    An extension to the django.contrib.sites site model, providing
    localtv-specific data.

    Fields:
     - site: A link to the django.contrib.sites.models.Site object
     - logo: custom logo image for this site
     - background: custom background image for this site (unused?)
     - admins: a collection of Users who have access to administrate this
       sitelocation
     - status: one of SITE_STATUSES; either disabled or active
     - sidebar_html: custom html to appear on the right sidebar of many
       user-facing pages.  Can be whatever's most appropriate for the owners of
       said site.
     - footer_html: HTML that appears at the bottom of most user-facing pages.
       Can be whatever's most appropriate for the owners of said site.
     - about_html: HTML to display on the subsite's about page
     - tagline: displays below the subsite's title on most user-facing pages
     - css: The intention here is to allow subsites to paste in their own CSS
       here from the admin.  Not used presently, though eventually it should
       be.
     - frontpage_style: The style of the frontpage.  Either one of:
        * list
        * scrolling
        * categorized
     - display_submit_button: whether or not we should allow users to see that
       they can submit videos or not (doesn't affect whether or not they
       actually can though)
     - submission_requires_login: whether or not users need to log in to submit
       videos.
    """
    site = models.ForeignKey(Site, unique=True)
    logo = models.ImageField(upload_to='localtv/site_logos', blank=True)
    background = models.ImageField(upload_to='localtv/site_backgrounds',
                                   blank=True)
    admins = models.ManyToManyField('auth.User', blank=True,
                                    related_name='admin_for')
    status = models.IntegerField(
        choices=SITE_STATUSES, default=SITE_STATUS_ACTIVE)
    sidebar_html = models.TextField(blank=True)
    footer_html = models.TextField(blank=True)
    about_html = models.TextField(blank=True)
    tagline = models.CharField(max_length=250, blank=True)
    css = models.TextField(blank=True)
    frontpage_style = models.CharField(max_length=32, default="list")
    display_submit_button = models.BooleanField(default=True)
    submission_requires_login = models.BooleanField(default=False)

    def __unicode__(self):
        return self.site.name


    def user_is_admin(self, user):
        """
        Return True if the given User is an admin for this SiteLocation.
        """
        if not user.is_authenticated() or not user.is_active:
            return False

        if user.is_superuser:
            return True

        for sitelocation in user.admin_for.all():
            if self == sitelocation:
                return True

        return False
    
class Tag(models.Model):
    """
    Tags for videos.

    Presently apply to all sitelocations.  Maybe eventually only certain tags
    should apply to certain sitelocations?

    Fields:
      - name: name of this tag
    """
    name = models.CharField(max_length=255)

    def __unicode__(self):
        return self.name


class Feed(models.Model):
    """
    Feed to pull videos in from.

    If the same feed is used on two different subsites, they will require two
    separate entries here.
<commit_msg>simplying checking if a user is an admin<commit_after>import datetime
import re
import urllib
import urllib2
import Image
import StringIO

from django.db import models
from django.contrib import admin
from django.contrib.sites.models import Site
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
from django.forms.fields import slug_re
from django.template import mark_safe

import feedparser
import vidscraper


# the difference between unapproved and rejected is that unapproved simply
# hasn't been looked at by an administrator yet.
VIDEO_STATUS_UNAPPROVED = FEED_STATUS_UNAPPROVED =0
VIDEO_STATUS_ACTIVE = FEED_STATUS_ACTIVE = 1
VIDEO_STATUS_REJECTED = FEED_STATUS_REJECTED = 2

VIDEO_STATUSES = FEED_STATUSES = (
    (VIDEO_STATUS_UNAPPROVED, 'Unapproved'),
    (VIDEO_STATUS_ACTIVE, 'Active'),
    (VIDEO_STATUS_REJECTED, 'Rejected'))

SITE_STATUS_DISABLED = 0
SITE_STATUS_ACTIVE = 1

SITE_STATUSES = (
    (SITE_STATUS_DISABLED, 'Disabled'),
    (SITE_STATUS_ACTIVE, 'Active'))

VIDEO_THUMB_SIZES = [
    (375, 295), # featured on frontpage
    (140, 110)]

VIDEO_SERVICE_REGEXES = (
    ('YouTube', r'http://gdata\.youtube\.com/feeds/base/videos/-/'),
    ('YouTube', r'http://(www\.)?youtube\.com/'),
    ('blip.tv', r'http://(.+\.)?blip\.tv/'),
    ('Vimeo', r'http://(www\.)?vimeo\.com/'))

class Error(Exception): pass
class CannotOpenImageUrl(Error): pass


class OpenIdUser(models.Model):
    """
    Custom openid user authentication model.  Presently does not match
    up to Django's contrib.auth.models.User model, probably should be
    adjusted to do so eventually.

    Login and registration functionality provided in localtv.openid
    and its submodules.

    Fields:
      - url: URL that this user is identified by
      - user: the Django User object that this is a valid login for
    """
    user = models.OneToOneField('auth.User')
    url = models.URLField(verify_exists=False, unique=True)

    def __unicode__(self):
        return "%s <%s>" % (self.user.username, self.user.email)


class SiteLocation(models.Model):
    """
    An extension to the django.contrib.sites site model, providing
    localtv-specific data.

    Fields:
     - site: A link to the django.contrib.sites.models.Site object
     - logo: custom logo image for this site
     - background: custom background image for this site (unused?)
     - admins: a collection of Users who have access to administrate this
       sitelocation
     - status: one of SITE_STATUSES; either disabled or active
     - sidebar_html: custom html to appear on the right sidebar of many
       user-facing pages.  Can be whatever's most appropriate for the owners of
       said site.
     - footer_html: HTML that appears at the bottom of most user-facing pages.
       Can be whatever's most appropriate for the owners of said site.
     - about_html: HTML to display on the subsite's about page
     - tagline: displays below the subsite's title on most user-facing pages
     - css: The intention here is to allow subsites to paste in their own CSS
       here from the admin.  Not used presently, though eventually it should
       be.
     - frontpage_style: The style of the frontpage.  Either one of:
        * list
        * scrolling
        * categorized
     - display_submit_button: whether or not we should allow users to see that
       they can submit videos or not (doesn't affect whether or not they
       actually can though)
     - submission_requires_login: whether or not users need to log in to submit
       videos.
    """
    site = models.ForeignKey(Site, unique=True)
    logo = models.ImageField(upload_to='localtv/site_logos', blank=True)
    background = models.ImageField(upload_to='localtv/site_backgrounds',
                                   blank=True)
    admins = models.ManyToManyField('auth.User', blank=True,
                                    related_name='admin_for')
    status = models.IntegerField(
        choices=SITE_STATUSES, default=SITE_STATUS_ACTIVE)
    sidebar_html = models.TextField(blank=True)
    footer_html = models.TextField(blank=True)
    about_html = models.TextField(blank=True)
    tagline = models.CharField(max_length=250, blank=True)
    css = models.TextField(blank=True)
    frontpage_style = models.CharField(max_length=32, default="list")
    display_submit_button = models.BooleanField(default=True)
    submission_requires_login = models.BooleanField(default=False)

    def __unicode__(self):
        return self.site.name


    def user_is_admin(self, user):
        """
        Return True if the given User is an admin for this SiteLocation.
        """
        if not user.is_authenticated() or not user.is_active:
            return False

        if user.is_superuser:
            return True

        return bool(self.admins.filter(pk=user.pk).count())

class Tag(models.Model):
    """
    Tags for videos.

    Presently apply to all sitelocations.  Maybe eventually only certain tags
    should apply to certain sitelocations?

    Fields:
      - name: name of this tag
    """
    name = models.CharField(max_length=255)

    def __unicode__(self):
        return self.name


class Feed(models.Model):
    """
    Feed to pull videos in from.

    If the same feed is used on two different subsites, they will require two
    separate entries here.

{'fraction_non_alphanumeric': 0.0835183129855716, 'fraction_numerical': 0.004809470958194599, 'mean_word_length': 3.0880907372400754, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7012045', 'n_tokens_mistral': 3242, 'n_tokens_neox': 3096, 'n_words': 1014}