Example #1
0
def add_html_doc(req, ensemble_id):
    import base.models as M
    user = UR.getUserInfo(req, False)
    if user is None:
        redirect_url = "/login?next=%s" % (req.META.get("PATH_INFO", "/"), )
        return HttpResponseRedirect(redirect_url)
    if not auth.canEditEnsemble(user.id, ensemble_id):
        return HttpResponseRedirect("/notallowed")
    addform = forms.Html5Form()
    if req.method == 'POST':
        addform = forms.Html5Form(req.POST)
        if addform.is_valid():
            source = M.Source()
            source.numpages = 1
            source.w = 0
            source.h = 0
            source.rotation = 0
            source.version = 0
            source.type = 4
            source.submittedby = user
            source.title = addform.cleaned_data['title']
            source.save()
            ownership = M.Ownership()
            ownership.source = source
            ownership.ensemble_id = ensemble_id
            ownership.save()
            info = M.HTML5Info()
            info.source = source
            info.url = addform.cleaned_data['url']
            info.save()
            return HttpResponseRedirect("/")
    return render_to_response("web/add_html_doc.html", {"form": addform})
Example #2
0
def add_youtube_doc(req, ensemble_id): 
    import base.models as M
    from apiclient.discovery import build
    from urlparse import urlparse, parse_qs
    import re
    re_iso8601 = re.compile("PT(?:(?P<hours>\d+)H)?(?:(?P<minutes>\d+)M)?(?:(?P<seconds>\d+)S)?")
    youtube = build("youtube", "v3", developerKey=settings.GOOGLE_DEVELOPER_KEY)
    user       = UR.getUserInfo(req, False)
    if user is None:
        redirect_url = "/login?next=%s" % (req.META.get("PATH_INFO","/"),)
        return HttpResponseRedirect(redirect_url)
    if not auth.canEditEnsemble(user.id, ensemble_id):
        return HttpResponseRedirect("/notallowed")
    addform = forms.YoutubeForm()
    if req.method == 'POST':
        addform = forms.YoutubeForm(req.POST)
        if addform.is_valid():             
            source = M.Source()
            source.numpages = 1
            source.w = 0
            source.h = 0
            source.rotation = 0
            source.version = 0
            source.type = 2
            source.submittedby=user
            source.save()            
            ownership = M.Ownership()
            ownership.source = source
            ownership.ensemble_id = ensemble_id
            ownership.save()
            info = M.YoutubeInfo()
            info.source = source
            url = addform.cleaned_data['url']
            result = urlparse(url)
            key = parse_qs(result.query)["v"][0]
            info.key = key
            info.save();
            ginfo = youtube.videos().list(part="id,contentDetails,snippet", id=key).execute()
            source.title = ginfo["items"][0]["snippet"]["title"]
            matches_dict = re_iso8601.match(ginfo["items"][0]["contentDetails"]["duration"]).groupdict()
            numsecs = 0
            if matches_dict["hours"] is not None: 
                numsecs += int(matches_dict["hours"])*3600
            if matches_dict["minutes"] is not None: 
                numsecs += int(matches_dict["minutes"])*60
            if matches_dict["seconds"] is not None: 
                numsecs += int(matches_dict["seconds"])
            source.numpages = numsecs #we use 1/100 sec precision. 
            source.save();
            #addform.cleaned_data['title']

            return HttpResponseRedirect("/")         
    return render_to_response("web/add_youtube_doc.html", {"form": addform})
Example #3
0
def ondemand(req, ensemble_id):
    url = req.GET.get("url", None)
    if url:
        try:
            source_info = M.OnDemandInfo.objects.get(url=url,
                                                     ensemble_id=ensemble_id)
            return HttpResponseRedirect("/f/%s" % (source_info.source_id, ))
        except M.OnDemandInfo.DoesNotExist:
            ensemble = None
            try:
                ensemble = M.Ensemble.objects.get(pk=ensemble_id)
            except M.Ensemble.DoesNotExist:
                return HttpResponse("No such ensemble: %s " % (ensemble_id, ))
            if not ensemble.allow_ondemand:
                return HttpResponse(
                    "ondemand uplaod not allowed for that ensemble: %s " %
                    (ensemble_id, ))
            import urllib2
            from upload.views import insert_pdf_metadata
            f = urllib2.urlopen(url)
            s = None
            try:
                s = f.read()
                f.close()
                source = M.Source()
                uid = UR.getUserId(req)
                source.submittedby_id = uid
                source.title = url.rpartition("/")[2]
                source.save()
                sid = source.id
                annotations.addOwnership(sid, ensemble_id)
                REPOSITORY_DIR = "%s/%s" % (settings.HTTPD_MEDIA,
                                            "/pdf/repository")
                f2 = open("%s/%s" % (
                    REPOSITORY_DIR,
                    sid,
                ), "wb")
                f2.write(s)
                f2.close()
                insert_pdf_metadata(sid, REPOSITORY_DIR)
                info = M.OnDemandInfo()
                info.url = url
                info.ensemble_id = ensemble_id
                info.source_id = sid
                info.save()
                return HttpResponseRedirect("/f/%s" % (sid, ))
            except Exception as e:
                return HttpResponse("URL Read Error: %s, %s " % (url, e))
    else:
        return HttpResponse("Missing parameter: url")
Example #4
0
def add_html_doc(req, ensemble_id):
    import base.models as M
    user = UR.getUserInfo(req, False)
    if user is None:
        redirect_url = "/login?next=%s" % (req.META.get("PATH_INFO", "/"), )
        return HttpResponseRedirect(redirect_url)
    if not auth.canEditEnsemble(user.id, ensemble_id):
        return HttpResponseRedirect("/notallowed")
    addform = forms.Html5Form()
    if req.method == 'POST':
        addform = forms.Html5Form(req.POST)
        if addform.is_valid():
            source = M.Source()
            source.numpages = 1
            source.w = 0
            source.h = 0
            source.rotation = 0
            source.version = 0
            source.type = 4
            source.submittedby = user
            source.title = addform.cleaned_data['title']
            source.save()
            ownership = M.Ownership()
            ownership.source = source
            ownership.ensemble_id = ensemble_id
            ownership.save()
            info = M.HTML5Info()
            info.source = source
            # trailing slash is sometimes added by server redirects
            # but person specifying upload url may not realize this
            # so remove trailing slash as well as hash part of the URL
            info.url = addform.cleaned_data['url'].partition("#")[0].rstrip(
                "/")
            info.save()
            return HttpResponseRedirect("/")
    return render_to_response("web/add_html_doc.html", {"form": addform})