Beispiel #1
0
def MeguzUpdateMultimedia(request, prize_id, user_token):
    from django.core.urlresolvers import reverse
    from django.contrib import messages
    from django_youtube.api import Api, AccessControl, ApiError

    from main.models import User

    prize = Offer.objects.get(pk=prize_id)
    user = User.objects.get(token=user_token)
    meguz = Meguz.objects.get(prize=prize, user=user)
    if meguz is None:
        return HttpResponseRedirect("/")
    else:

        # If request.GET isset from Youtube, update media_url with video ID
        if request.method == "GET":
            if "status" in request.GET:
                if request.GET["status"] == "200":
                    if "id" in request.GET:

                        # Update database info
                        meguz.video_id = request.GET["id"]
                        meguz.video_thumb = "http://img.youtube.com/vi/%s/1.jpg" % request.GET["id"]
                        meguz.save()

                        # Update youtube metadata
                        try:
                            api = Api()
                            api.authenticate()

                            title = meguz.title + " | Meguz.com"
                            description = meguz.description + "... Visita www.meguz.com para mas informacion"
                            keywords = (
                                meguz.prize.category.name
                                + ","
                                + meguz.prize.prize_name
                                + ","
                                + meguz.prize.company.name
                            )
                            api.update_video(meguz.video_id, title, description, keywords)

                            # Api error happend
                        except ApiError as e:
                            messages.add_message(request, messages.ERROR, e.message)
                            return HttpResponseRedirect("/")

                            # Other error
                        except:
                            messages.add_message(
                                request, messages.ERROR, _("Ha ocurrido un error, por favor intenta de nuevo")
                            )
                            return HttpResponseRedirect("/")

                        return HttpResponseRedirect("/usuario/mis-meguz")  # /meguz/{id}/{slug}
Beispiel #2
0
    def save(self, *args, **kwargs):
        """
        Syncronize the video information on db with the video on Youtube
        The reason that I didn't use signals is to avoid saving the video instance twice.
        """

        # if this is a new instance add details from api
        if not self.id:
            # Connect to api and get the details
            entry = self.entry()

            # Set the details
            self.title = entry.media.title.text
            self.description = entry.media.description.text
            self.keywords = entry.media.keywords.text
            self.youtube_url = entry.media.player.url
            self.swf_url = entry.GetSwfUrl()
            if entry.media.private:
                self.access_control = AccessControl.Private
            else:
                self.access_control = AccessControl.Public

            # Save the instance
            super(Video, self).save(*args, **kwargs)

            # show thumbnails
            for thumbnail in entry.media.thumbnail:
                t = Thumbnail()
                t.url = thumbnail.url
                t.video = self
                t.save()
        else:
            # updating the video instance
            # Connect to API and update video on youtube
            api = Api()

            # update method needs authentication
            api.authenticate()

            # Update the info on youtube, raise error on failure
            api.update_video(self.video_id, self.title, self.description,
                             self.keywords, self.access_control)

        # Save the model
        return super(Video, self).save(*args, **kwargs)
Beispiel #3
0
    def save(self, *args, **kwargs):
        """
        Syncronize the video information on db with the video on Youtube
        The reason that I didn't use signals is to avoid saving the video instance twice.
        """

        # if this is a new instance add details from api
        if not self.id:
            # Connect to api and get the details
            entry = self.entry()

            # Set the details
            self.title = entry.media.title.text
            self.description = entry.media.description.text
            self.keywords = entry.media.keywords.text
            self.youtube_url = entry.media.player.url
            self.swf_url = entry.GetSwfUrl()
            if entry.media.private:
                self.access_control = AccessControl.Private
            else:
                self.access_control = AccessControl.Public

            # Save the instance
            super(Video, self).save(*args, **kwargs)

            # show thumbnails
            for thumbnail in entry.media.thumbnail:
                t = Thumbnail()
                t.url = thumbnail.url
                t.video = self
                t.save()
        else:
            # updating the video instance
            # Connect to API and update video on youtube
            api = Api()

            # update method needs authentication
            api.authenticate()

            # Update the info on youtube, raise error on failure
            api.update_video(self.video_id, self.title, self.description,
                             self.keywords, self.access_control)

        # Save the model
        return super(Video, self).save(*args, **kwargs)
Beispiel #4
0
def MeguzEditForm(request, meguz_id, user_token):

	from django.core.urlresolvers import reverse
	from django.contrib import messages
	from django_youtube.api import Api, AccessControl, ApiError	

	if request.method == 'POST':
		meguzForm = MeguzForm(request.POST)

		if(meguzForm.is_valid()):

			# Load model with form
			meguz = Meguz.objects.get(pk=meguz_id)
			meguz.title = request.POST['title']
			meguz.description = request.POST['description']
			meguz.id = meguz_id

			# Load user id
			from main.models import User
			user = User.objects.get(token=user_token)
			if user is None:
				context = {'response':'Forbidden'}	
			else:
				meguz.save()

				if meguz.status == 'C':
					es = ES("localhost:9200")
					meguzES = es.get("meguz","meguz",meguz.id)
					meguzES.title = meguz.title
					meguzES.description = meguz.description
					meguzES.save()

				# Update youtube data
				try:
					api = Api()
					api.authenticate()

					title = meguz.title + " | Meguz.com"
					description = meguz.description + "... Visita www.meguz.com para mas informacion"
					keywords = meguz.prize.category.name + "," + meguz.prize.prize_name + "," + meguz.prize.company.name
					
					if meguz.video_id != '':
						api.update_video(meguz.video_id, title, description, keywords)

				# Api error happend
				except ApiError as e:
					messages.add_message(request, messages.ERROR, e.message)
					return HttpResponseRedirect("/")

				# Other error
				except:
					messages.add_message(request, messages.ERROR, _('Ha ocurrido un error, por favor intenta de nuevo'))
					return HttpResponseRedirect("/")


				context = {'response':meguz.id}
		else:
			context = {'response':'fail'}

	else:			
		context = {'response': 'invalid'}

	return render_to_response('meguz/edit_form.html', context, context_instance=RequestContext(request))