Ejemplo n.º 1
0
def add_video(request):
    competition_id = request.POST.get('competition_id')
    if competition_id is None:
        return api_response.error("Missing competition id")
    else:
        competition = Competition.objects.get_by_id(competition_id)
        if competition is None:
            logging.error("Competition not found: %s" % id)
            return api_response.error("Competition not found.")

    video_url = request.POST.get('video_url')

    thumbnail_url = None
    if video_url is not None:
        thumbnail_url = get_thumbnail(video_url)

    video = Video.objects.create(
        competition_id=competition.competition_id,
        user_id=request.user.id, video_url=video_url,
        thumbnail_url=thumbnail_url)
    competition.videos.append(video.video_id)
    competition.save()
    Activity.objects.joined_competition_activity(request.user, competition,
                                                 video)
    return api_response.ok(video)
Ejemplo n.º 2
0
def add_vote(request):
    competition_id = request.POST.get('competition_id')
    video_id = request.POST.get('video_id')
    try:
        Vote.objects.create(competition_id=competition_id, video_id=video_id,
                            user_id=request.user.id)
    except VoteError as e:
        return api_response.error(e.message)
    video = Video.objects.get(video_id=video_id)
    video.inc_score()
    return api_response.ok('voted')
Ejemplo n.º 3
0
def invite_friends_to_competition_by_email(request):
    """	Invite friend to compete """
    sender_user_id = request.user.id

    competition_id = request.POST.get('competition_id')
    if competition_id is None:
        return api_response.invalid_param(
            {'message': 'missing competition_id'})

    errors = []
    emailsStr = request.POST.get('emails')
    if emailsStr is not None:
        emails = emailsStr.split(',')
        for email in emails:
            email = email.strip()
            if not email_re.search(email):
                errors.append(email)
            else:
                Invite.objects.get_or_create(sender_user_id=sender_user_id,
                                             competition_id=competition_id,
                                             kind=InviteType.FRIEND,
                                             invite_email=email)
                context = {
                    'url': settings.SERVER_URL,
                    'compete_url': reverse('competition:view',
                                           args=[competition_id]),
                }
                html = render_to_string('emails/invite_friend_email.html',
                                        context)
                mail.send_mail(
                    sender='Starboxx TV Invitations <%s>' % settings.EMAIL_NOREPLY,
                    to=email,
                    subject="Invite to Compete on Starboxx.TV",
                    body='',
                    html=html)

    if len(errors) > 0:
        return api_response.error(errors)
    else:
        return api_response.ok({'stat': 'ok'})
Ejemplo n.º 4
0
def get_competition(request, pk):
    video = Video.objects.get_by_id(pk)
    if video is None:
        logging.error("Video not found: %s" % pk)
        api_response.error("Video not found.")
    return api_response.ok(video)
Ejemplo n.º 5
0
    def put(self, request, *args, **kwargs):
        # PUT = QueryDict(request.body, request.encoding)
        PUT = simplejson.loads(request.body, request.encoding)
        # todo: check if ajax
        # todo: check if PUT
        # todo: check for CSRF
        competition = Competition.objects.get_by_id(self.pk)
        if competition is None:
            logging.error("Competition not found: %s" % self.pk)
            api_response.error("Competition not found.")
            return
        try:
            thumbnail_url = PUT.get('thumbnail_url')
            if thumbnail_url is not None:
                competition.thumbnail_url = thumbnail_url
            name = PUT.get('name')
            if name is not None:
                competition.name = name
            description = PUT.get('description')
            if description is not None:
                competition.description = description
            is_private = PUT.get('is_private')
            if is_private is not None:
                competition.is_private = is_private
            category_id = PUT.get('category_id')
            if category_id is not None:
                competition.category_id = category_id
            minimum_bounty = PUT.get('minimum_bounty')
            if minimum_bounty is not None:
                competition.minimum_bounty = int(minimum_bounty)
            intro_video_url = PUT.get('intro_video_url')
            if intro_video_url is not None:
                competition.intro_video_url = intro_video_url
                if thumbnail_url is None:
                    thumbnail_url = get_thumbnail(intro_video_url)
                    competition.thumbnail_url = thumbnail_url

            categories = PUT.get('categories')
            if categories is not None:
                competition.categories = categories

            # todo(dan) check dates
            beginning_date = PUT.get('beginning_date')
            if beginning_date is not None:
                if beginning_date:
                    beginning_date = beginning_date.strip()
                    beginning_date = datetime.strptime(beginning_date,
                                                       "%m/%d/%Y")
                    competition.beginning_date = beginning_date
                else:
                    competition.beginning_date = None

            end_date = PUT.get('end_date')
            if end_date is not None:
                if end_date:
                    end_date = end_date.strip()
                    end_date = datetime.strptime(end_date, "%m/%d/%Y")
                    competition.end_date = end_date
                else:
                    competition.end_date = None

            competition.save()
            return api_response.ok(competition)
        except Exception as e:
            return api_response.error(e)
Ejemplo n.º 6
0
 def get(self, request, *args, **kwargs):
     competition = Competition.objects.get_by_id(self.pk)
     if competition is None:
         logging.error("Competition not found: %s" % self.pk)
         api_response.error("Competition not found.")
     return api_response.ok(competition)