def notify(request, key): """Process vid.ly notification requests.""" # Verify key matches stored key. if key != settings.NOTIFY_KEY: return HttpResponseForbidden() notification = parseNotify(request) # Check for finished videos. for task in notification['tasks']: if task.finished: video = get_object_or_none(Video, shortlink=task.shortlink) if video is not None: video.state = 'complete' video.save() send_video_complete_email(video) # Check for video errors for error in notification['errors']: video = get_object_or_none(Video, shortlink=error.shortlink) if video is not None: video.state = 'error' video.save() send_video_error_email(video) return HttpResponse()
def upvote(request, video_shortlink): """Add an upvote to a video.""" response = HttpResponse(mimetype='application/json') if video_shortlink in request.COOKIES: response.status_code = 403 response.content = json.dumps({'error': 'already voted'}) return response video = get_object_or_none(Video, shortlink=video_shortlink) if video is not None: try: video.upvote() except socket.timeout: log.warning('Timeout connecting to celery to upvote video ' 'shortlink: %s' % video_shortlink) response.status_code = 500 response.content = json.dumps({'error': 'celery timeout'}) else: response.set_cookie(str(video_shortlink), value='1', httponly=False, max_age=settings.VOTE_COOKIE_AGE) response.content = json.dumps({'success': 'success'}) else: response.status_code = 404 response.content = json.dumps({'error': 'video not found'}) return response
def send_rejection_email(user_id): """ Send email to the video's creator telling them that their video has been rejected. """ user = get_object_or_none(User, id=user_id) if user: with use_lang(user.profile.locale): body = render_to_string('videos/2013/rejection_email.html', { 'user': user }) _send_mail(EMAIL_SUBJECT, body, [user.email])
def send_rejection_email(user_id): """ Send email to the video's creator telling them that their video has been rejected. """ user = get_object_or_none(User, id=user_id) if user: with use_lang(user.profile.locale): text_body = render_to_string('videos/2013/rejection_email.ltxt', {'user': user}) html_body = render_to_string('videos/2013/rejection_email.html', {'user': user}) _send_mail(EMAIL_SUBJECT, text_body, html_body, [user.email])
def cached_viewcount(video_id): """Get the viewcount for the specified video from the cache. If the viewcount isn't in the cache, load it from the DB and store it in the cache. """ key = VIEWS_KEY % video_id viewcount = cache.get(key) if viewcount is None: video = get_object_or_none(Video, id=video_id) if video is None: return None cache.set(key, video.views, 0) # Cache indefinitely viewcount = video.views return viewcount
def process_video(video_id): """Update metadata about the given video on Vimeo.""" from flicks.videos.models import Video video = get_object_or_none(Video, id=video_id) if video: vimeo.set_title(video.vimeo_id, video.title) # Set description to title + author + description. description = u'{title} by {author}\n\n{description}'.format( title=video.title, author=video.user.profile.display_name, description=video.description) vimeo.set_description(video.vimeo_id, description) # Add to the channel for the user's region. channels = settings.VIMEO_REGION_CHANNELS channel_id = channels.get(video.user.profile.region, None) if channel_id: vimeo.add_to_channel(video.vimeo_id, channel_id) # Set their country code as a tag. vimeo.add_tags(video.vimeo_id, [video.user.profile.country]) video.processed = True video.save() # Email moderators that a video has finished processing and is ready # for review. perm = Permission.objects.get(codename='change_video2013') moderators = User.objects.filter(Q(groups__permissions=perm) | Q(user_permissions=perm)).distinct() subject = (u'[flicks-moderation] `{0}` is ready for review' .format(video.title)) message = render_to_string('videos/2013/moderation_email.html', {'video': video}) send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [u.email for u in moderators])
def save(self, *args, **kwargs): """ Prior to saving, trigger approval processing if approval status has changed. """ original = get_object_or_none(Video2013, id=self.id) # Only process approval if the value changed. if original and original.approved != self.approved: self.process_approval() # Save after processing to prevent making the video public until # processing is complete. return_value = super(Video2013, self).save(*args, **kwargs) # Send an email out if the user hasn't been notified and re-save. # Don't send an email out if this is a new, already-approved video. if original and self.approved and not self.user_notified: send_approval_email(self) self.user_notified = True return_value = super(Video2013, self).save(*args, **kwargs) return return_value
def edit_profile(request): """Create and/or edit a user profile.""" profile = get_object_or_none(UserProfile, pk=request.user.pk) if request.method == 'POST': if profile: form = UserProfileEditForm(request.POST, instance=profile) else: form = UserProfileCreateForm(request.POST, instance=profile) if form.is_valid(): profile = form.save(commit=False) profile.user = request.user profile.save() return redirect('flicks.users.my_profile') else: if profile: form = UserProfileEditForm(instance=profile) else: form = UserProfileCreateForm(instance=profile) d = dict(profile=profile, edit_form=form, page_type='secondary form') return render(request, 'users/edit_profile.html', d)
def test_exists(self): """If no exceptions occur, return the matched object.""" video = Video2012Factory.create(title='exists') value = get_object_or_none(Video2012, title='exists') eq_(value, video)
def test_multiple_objects_returned(self): """Return None if multiple objects are returned.""" Video2012Factory.create(title='multiple') Video2012Factory.create(title='multiple') value = get_object_or_none(Video2012, title='multiple') eq_(value, None)
def test_does_not_exist(self): """Return None if no matching video exists.""" value = get_object_or_none(Video2012, title='Does not exist') eq_(value, None)
def test_exists(self): """If no exceptions occur, return the matched object.""" with build_video(self.user, title='exists') as video: value = get_object_or_none(Video, title='exists') eq_(value, video)
def test_multiple_objects_returned(self): """Return None if multiple objects are returned.""" mkvideo = partial(build_video, self.user, title='multiple') with nested(mkvideo(), mkvideo()): value = get_object_or_none(Video, title='multiple') eq_(value, None)