# Path: channels/models.py
# class Channel(models.Model):
#     name = models.CharField(max_length=64, unique=True)
# 
#     STATUS_ACTIVE = 'AC'
#     STATUS_DELETED = 'DL'
# 
#     STATUS_CHOICES = (
#         (STATUS_ACTIVE, 'Active'),
#         (STATUS_DELETED, 'Deleted'),
#     )
# 
#     status = models.CharField(max_length=2, choices=STATUS_CHOICES, default=STATUS_ACTIVE)
#     subscribers = models.ManyToManyField('auth.user', related_name="subscriptions")
#     members = models.ManyToManyField('auth.user', related_name="members")
# 
#     def get_absolute_url(self):
#         return reverse("view_channel", kwargs={"slug": self.name}) 
# 
#     def clean(self):
#         if len(self.name) < 2:
#             raise ValidationError('Name is to short (<2 Chars)')
# 
#         if not re.match(r'^\w+$', self.name):
#             raise ValidationError("Name must only contain letters, numbers and underscores.")


from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseBadRequest
from django.contrib.auth.decorators import login_required
from models import VideoFile, Video
from channels.models import Channel
    from bitvid.settings import VIDEO_BUCKET_NAME
import boto.s3.connection
import boto.s3.key
import re
import bitvid.dbinfo
# Create your views here.
@login_required
def upload(request):
    if request.method == "GET":

    	chans = Channel.objects.filter(members__in=[request.user])
        return render(request, "upload.html",{"channels":chans})
    else:

        user = request.user
        file = request.FILES.get("file", None)

        if file is None:
            return HttpResponseBadRequest("You did not pass a file.")

        title = request.POST.get("title", "")
        desc = request.POST.get("desc", "")
        channel_id_str = request.POST.get("channel", "1")

        try:
            channel_id = int(channel_id_str)
        except ValueError:
            return HttpResponseBadRequest("Channel ID must be an integer")

        if title == "":
            return HttpResponseBadRequest("Title must not be empty.")

        if not re.match(r'^.{0,2000}$', title):
            return HttpResponseBadRequest("Description must not be longer than 2000 characters.")


# Path: channels/models.py
# class Channel(models.Model):
#     name = models.CharField(max_length=64, unique=True)
# 
#     STATUS_ACTIVE = 'AC'
#     STATUS_DELETED = 'DL'
# 
#     STATUS_CHOICES = (
#         (STATUS_ACTIVE, 'Active'),
#         (STATUS_DELETED, 'Deleted'),
#     )
# 
#     status = models.CharField(max_length=2, choices=STATUS_CHOICES, default=STATUS_ACTIVE)
#     subscribers = models.ManyToManyField('auth.user', related_name="subscriptions")
#     members = models.ManyToManyField('auth.user', related_name="members")
# 
#     def get_absolute_url(self):
#         return reverse("view_channel", kwargs={"slug": self.name}) 
# 
#     def clean(self):
#         if len(self.name) < 2:
#             raise ValidationError('Name is to short (<2 Chars)')
# 
#         if not re.match(r'^\w+$', self.name):
#             raise ValidationError("Name must only contain letters, numbers and underscores.")


# Path: videos/views.py
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseBadRequest
from django.contrib.auth.decorators import login_required
from models import VideoFile, Video
from channels.models import Channel
    from bitvid.settings import VIDEO_BUCKET_NAME
import boto.s3.connection
import boto.s3.key
import re
import bitvid.dbinfo

# Create your views here.
@login_required
def upload(request):
    if request.method == "GET":

    	chans = Channel.objects.filter(members__in=[request.user])
        return render(request, "upload.html",{"channels":chans})
    else:

        user = request.user
        file = request.FILES.get("file", None)

        if file is None:
            return HttpResponseBadRequest("You did not pass a file.")

        title = request.POST.get("title", "")
        desc = request.POST.get("desc", "")
        channel_id_str = request.POST.get("channel", "1")

        try:
            channel_id = int(channel_id_str)
        except ValueError:
            return HttpResponseBadRequest("Channel ID must be an integer")

        if title == "":
            return HttpResponseBadRequest("Title must not be empty.")

        if not re.match(r'^.{0,2000}$', title):
            return HttpResponseBadRequest("Description must not be longer than 2000 characters.")


        if not Channel.objects.filter(pk=channel_id).exists():