예제 #1
0
def annotations(request):
    if request.method == "POST":
        received_annotation = json.loads(request.body)
        findex = received_annotation['findex']
        stuid = received_annotation['stuid']
        ftype = received_annotation.get('ftype', 0)
        atype = received_annotation.get('atype', 0)
        mid = received_annotation.get('mid', 0)
        del received_annotation['findex']
        del received_annotation['stuid']
        received_annotation.pop('ftype', None)
        received_annotation.pop('atype', None)
        received_annotation.pop('mid', None)
        # del received_annotation['ftype']
        # del received_annotation['atype']
        annotation = Annotation(user_id=request.user.id,
                                ftype=ftype,
                                findex=findex,
                                stuid=stuid,
                                atype=atype,
                                mid=mid,
                                annotation=json.dumps(received_annotation))
        annotation.save()
        return HttpResponseSeeOtherRedirect('/annotate/annotations/' +
                                            str(annotation.id))

    # 取得目前使用者所有標註資料。(理論上不會執行到這邊,應該會從 search 僅取得目前頁面的標註資料)
    annotations = [
        a for a in Annotation.objects.filter(
            user_id=request.user.id).order_by('id')
    ]
    rows = []
    for annotation in annotations:
        rows.append(json.loads(annotation.annotation))
    return JsonResponse(rows, safe=False)
예제 #2
0
def import_csv(filename):
    result = []

    with codecs.open(filename, 'r', 'utf-8') as f:
        word_columns = []
        index_columns = []
        sentence_column = 0

        reader = csv.reader(f, delimiter=';')
        for i, row in enumerate(reader):
            if i == 0:  # header row
                for j, r in enumerate(row):
                    if r.startswith('w'):
                        word_columns.append(j)
                    if r.startswith('index'):
                        index_columns.append(j)
                    if r.startswith('full'):
                        sentence_column = j
            else:  # other rows
                pk = int(row[0])
                words = [row[c] for c in word_columns]
                indices = [row[c] for c in index_columns]
                sentence = row[sentence_column]
                result.append(Annotation(pk, words, indices, sentence))

    return result
예제 #3
0
    def _create_or_update(self, anno=None):
        create = anno is None
        if create:
            anno = Annotation()

        body = self._get_request_body()
        anno.context_id = body['contextId']
        anno.collection_id = body['collectionId']
        anno.uri = body['uri']
        anno.media = body['media']
        anno.user_id = body['user']['id']
        anno.user_name = body['user']['name']
        anno.is_private = False if len(
            body.get('permissions', {}).get('read', [])) == 0 else True
        anno.text = body.get('text', '')
        anno.quote = body.get('quote', '')
        anno.json = json.dumps(body)

        if 'parent' in body and body['parent'] != '0':
            anno.parent_id = int(body['parent'])
        anno.save()

        if create and anno.parent_id:
            parent_anno = Annotation.objects.get(pk=int(body['parent']))
            parent_anno.total_comments = F('total_comments') + 1
            parent_anno.save()

        if not create:
            anno.tags.clear()
        for tag_name in body.get('tags', []):
            tag_object, created = AnnotationTags.objects.get_or_create(
                name=tag_name.strip())
            anno.tags.add(tag_object)

        return anno
예제 #4
0
def import_annotation(a,
                      video_obj,
                      frame_to_pk,
                      detection_to_pk,
                      vdn_dataset=None):
    da = Annotation()
    da.video = video_obj
    da.x = a['x']
    da.y = a['y']
    da.h = a['h']
    da.w = a['w']
    da.vdn_key = a['id']
    if vdn_dataset:
        da.vdn_dataset = vdn_dataset
    if a['label'].strip():
        da.label = a['label']
        if vdn_dataset:
            label_object, created = VLabel.objects.get_or_create(
                label_name=a['label'],
                source=VLabel.VDN,
                video=video_obj,
                vdn_dataset=vdn_dataset)
        else:
            label_object, created = VLabel.objects.get_or_create(
                label_name=a['label'], source=VLabel.UI, video=video_obj)
        da.label_parent = label_object
    da.frame_id = frame_to_pk[a['frame']]
    if a['detection']:
        da.detection_id = detection_to_pk[a['detection']]
    da.full_frame = a['full_frame']
    da.metadata_text = a['metadata_text']
    da.save()
    return da
예제 #5
0
    def post(self, unused):
        self.response.headers['Content-type'] = 'text/javascript'
        user = users.get_current_user()
        annos = self.request.get('annos', '').strip()
        format = self.request.get('format',
                                  utils.guess_format(annos, force_tabs=True))
        if not annos:
            simplejson.dumps({'status': 'fail'})
            return
        name = user and user.nickname() or ""
        title = self.request.get('title') or ""
        anno_id = hashlib.md5(annos + title).hexdigest()

        # dont save unless it's new.
        a = Annotation.gql(
            "WHERE anno_id = :anno_id AND title = :title AND author = :author",
            anno_id=anno_id,
            title=title,
            author=user).get()
        if a is None:
            a = Annotation(title=title,
                           author=user,
                           content=annos.strip(),
                           anno_id=anno_id,
                           format=format)
            a.put()

        self.response.out.write(
            simplejson.dumps({
                'status': 'success',
                'anno_id': anno_id
            }))
예제 #6
0
    def get(self, anno_id=''):
        user = users.get_current_user()
        if anno_id == "":
            a = Annotation.all().order("-date").get()
        else:
            a = Annotation.all().filter('anno_id = ', anno_id).get()
        if a is None: a = Annotation()
        user_tmpl = user_stuff(self.request.uri)
        history = [x for x in Annotation.all().order("-date") if x.title][:12]
        name = user and user.nickname() or ""
        user_history = [x for x in Annotation.gql("WHERE author = :author", \
                              author=user) if x.title][:12] if user else None

        bar_hists = a.get_bar_hists()

        query = Annotation.all()
        gobe_examples = [
            Annotation.by_anno_id('8316edf46cc9b4ec845596e91997630f'),
            Annotation.by_anno_id('50f939d37ebf65f4588ba5bc435e7bdf'),
            Annotation.by_anno_id('e58139270757f77ef25ddcfa0df8e61b'),
        ]

        self.response.out.write(
            render("index.html",
                   user_tmpl,
                   anno_name=name,
                   anno_id=a.anno_id or "",
                   anno_content=a.content or "",
                   anno_title=a.title or "",
                   bar_hists=bar_hists,
                   gobe_examples=gobe_examples,
                   history=history,
                   user_history=user_history))
예제 #7
0
def _addAnnotation(sentence,
                   annotation,
                   user,
                   prevSentence=None,
                   nextSentence=None):
    a = Annotation(sentence, annotation, user, prevSentence, nextSentence)
    db_session.add(a)
    db_session.commit()
예제 #8
0
def index():
    user: User = current_user
    facial_exprssions_translations = None
    if user.language == 'english':
        facial_exprssions_translations = utils.english_fe
    elif user.language == 'persian':
        facial_exprssions_translations = utils.persian_fe
    elif user.language == 'filipino':
        facial_exprssions_translations = utils.filipino_fe

    print(user.language, '**********')
    print(user.username, '  %%%%%%%%')

    if request.method == "POST":  # if the request is post (i.e. new video annotated?)
        # print(request.form)
        form = request.form



        annotation: Annotation = Annotation()
        annotation.emotion = form['emotion']
        annotation.anger_score = form['anger']
        annotation.contempt_score = form['contempt']
        annotation.disgust_score = form['disgust']
        annotation.annoyed_score = form['annoyed']
        annotation.gender = form['gender']
        annotation.filename = form.get("token")
        user.add_video(form.get("token"))

        social_signals = ','.join([ss for ss in form.getlist('socialsignal')])
        extra_ss = form.get("extra")
        ## TODO: should be tested
        if len(extra_ss) > 0:
            social_signals += ',%s' % extra_ss
        annotation.confidence = form['confidence']
        annotation.annotator_culture = user.culture
        annotation.annotator_language = user.language
        annotation.annotator_individuality = user.individuality
        annotation.social_signals = social_signals


        db.session.add(annotation)
        db.session.commit()
        vid = utils.get_random_video(user.culture, user.get_annotated_videos())
        completed = utils.get_completed_videos(user.culture, user.get_annotated_videos())
        if vid == "FINISHED":
            return render_template('thankyou.html')
        print("Annotation %s created :)" % annotation)

        return render_template('index.html', context={'video':vid, 'language': user.language, 'completed': completed,
                                                      'expressions': facial_exprssions_translations})

    vid = utils.get_random_video(user.culture, user.get_annotated_videos())
    completed = utils.get_completed_videos(user.culture, user.get_annotated_videos())
    if vid == "FINISHED":
        return render_template('thankyou.html')
    return render_template('index.html', context={'video':vid, 'language': user.language, 'completed': completed,
                                                  'expressions': facial_exprssions_translations})
예제 #9
0
def annotate(request, id):
    u = User.objects.get(id=1)
    aud = AudioMeta.objects.get(id=id)
    t = request.POST['t']
    a = open("/home/leegao/webapps/notetag/notetag/test.txt", "w")
    a.write(t)
    a.close()
    m = request.POST['data']
    an = Annotation(user_id=u, aud_id=aud, time=int(t), msg=m)
    an.save()
    return HttpResponse(content="0")
예제 #10
0
def post_annotation():
    data = request.get_json()

    doc = data['doc']
    target_text = data['target_text']
    index = data['index']
    anchor_offset = data['anchor_offset']
    focus_offset = data['focus_offset']
    type = data['type']
    basket = data['basket']

    doc = Doc.objects().get(id=doc)
    sent = Sent.objects().get(doc=doc, index=index)
    user = g.user

    target_sent = Sent.objects().get(doc=doc, index=index)

    # In sentence, filter logic have to be changed
    if type == 'sentence':
        annotations = Annotation.objects.filter(doc=doc,
                                                sent=sent,
                                                index=index,
                                                user=g.user,
                                                type=type)
    else:
        annotations = Annotation.objects.filter(doc=doc,
                                                sent=sent,
                                                index=index,
                                                user=g.user,
                                                type=type,
                                                anchor_offset=anchor_offset)

    if annotations.count() > 0:
        annotation = annotations[0]
    else:
        annotation = Annotation(doc=doc,
                                sent=sent,
                                user=user,
                                index=index,
                                type=type,
                                anchor_offset=anchor_offset)

    annotation.anchor_offset = anchor_offset
    annotation.focus_offset = focus_offset
    annotation.entire_text = target_sent.text
    annotation.target_text = target_text
    annotation.basket = basket
    annotation.ip = request.remote_addr

    annotation.save()

    return json.dumps({
        'annotation': annotation.dump(),
    })
예제 #11
0
    def get(self, anno_id):
        # they can request a certain format by ending with .gobe whatever.
        self.response.headers['Content-type'] = 'text/plain'

        if anno_id == "":
            a = Annotation.all().order("-date").get()
        elif anno_id != "all":
            a = Annotation.all().filter('anno_id = ', anno_id).get()
        if a is None: a = Annotation()
        content = a.content

        if a.format and a.format != "gobe":
            content = [x.strip() for x in content.split("\n")]
            content = utils.main(content, a.format, force_tabs=True)

        self.response.out.write(content or "")
예제 #12
0
def add_answer(request, stimulus_index, answer_pk):

    response = {}

    #Add the answer to the database
    answer = Answer.objects.get(pk=answer_pk)
    Annotation(question_nr=stimulus_index, answer=answer).save()

    #Check and return whether the answer is also correct
    correct_pk = open(STIMULI_FILE_LOCATION).readlines()[int(
        stimulus_index)].strip().split('\t')[1]
    response['correct'] = correct_pk == answer_pk

    print('Received', request.POST)

    if response['correct']:
        minimum_streak_to_enter_highscore = calculate_minimum_streak_to_enter_highscore(
        )

        #First, see whether this is info to add to an existing streak
        if request.POST['streak_id'] not in [
                None, 'undefined', 'null', False, 'false'
        ]:

            current_streak = Streak.objects.filter(
                pk=request.POST['streak_id'])[0]
            current_streak.length = request.POST['streak_length']
            current_streak.save()
            response['streak_id'] = current_streak.pk

        #if not, check whether you made the highscore
        elif int(request.POST['streak_length']
                 ) >= minimum_streak_to_enter_highscore:

            #If so, add the streak to the database
            new_streak = Streak(playername='Anonymous',
                                length=int(request.POST['streak_length']))
            new_streak.save()
            response['streak_id'] = new_streak.pk
            response['ask_for_name'] = True

        #else, do nothing specail
        else:
            response['streak_id'] = False

    return HttpResponse(json.dumps(response))
예제 #13
0
def _handle_post_annotation_request(user, document, request):
    if not user.is_authenticated:
        return HttpResponseForbidden()

    annotation = Annotation()
    annotation.annotator = user
    annotation.content = sanitize(request.POST['annotation_content'])
    annotation.document_this_annotation_belongs = document
    annotation.page_index = request.POST["page_id"].split("_")[2]
    annotation.height_percent = request.POST["height_percent"]
    annotation.width_percent = request.POST["width_percent"]
    annotation.top_percent = request.POST["top_percent"]
    annotation.left_percent = request.POST["left_percent"]
    annotation.frame_color = request.POST["frame_color"]
    annotation.is_public = True if request.POST[
        "is_public"] == 'true' else False
    annotation.save()

    # send notification to the document uploader
    if annotation.annotator.pk != document.owner.pk:
        notify.send(
            sender=annotation.annotator,
            recipient=document.owner,
            action_object=annotation,
            verb='post annotation',
            redirect_url=annotation.url,
            image_url=annotation.annotator.portrait_url,
            description=h.handle(annotation.content),
            is_public=annotation.is_public,
        )
    # send notification to the collectors, i.e., followers
    for user in document.collectors.all():
        if annotation.annotator.pk != user.pk and document.owner.pk != user.pk:
            notify.send(
                sender=annotation.annotator,
                recipient=user,
                action_object=annotation,
                verb='post annotation',
                redirect_url=annotation.url,
                image_url=annotation.annotator.portrait_url,
                description=h.handle(annotation.content),
                is_public=annotation.is_public,
            )

    context = {
        "document": document,
        'annotation': annotation,
        'ANONYMOUS_USER_PORTRAIT_URL': settings.ANONYMOUS_USER_PORTRAIT_URL,
        "new_annotation_id": annotation.id,
    }
    return JsonResponse(
        {
            'new_annotationdiv_html':
            render(request, "file_viewer/one_annotation_div.html",
                   context).content,
            'new_annotation_id':
            annotation.id,
            'new_annotation_uuid':
            str(annotation.clean_uuid),
            'new_annotation_json':
            annotation,
        },
        encoder=AnnotationEncoder)
예제 #14
0
def ImportGFF(specimen, file):
    fileName = file.split('/')
    fileName = fileName[-1].split('.')[0]

    specimenObject = GetSpecimen(specimen)

    gff, created = GFF.objects.get_or_create(Specimen=specimenObject,
                                             FileName=fileName,
                                             Public=True)
    print gff.FileName
    #Get the specimen this annotation file is for
    gff.Specimen = GetSpecimen(specimen)
    #Set default version type
    gff.GFFVersion = 2

    #Open file and check for version type
    #TODO: Grab GFF Info and store in database
    annotationFile = open(file, 'r')
    counter = 0
    for line in annotationFile.readlines():
        counter += 1

        if line.startswith("##"):
            if "gff-version" in line:
                temp = line.split(' ')
                gff.GFFVersion = temp[1]

        if counter == 10:
            break
    annotationFile.close()
    gff.save()

    #Grab a list of chromosomes related to this specimen
    validChromosomes = GetRelatedChromosomes(gff.Specimen)

    annotations = {}

    #In GFF format, each line is an annotation.
    #Read in each annotation and split at tabs (as per gff spec)
    #Then store as needed into an Annotation object which will be pushed onto the annotations stack
    #TODO: Bust out version reading into methods
    if gff.GFFVersion == 2:
        print "BEGINNING READING OF VERSION 2 FILE...\n"
        annotationFile = open(file, 'r')
        counter = 0
        for line in annotationFile.readlines():
            counter += 1
            if not line.startswith('#'):
                elements = line.split('\t')

                annotation = Annotation()
                #TODO: Handle when values come back null from not finding a matching chromosome!
                annotation.Specimen = specimen
                annotation.Chromosome = ParseChromosomeName(
                    validChromosomes,
                    elements[0])  #Related validChromosomes, chromosome
                annotation.ID = counter
                annotation.Source = elements[1]
                annotation.Feature = elements[2]
                annotation.Start = elements[3]
                annotation.End = elements[4]

                #Do checking for Score
                if elements[5] == '.':
                    annotation.Score = None
                else:
                    annotation.Score = elements[5]

                #Do checking for Strand
                if elements[6] == '.':
                    annotation.Strand = None
                else:
                    annotation.Strand = elements[6]

                #Do checking for frame
                if elements[7] == '.':
                    annotation.Frame = None
                else:
                    annotation.Frame = elements[7]

                #Check for existence of attribute and possible extra length
                if len(elements) >= 9:
                    annotation.Attribute = elements[8:]
                else:
                    annotation.Attribute = None

                if annotation.Chromosome in annotations:
                    #appent to the current list
                    annotations[annotation.Chromosome].append(annotation)
                else:
                    #Create the chromosome entry in the annotations dictionary
                    annotations[annotation.Chromosome] = []
                    annotations[annotation.Chromosome].append(annotation)

                if counter % 10000 == 0:
                    sys.stdout.write('.')

                    #print "RESULTS: ", annotation.Specimen, annotation.Source, annotation.Feature, annotation.Start, annotation.End, annotation.Score, annotation.Strand, annotation.Frame, annotation.Attribute

        print "DONE READING FILE!"
    else:
        print "THIS GFF VERSION IS NOT SUPPORTED: Version", gff.GFFVersion

    print "BEGINNING SORTING LIST..."
    #Sort the list of annotations read in by their start value (this could probably be optimized by using an always ordered list and inserting in order above)
    #annotations = sorted(annotations, key = lambda annotation: annotation.Start)
    for sublist in annotations:
        annotations[sublist] = sorted(
            annotations[sublist], key=lambda annotation: int(annotation.Start))
    print "DONE SORTING!"

    chunkAndStoreAnnotations(gff, annotations)
예제 #15
0
def display_file_viewer_page(request):
    if request.method == "POST":
        if request.POST["operation"] == "delete_annotation":
            annotation = Annotation.objects.get(
                id=int(request.POST["annotation_id"]))
            annotation.delete()
            return HttpResponse()

        elif request.POST["operation"] == "delete_annotation_reply":
            document = Document.objects.get(
                id=int(request.POST["document_id"]))
            reply_annotation = AnnotationReply.objects.get(
                id=int(request.POST["reply_id"]))
            reply_annotation.delete()
            context = {
                "document": document,
                "annotations": document.annotation_set.order_by("page_index"),
            }
            return render(request,
                          "file_viewer/annotation_viewer_subpage.html",
                          context)

        elif request.POST["operation"] == "delete_comment":
            document = Document.objects.get(
                id=int(request.POST["document_id"]))
            comment = Comment.objects.get(id=int(request.POST["comment_id"]))
            comment.delete()
            context = {
                "document": document,
                "comments": document.comment_set.order_by("-post_time"),
            }
            return render(request, "file_viewer/comment_viewer_subpage.html",
                          context)

        elif request.POST["operation"] == "like_annotation":
            annotation = Annotation.objects.get(
                id=int(request.POST["annotation_id"]))
            annotation.num_like += 1
            annotation.save()
            return HttpResponse()

        elif request.POST["operation"] == "like_annotation_reply":
            annotation_reply = AnnotationReply.objects.get(
                id=int(request.POST["annotation_reply_id"]))
            annotation_reply.num_like += 1
            annotation_reply.save()
            return HttpResponse()

        elif request.POST["operation"] == "like_comment":
            comment = Comment.objects.get(id=int(request.POST["comment_id"]))
            comment.num_like += 1
            comment.save()
            return HttpResponse()

        elif request.POST["operation"] == "collect":
            user = get_user(request)
            document = Document.objects.get(
                id=int(request.POST["document_id"]))
            document.collectors.add(user)
            document.save()
            return HttpResponse()

        elif request.POST["operation"] == "uncollect":
            user = get_user(request)
            document = Document.objects.get(
                id=int(request.POST["document_id"]))
            document.collectors.remove(user)
            document.save()
            return HttpResponse()

        elif request.POST["operation"] == "refresh":
            document = Document.objects.get(
                id=int(request.POST["document_id"]))
            context = {
                "document": document,
                "comments": document.comment_set.order_by("-post_time"),
            }
            return render(request, "file_viewer/comment_viewer_subpage.html",
                          context)

        elif request.POST["operation"] == "comment":
            document = Document.objects.get(
                id=int(request.POST["document_id"]))
            if request.POST["comment_content"] != "":
                comment = Comment()
                comment.content = request.POST["comment_content"]
                comment.commenter = get_user(request)
                comment.document_this_comment_belongs = document
                if "reply_to_comment_id" in request.POST:
                    comment.reply_to_comment = Comment.objects.get(
                        id=int(request.POST["reply_to_comment_id"]))
                comment.save()
            context = {
                "document": document,
                "comments": document.comment_set.order_by("-post_time"),
            }
            return render(request, "file_viewer/comment_viewer_subpage.html",
                          context)

        elif request.POST["operation"] == "annotate":
            document = Document.objects.get(
                id=int(request.POST["document_id"]))
            annotation = Annotation()
            annotation.content = request.POST["annotation_content"]
            annotation.annotator = get_user(request)
            annotation.document_this_annotation_belongs = document
            annotation.page_index = request.POST["page_id"].split("_")[2]
            annotation.height_percent = request.POST["height_percent"]
            annotation.width_percent = request.POST["width_percent"]
            annotation.top_percent = request.POST["top_percent"]
            annotation.left_percent = request.POST["left_percent"]
            annotation.frame_color = request.POST["frame_color"]
            annotation.save()
            context = {
                "document": document,
                "annotations": document.annotation_set.order_by("page_index"),
                "new_annotation_id": annotation.id,
            }
            return render(request,
                          "file_viewer/annotation_viewer_subpage.html",
                          context)

        elif request.POST["operation"] == "reply_annotation":
            document = Document.objects.get(
                id=int(request.POST["document_id"]))
            if request.POST["annotation_reply_content"] != "":
                annotation_reply = AnnotationReply()
                annotation = Annotation.objects.get(
                    id=int(request.POST["reply_to_annotation_id"]))
                annotation_reply.content = request.POST[
                    "annotation_reply_content"]
                annotation_reply.replier = get_user(request)
                annotation_reply.reply_to_annotation = annotation
                if request.POST.has_key("reply_to_annotation_reply_id"):
                    annotation_reply.reply_to_annotation_reply = AnnotationReply.objects.get(
                        id=int(request.POST["reply_to_annotation_reply_id"]))
                annotation_reply.save()
            context = {
                "document": document,
                "annotations": document.annotation_set.order_by("page_index"),
            }
            return render(request,
                          "file_viewer/annotation_viewer_subpage.html",
                          context)

    else:
        document = Document.objects.get(id=int(request.GET["document_id"]))
        file = document.unique_file

        file_position = file.file_field.storage.path(file.file_field)
        file_url = file.file_field.url

        file_directory_name, file_name_and_extension = os.path.split(
            file_position)
        file_name, extension = file_name_and_extension.split(".")
        img_folder_path = os.path.join(file_directory_name, file_name)

        pages = []

        user = get_user(request)
        collected = False
        if user.is_authenticated(
        ) and document in user.collected_document_set.all():
            collected = True

        document.num_visit += 1
        document.save()

        if extension == "zip":
            zip_file = zipfile.ZipFile(file_position, "r")
            zip_alphabetical_list = sorted(zip_file.namelist())

            if not os.path.isdir(img_folder_path):
                os.mkdir(img_folder_path)
                i = 0
                for page_name in zip_alphabetical_list:
                    zip_file.extract(page_name, img_folder_path)
                    os.rename(
                        os.path.join(img_folder_path, page_name),
                        os.path.join(img_folder_path,
                                     str(i) + "." + page_name.split(".")[-1]))
                    pages.extend([
                        os.path.dirname(file_url)[1:] + "/" + file_name + "/" +
                        str(i) + "." + page_name.split(".")[-1]
                    ])
                    i += 1
            else:
                i = 0
                for page_name in zip_alphabetical_list:
                    pages.extend([
                        os.path.dirname(file_url)[1:] + "/" + file_name + "/" +
                        str(i) + "." + page_name.split(".")[-1]
                    ])
                    i += 1

        elif extension == "rar":
            rar_file = rarfile.RarFile(file_position, "r")
            rar_alphabetical_list = sorted(rar_file.namelist())

            if not os.path.isdir(img_folder_path):
                os.mkdir(img_folder_path)
                i = 0
                for page_name in rar_alphabetical_list:
                    rar_file.extract(page_name, img_folder_path)
                    os.rename(
                        os.path.join(img_folder_path, page_name),
                        os.path.join(img_folder_path,
                                     str(i) + "." + page_name.split(".")[-1]))
                    pages.extend([
                        os.path.dirname(file_url)[1:] + "/" + file_name + "/" +
                        str(i) + "." + page_name.split(".")[-1]
                    ])
                    i += 1
            else:
                i = 0
                for page_name in rar_alphabetical_list:
                    pages.extend([
                        os.path.dirname(file_url)[1:] + "/" + file_name + "/" +
                        str(i) + "." + page_name.split(".")[-1]
                    ])
                    i += 1

        elif extension == "pdf":
            '''
            if not os.path.isdir(img_folder_path):
                os.mkdir(img_folder_path)
                document_images = Image(filename=file_position, resolution=240)
                for i, page in enumerate(document_images.sequence):
                    with Image(page) as page_image:
                        page_image.alpha_channel = False
                        page_image.save(filename = os.path.join(img_folder_path, str(i) + ".png"))
                        pages.extend([os.path.dirname(file_url)[1:] + "/" + file_name + "/" + str(i) + ".png"])
            else:
                document_images = Image(filename=file_position, resolution=180)
                for i, page in enumerate(document_images.sequence):
                    pages.extend([os.path.dirname(file_url)[1:] + "/" + file_name + "/" + str(i) + ".png"])
            '''
            context = {
                "document": document,
                "file_url": file_url[1:],
                "comments": document.comment_set.order_by("-post_time"),
                "annotations": document.annotation_set.order_by("page_index"),
                "collected": collected
            }
            return render(request, "file_viewer/pdf_file_viewer_page.html",
                          context)

        context = {
            "numPages": len(pages),
            "document": document,
            "pages": pages,
            "comments": document.comment_set.order_by("-post_time"),
            "annotations": document.annotation_set.order_by("page_index"),
            "collected": collected,
        }
        return render(request, "file_viewer/file_viewer_page.html", context)
예제 #16
0
                                'Derived by automated computational analysis '
                                'using gene prediction method:', 'By')
                        except KeyError:
                            note = 'None'
                            #print('falta la nota')

                        try:
                            print(
                                'x1: %s x2: %s s: %s | gene: %s synonym: %s product: %s notes: %s'
                                %
                                (feature.location.start, feature.location.end,
                                 feature.location.strand, gene, gene_synonym,
                                 product, note))
                            ann = Annotation(species_name,
                                             int(feature.location.start),
                                             int(feature.location.end),
                                             int(feature.location.strand),
                                             product, note)
                            # output_file.write(str(ann) + '\n')
                            annotations.append(ann)
                        except Exception as e:
                            print(
                                'Exception: Cannot create annotation in position '
                                + str(i) + ' - e: ' + str(e))
                    anterior_start = int(feature.location.start)
                    anterior_end = int(feature.location.end)

                    i += 1

                total += i
                print('==> ANOTACIONES EN ' + record.id + ': ' + str(i))