def video_new(request): if not request.user.is_authenticated(): return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path)) else: if request.method == 'POST': form = TagVideoForm(request.POST) if form.is_valid(): # deconstruct the url try: split_url = request.POST['video'].split('?') split_url = split_url[1].split('&') for split in split_url: if split.find('v=') >= 0: bits = split.split('=') vidId = bits[1] except: return render(request, 'youtag/tagvideo_new.html', {'form': form, 'errors': ('The URL was not properly formatted.',)}) # check and see if the video id exists already try: video = Video.objects.get(vidId=vidId) # if they have this video already, send them away tagvideo = TagVideo.objects.get(video=video, owner=request.user) if tagvideo is not None: return render(request, 'youtag/tagvideo_new.html', {'form': form, 'errors': ('That video already has been tagged.',)}) except: # if not, build a new video video = Video() video.vidId = vidId video.title = '' video.desc = '' # grab the video from the YouTube servers try: querystring = 'https://www.googleapis.com/youtube/v3/videos?part=snippet&key=' + DEVELOPER_KEY + '&id=' + vidId results = json.loads(requests.request('GET', querystring).text) video.title = results['items'][0]['snippet']['title'] video.desc = results['items'][0]['snippet']['description'] except: return render(request, 'youtag/tagvideo_new.html', {'form': form, 'errors': ('That video was not found.',)}) # start assigning data video.save() tagvideo = TagVideo() tagvideo.video = video tagvideo.owner = request.user tagvideo.created = timezone.now() tagvideo.save() # split up the tags taglist = request.POST['tag'].replace(' ', '') taglist = taglist.split(',') for tag in taglist: try: tag_obj = Tag.objects.get(word=tag) except: tag_obj = Tag.objects.create(word=tag) # add in the tag association if not tag == '': tag_obj.save() tagvideo.tags.add(tag_obj) # save them all! tagvideo.save() # CREATED! redirect_url = '../' + str(tagvideo.id) return redirect(redirect_url) else: return render(request, 'youtag/tagvideo_new.html', {'form': form, 'errors': ('There was errors in your submission.',)}) else: form = TagVideoForm() return render(request, 'youtag/tagvideo_new.html', {'form': form})
def ApiList(request, format=None): if request.method == 'GET': tagvideos = TagVideo.objects.filter(owner=request.user) serializer = TagVideoSerializer(tagvideos, many=True) return Response(serializer.data) elif request.method == 'POST': if not request.user.is_authenticated(): return Response('You are not logged in.', status=status.HTTP_400_BAD_REQUEST) else: data = json.loads(request.POST['_content']) # deconstruct the url try: split_url = data['url'].split('?') split_url = split_url[1].split('&') for split in split_url: if split.find('v=') >= 0: bits = split.split('=') vidId = bits[1] except: return Response('url is not valid.', status=status.HTTP_400_BAD_REQUEST) # make sure a list of tags is included try: taglist = data['tags'].replace(' ', '') except: return Response( 'Please include a comma-separated list of tags.', status=status.HTTP_400_BAD_REQUEST) try: video = Video.objects.get(vidId=vidId) # if they have this video already, send them away tagvideo = TagVideo.objects.get(video=video, owner=request.user) if tagvideo is not None: return Response('This video has already been tagged.', status=status.HTTP_400_BAD_REQUEST) except: # if not, build a new video video = Video() video.vidId = vidId video.title = '' video.desc = '' # grab the video from the YouTube servers try: querystring = 'https://www.googleapis.com/youtube/v3/videos?part=snippet&key=' + DEVELOPER_KEY + '&id=' + vidId results = json.loads(requests.request('GET', querystring).text) video.title = results['items'][0]['snippet']['title'] video.desc = results['items'][0]['snippet']['description'] except: return Response('That Youtube video does not exist.', status=status.HTTP_400_BAD_REQUEST) # start assigning data video.save() tagvideo = TagVideo() tagvideo.video = video tagvideo.owner = request.user tagvideo.created = timezone.now() tagvideo.save() # split up the tags taglist = taglist.split(',') for tag in taglist: try: tag_obj = Tag.objects.get(word=tag) except: tag_obj = Tag.objects.create(word=tag) # add in the tag association if not tag == '': tag_obj.save() tagvideo.tags.add(tag_obj) # save them all! tagvideo.save() return Response(status=status.HTTP_201_CREATED) else: return Response('This node only accepts GET and POST', status=status.HTTP_400_BAD_REQUEST)
def ApiList(request, format=None): if request.method == 'GET': tagvideos = TagVideo.objects.filter(owner=request.user) serializer = TagVideoSerializer(tagvideos, many=True) return Response(serializer.data) elif request.method == 'POST': if not request.user.is_authenticated(): return Response('You are not logged in.', status=status.HTTP_400_BAD_REQUEST) else: data = json.loads(request.POST['_content']) # deconstruct the url try: split_url = data['url'].split('?') split_url = split_url[1].split('&') for split in split_url: if split.find('v=') >= 0: bits = split.split('=') vidId = bits[1] except: return Response('url is not valid.', status=status.HTTP_400_BAD_REQUEST) # make sure a list of tags is included try: taglist = data['tags'].replace(' ', '') except: return Response('Please include a comma-separated list of tags.', status=status.HTTP_400_BAD_REQUEST) try: video = Video.objects.get(vidId=vidId) # if they have this video already, send them away tagvideo = TagVideo.objects.get(video=video, owner=request.user) if tagvideo is not None: return Response('This video has already been tagged.', status=status.HTTP_400_BAD_REQUEST) except: # if not, build a new video video = Video() video.vidId = vidId video.title = '' video.desc = '' # grab the video from the YouTube servers try: querystring = 'https://www.googleapis.com/youtube/v3/videos?part=snippet&key=' + DEVELOPER_KEY + '&id=' + vidId results = json.loads(requests.request('GET', querystring).text) video.title = results['items'][0]['snippet']['title'] video.desc = results['items'][0]['snippet']['description'] except: return Response('That Youtube video does not exist.', status=status.HTTP_400_BAD_REQUEST) # start assigning data video.save() tagvideo = TagVideo() tagvideo.video = video tagvideo.owner = request.user tagvideo.created = timezone.now() tagvideo.save() # split up the tags taglist = taglist.split(',') for tag in taglist: try: tag_obj = Tag.objects.get(word=tag) except: tag_obj = Tag.objects.create(word=tag) # add in the tag association if not tag == '': tag_obj.save() tagvideo.tags.add(tag_obj) # save them all! tagvideo.save() return Response(status=status.HTTP_201_CREATED) else: return Response('This node only accepts GET and POST', status=status.HTTP_400_BAD_REQUEST)
def video_new(request): if not request.user.is_authenticated(): return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path)) else: if request.method == 'POST': form = TagVideoForm(request.POST) if form.is_valid(): # deconstruct the url try: split_url = request.POST['video'].split('?') split_url = split_url[1].split('&') for split in split_url: if split.find('v=') >= 0: bits = split.split('=') vidId = bits[1] except: return render( request, 'youtag/tagvideo_new.html', { 'form': form, 'errors': ('The URL was not properly formatted.', ) }) # check and see if the video id exists already try: video = Video.objects.get(vidId=vidId) # if they have this video already, send them away tagvideo = TagVideo.objects.get(video=video, owner=request.user) if tagvideo is not None: return render( request, 'youtag/tagvideo_new.html', { 'form': form, 'errors': ('That video already has been tagged.', ) }) except: # if not, build a new video video = Video() video.vidId = vidId video.title = '' video.desc = '' # grab the video from the YouTube servers try: querystring = 'https://www.googleapis.com/youtube/v3/videos?part=snippet&key=' + DEVELOPER_KEY + '&id=' + vidId results = json.loads( requests.request('GET', querystring).text) video.title = results['items'][0]['snippet']['title'] video.desc = results['items'][0]['snippet'][ 'description'] except: return render( request, 'youtag/tagvideo_new.html', { 'form': form, 'errors': ('That video was not found.', ) }) # start assigning data video.save() tagvideo = TagVideo() tagvideo.video = video tagvideo.owner = request.user tagvideo.created = timezone.now() tagvideo.save() # split up the tags taglist = request.POST['tag'].replace(' ', '') taglist = taglist.split(',') for tag in taglist: try: tag_obj = Tag.objects.get(word=tag) except: tag_obj = Tag.objects.create(word=tag) # add in the tag association if not tag == '': tag_obj.save() tagvideo.tags.add(tag_obj) # save them all! tagvideo.save() # CREATED! redirect_url = '../' + str(tagvideo.id) return redirect(redirect_url) else: return render( request, 'youtag/tagvideo_new.html', { 'form': form, 'errors': ('There was errors in your submission.', ) }) else: form = TagVideoForm() return render(request, 'youtag/tagvideo_new.html', {'form': form})