def extract_more_verses(text, ref, point, outline): """ -text: string of verse numbers without book or chapter, e.g. '3, 7, 10-14, 16'. -ref: Reference immediately preceding this string of verse numbers. -point: outline point the verses are under. -outline: outline list from extract(). Helper function for extract() -- creates References for verses and adds them to outline. Creates References for verses and adds them to outline. """ # gets list of tuples: (verse, end_verse,) # e.g. '3, 7, 10-14, 16' --> [(3, None,), (7, None,), (10, 14,), (16, None,)] verses = re.findall(re.compile(r'(?P<verse>\d{1,3})(?:-(?P<end_verse>\d{1,3}))?'), text) for verse in verses: more_verses_ref = Reference(outline_point=point, book=ref.book, verse=int(verse[0])) more_verses_ref.end_verse = int(verse[1]) if verse[1] else None if ref.end_chapter is not None: more_verses_ref.chapter = ref.end_chapter else: more_verses_ref.chapter = ref.chapter if more_verses_ref.end_verse is not None: more_verses_ref.end_chapter = more_verses_ref.chapter more_verses_ref.save() outline[-1][1].append(more_verses_ref) return
def save_references(request): """ SGD_features.tab format looks like this: 0) Primary SGDID (mandatory) 1) Feature type (mandatory) 2) Feature qualifier (optional) 3) Feature name (optional) 4) Standard gene name (optional) 5) Alias (optional, multiples separated by |) 6) Parent feature name (optional) 7) Secondary SGDID (optional, multiples separated by |) 8) Chromosome (optional) 9) Start_coordinate (optional) 10) Stop_coordinate (optional) 11) Strand (optional) 12) Genetic position (optional) 13) Coordinate version (optional) 14) Sequence version (optional) 15) Description (optional) """ featuresFile = request.FILES['features_file'] features = [x.split('\t') for x in featuresFile.read().splitlines()] for line in features: reference = Reference() reference.sgdid = line[0] reference.feature_type = line[1] if line[2]: reference.qualifier = line[2] if line[3]: reference.feature_name = line[3] if line[4]: reference.standard_name = line[4] if line[5]: reference.aliases = line[5] if line[6]: reference.parent_name = line[6] if line[7]: reference.secondary_sgdid = line[7] if line[15]: reference.description = line[15] reference.createdDate = datetime.datetime.now() reference.modifiedDate = datetime.datetime.now() reference.save() return HttpResponseRedirect('/strains/')
def extract(text): """ Extract from a block of text a list of 2-tuples containing an outline point and a list of normalized, tupled verse references under that outline point. """ outline = [] point = OutlinePoint(level=0, string='Scripture Reading:') point.save() outline.append((point, [],)) for r in re.finditer(scripture_re, text): try: # find Roman numerals / outline points is_bullet = False for i in range(1,5): if r.group(i): point = OutlinePoint(level=i, string=r.group(i)) point.save() outline.append((point, [],)) is_bullet = True if is_bullet == False: ref = Reference(outline_point=point) outline[-1][1].append(ref) if r.group('book'): # reference contains book name ref.book = get_book(r.group('book'))[1] ref.chapter = int(r.group('chapter')) if r.group('chapter') else None ref.verse = int(r.group('verse')) if r.group('verse') else None ref.end_chapter = int(r.group('end_chapter')) if r.group('end_chapter') else None ref.end_verse = int(r.group('end_verse')) if r.group('end_verse') else None if ((ref.end_verse is not None) and (ref.end_chapter is None)): ref.end_chapter = ref.chapter ref.save() if r.group('more_verses'): extract_more_verses(r.group('more_verses'), ref, point, outline) else: # get book from previous reference if len(outline[-1][1]) > 1: ref.book = outline[-1][1][-2].book else: # if this is the first reference under an outline point ref.book = outline[-2][1][-1].book if r.group('headless_chapter'): # reference has no book ref.chapter = int(r.group('headless_chapter')) ref.verse = int(r.group('headless_verse')) if r.group('headless_verse') else None ref.end_chapter = int(r.group('headless_end_chapter')) if r.group('headless_end_chapter') else None ref.end_verse = int(r.group('headless_end_verse')) if r.group('headless_end_verse') else None if ((ref.end_verse is not None) and (ref.end_chapter is None)): ref.end_chapter = ref.chapter ref.save() if r.group('more_headless_verses'): extract_more_verses(r.group('more_headless_verses'), ref, point, outline) else: if r.group('lonely_verse'): if len(outline[-1][1]) > 1: ref.chapter = outline[-1][1][-2].chapter else: # this is the first reference under an outline point ref.chapter = outline[-2][1][-1].chapter ref.verse = int(r.group('lonely_verse')) ref.end_verse = int(r.group('lonely_end_verse')) if r.group('lonely_end_verse') else None if ((ref.end_verse is not None) and (ref.end_chapter is None)): ref.end_chapter = ref.chapter ref.save() if r.group('more_lonely_verses'): extract_more_verses(r.group('more_lonely_verses'), ref, point, outline) except InvalidReferenceException: pass return outline
def post(self, request): param = QueryDict(request.body) uuid = param.get('uuid') title = param.get('title') time = param.get('time') origin = param.get('origin') _authors = param.getlist('authors') link = param.get('link') _tags = param.getlist('tags') content = param.get('content') refer_to = param.getlist('reference') score = param.get('score') try: year, month = time.split('-') year, month = int(year), int(month) publish_time = datetime.date(year, month, 1) except Exception as e: logger.error(traceback.format_exc(e)) return JsonResponse({'msg': '提供的日期{}有误'.format(time)}, status=500) for _tag in _tags: try: _tag = int(_tag) _ = ResearchTag.objects.get(research_tag_id=_tag) except Exception as e: logger.error(traceback.format_exc(e)) return JsonResponse({'msg': '错误的标签{}'.format(_tag)}, status=500) tags = ResearchTag.objects.filter( research_tag_id__in=[int(_t) for _t in _tags]) author_ids = [] for _author in _authors: if _author.isdigit(): author_ids.append(int(_author)) elif Author.objects.filter(name=_author).exists(): a = Author.objects.get(name=_author).author_id author_ids.append(a) else: a = Author(name=_author) a.save() author_ids.append(a.author_id) authors = Author.objects.filter(author_id__in=author_ids) try: score = int(score) except Exception as e: logger.error(traceback.format_exc(e)) return JsonResponse({'msg': '错误的评分分数格式'}, status=500) if not Paper.objects.filter(paper_uuid=uuid).exists(): # 新建的场合 try: comment = PaperComment(content=content) comment.save() paper = Paper(paper_uuid=uuid, title=title, publish_origin=origin, publish_time=publish_time, author=authors, link=link, tag=tags, comment=comment, self_score=score) paper.save() redis.set(self.LATEST_KEY, str(uuid_gen.uuid4())) except Exception as e: logger.error(traceback.format_exc(e)) return JsonResponse({'msg': '保存失败'}, status=500) else: return JsonResponse({ 'next': reverse('paperdb.detail', kwargs={'paper_uuid': paper.paper_uuid}) }) try: # 编辑的场合 paper = Paper.objects.get(paper_uuid=uuid) except Exception as e: logger.error(traceback.format_exc(e)) return JsonResponse({'msg': '错误的uuid/未找到相关论文记录'}, status=404) else: paper.title = title paper.publish_time = publish_time paper.publish_origin = origin paper.author = authors paper.link = paper.link paper.tag = tags paper.self_score = score try: paper.save() except Exception as e: logger.error(traceback.format_exc(e)) return JsonResponse({'msg': '保存失败'}, status=500) if paper.comment is None: if content != '': comment = PaperComment(content=content) comment.save() paper.comment = comment paper.save() elif content != paper.comment.content.replace( '\r\n', '\n'): # traditional下的换行符出入 paper.comment.content = content paper.comment.save() for refer_to_paper in Paper.objects.filter(paper_uuid__in=refer_to): if not Reference.objects.filter( reference_src=paper, reference_trg=refer_to_paper).exists(): reference = Reference(reference_src=paper, reference_trg=refer_to_paper) reference.save() return JsonResponse({ 'next': reverse('paperdb.detail', kwargs={'paper_uuid': paper.paper_uuid}) })