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)
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')
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'})
def signup_complete(request): # todo rewrite in classed-based-style """ Complete signup and send verification email """ # todo: check if ajax # todo: check if POST # todo: check for CSRF - ok # logging.error(request.POST) username = request.POST.get('username') email = request.POST.get('email') gender = request.POST.get('gender') birthdate = request.POST.get('birthdate') try: if birthdate: birthdate = datetime.strptime(birthdate, '%b %d %Y') except ValueError: birthdate = None logging.error('Wrong birthday') username_exists = User.objects.filter(username=username) email_exists = User.objects.filter(email=email) if username_exists: # todo: show this error on front-end return api_response.invalid_param('username already exists') if not email_exists: user = request.user profile = user.profile user.username = username user.save() profile.birthdate = birthdate profile.gender = gender profile.save() EmailValidation.objects.add(user, username, email) return api_response.ok({'email': not email_exists, 'username': not username_exists})
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)
def get_video(request, video_id): video = Video.objects.get_by_id(video_id) if video is None: return api_response.unauthorized("Video: %s not found!" % video_id) return api_response.ok(video)
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)
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)