def edit_link(request, id): link = get_object_or_404(Link, id = id) books = Book.objects.filter(user = request.user) if request.method == 'POST': link.url = request.POST.get('URL') link.title = striphtml(request.POST.get('TITLE')) link.description = striphtml(request.POST.get('DESCRIPTION')) for tag in link.tags.all(): link.tags.remove(tag) tag_list = request.POST.get('TAGS') for tag in _parse_tags(tag_list): link.tags.add(striphtml(tag)) new_book_list = request.POST.getlist('BOOKS') for book in books: if book in link.books.all() and book.title not in new_book_list: link.books.remove(book) elif book not in link.books.all() and book.title in new_book_list: link.books.add(book) link.save_og() link.save() return redirect('/link/{}/'.format(link.id)) else: tag_text = ", ".join(tag.name for tag in link.tags.all()) return render(request, 'links/edit_link.html', {'link':link, 'books': books, 'tag_text': tag_text})
def parse_tags(tagstring): """Normalize tags after parsing""" if not tagstring: return [] elif "," not in tagstring and '"' not in tagstring: return [normalize(tagstring)] else: return [normalize(t) for t in _parse_tags(tagstring)]
def rtd_parse_tags(tag_string): """ Parses a string into its tags. - Lowercases all tags - Converts underscores to hyphens - Slugifies tags :see: https://django-taggit.readthedocs.io/page/custom_tagging.html :param tag_string: a delimited string of tags :return: a sorted list of tag strings """ if tag_string: tag_string = tag_string.lower().replace('_', '-') return sorted([slugify(tag) for tag in _parse_tags(tag_string)])
def custom_tag_string(tag_string): """ Custom tag parsing for taggit to better support multi-word tags. Expected parsing: - 'legal document' -> ["legal document"] - '"fiscal document", Arabic script' -> ["fiscal document", "Arabic script"] - '"fiscal document", "Arabic script"' -> ["fiscal document", "Arabic script"] This is configured in settings with TAGGIT_TAGS_FROM_STRING. """ # Stack overflow solution: https://stackoverflow.com/questions/30513783/django-taggit-how-to-allow-multi-word-tags # Our github issue for taggit: https://github.com/jazzband/django-taggit/issues/783 if "," not in tag_string: tag_string += "," return _parse_tags(tag_string)
def create_link(request): if request.method == 'POST': link = Link() link.user = request.user link.url = request.POST.get('URL') link.title = striphtml(request.POST.get('TITLE')) link.description = striphtml(request.POST.get('DESCRIPTION')) link.save() tag_list = request.POST.get('TAGS') for tag in _parse_tags(tag_list): link.tags.add(striphtml(tag)) for book_name in request.POST.getlist('BOOKS'): link.books.add(Book.objects.get(user = request.user, title = book_name)) link.save() return redirect('/link/{}/'.format(link.id)) else: books = Book.objects.filter(user = request.user) return render(request, 'links/new_link.html', {'books': books})
def get_queryset(self): call_list = Call.objects.all() city = self.request.GET.get('city', '') type_call = self.request.GET.get('type', '') tags = self.request.GET.get('tags', '').lower() name = self.request.GET.get('name', '') name = re.sub('[^\w -]', '', name) name = re.sub(' +', ' ', name) words = name.split(' ') for word in words: call_list = call_list.filter(name__contains=word) description = self.request.GET.get('description', '') description = re.sub('[^\w -]', '', description) description = re.sub(' +', ' ', description) words_descr = description.split(' ') for word_descr in words_descr: call_list = call_list.filter(description__contains=word_descr) tags_arr = _parse_tags(tags) for tag in tags_arr: call_list = call_list.filter(tags__name__iexact=tag) if city: call_list = call_list.filter(city=city) if type_call == 'offer': call_list = call_list.filter(type=True) elif type_call == 'search': call_list = call_list.filter(type=False) sort_calls = self.request.GET.get('sort', '') if sort_calls == 'new': call_list = call_list.order_by('-date_time') elif sort_calls == 'users': call_list = call_list.order_by('user_id') elif sort_calls == 'have-card': call_list = call_list.order_by('-card') paginator = Paginator(call_list, 5) page = self.request.GET.get('page') try: call_list = paginator.page(page) except PageNotAnInteger: call_list = paginator.page(1) except EmptyPage: call_list = paginator.page(paginator.num_pages) return call_list
def parse_tags(tagstring): """Normalize tags after parsing""" return [normalize(t) for t in _parse_tags(tagstring)]
def clean(self, value, row=None, *args, **kwargs): if value: if self.force_lower: value = value.lower() return _parse_tags(value)
def update_tags(self): for word_con in SysWordConnector.objects.all(): for tag in _parse_tags(Word.objects.filter(id=word_con.id).first().tags): word_con.tags.add(tag)
def tags_splitter(s): return [t.lower() for t in _parse_tags(s)]
def lowercase_tags(tag_string): """Use taggit to parse the lowercased string""" return _parse_tags(tag_string.lower())