def import_user_bookmarks_from_ns_list(user,nsbmk_txt): ref_and_metadata = import_references_from_ns_bookmark_list(nsbmk_txt) bmk_to_process = [] for ref,meta in ref_and_metadata.items(): try: bmk = UserBookmark.objects.get(owner=user,reference=ref) except ObjectDoesNotExist: bmk = UserBookmark(owner=user,reference=ref, saved_date=ref.pub_date) ref.save_count += 1 for src in ref.sources.all(): user.userprofile.sources.add(src) bmk.is_public = meta.is_public bmk.comment = meta.note # pile up the bookmarks for tag attribution bmk_to_process.append((bmk,meta)) with transaction.commit_on_success(): for b,_ in bmk_to_process: b.reference.save() b.save() classif_data_to_save = [] for bmk,meta in bmk_to_process: valid_tags = [t for t in meta.tags if len(t)<=TAG_NAME_MAX_LENGTH] if len(valid_tags)!=len(meta.tags): invalid_tags = [t for t in meta.tags if len(t)>TAG_NAME_MAX_LENGTH] logger.error("Could not import some bmk tags with too long names (%s>%s)"\ % (",".join(len(t) for t in invalid_tags),TAG_NAME_MAX_LENGTH)) classif_data_to_save.append(set_item_tag_names(user,bmk.reference,valid_tags)) with transaction.commit_on_success(): for cd in classif_data_to_save: cd.save()
def save(self): """Warning: the bookmark will be saved as well as the related objects (no commit options). Returns the bookmark. """ url, _ = sanitize_url(self.cleaned_data["url"]) title = self.cleaned_data["title"] \ or build_reference_title_from_url(url) comment = self.cleaned_data["comment"] pub_date = self.cleaned_data["pub_date"] \ or datetime.now(timezone.utc) src_url,_ = sanitize_url(self.cleaned_data["source_url"] \ or build_source_url_from_reference_url(url)) src_title = self.cleaned_data["source_title"] \ or build_reference_title_from_url(src_url) # Find or create a matching reference try: bookmarked_ref = Reference.objects.get(url=url) # Arbitrarily chose one of the possible sources src_query = bookmarked_ref.sources if src_query.count() > 1: ref_src = src_query.all()[0] else: ref_src = src_query.get() except ObjectDoesNotExist: try: ref_src = Reference.objects.get(url=src_url) except ObjectDoesNotExist: ref_src = Reference(url=src_url, title=src_title, pub_date=pub_date) ref_src.save() if src_url == url: bookmarked_ref = ref_src else: bookmarked_ref = Reference(url=url, title=title, pub_date=pub_date) bookmarked_ref.save() bookmarked_ref.sources.add(ref_src) with transaction.commit_on_success(): try: bmk = UserBookmark.objects.get(owner=self.user, reference=bookmarked_ref) except ObjectDoesNotExist: bmk = UserBookmark(owner=self.user, reference=bookmarked_ref, saved_date=datetime.now(timezone.utc)) bookmarked_ref.save_count += 1 bmk.save() bookmarked_ref.save() # allow the user-specific comment to be changed and also prefix # it with the user specified title if it differs from the # existing reference title. if comment: new_comment = comment else: new_comment = bmk.comment if self.cleaned_data["title"] and title!=bookmarked_ref.title \ and not new_comment.startswith(title): new_comment = "%s: %s" % (title, new_comment) if new_comment != bmk.comment: bmk.comment = new_comment bmk.save() with transaction.commit_on_success(): if ref_src not in self.user.userprofile.sources.all(): self.user.userprofile.sources.add(ref_src) with transaction.commit_on_success(): for rust in ReferenceUserStatus\ .objects.filter(owner=self.user, reference=bookmarked_ref).all(): rust.has_been_saved = True rust.save() return bmk