本文整理汇总了Python中tags.models.Tag.slug方法的典型用法代码示例。如果您正苦于以下问题:Python Tag.slug方法的具体用法?Python Tag.slug怎么用?Python Tag.slug使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tags.models.Tag
的用法示例。
在下文中一共展示了Tag.slug方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: new
# 需要导入模块: from tags.models import Tag [as 别名]
# 或者: from tags.models.Tag import slug [as 别名]
def new(request, slug):
"""Create new tie for reading."""
reading = get_object_or_404(Reading, slug=slug)
if request.method == "POST":
# If reading has 5 tags, do not add or create
if len(reading.tie_set.all()) >= 5:
messages.warning(request, "Each read can only have 5 tags")
else:
try:
# If user already added a tag
user = reading.tie_set.get(user=request.user)
messages.error(request, "You can only add 1 tag per read")
except ObjectDoesNotExist:
name = request.POST.get("tag_name")
name = name.lower()
pattern = only_letters()
# If name contains only letters
if re.search(pattern, name):
# If name does not contain any banned words
blacklist = banned_words()
if not re.search(blacklist, name):
try:
# If tag exists, get tag
tag = Tag.objects.get(name=name)
except ObjectDoesNotExist:
# If tag does not exist, create tag
tag = Tag(name=name, user=request.user)
tag.slug = slugify(tag.name)
tag.save()
try:
# Check to see if tie exists
tie = reading.tie_set.get(tag=tag)
except ObjectDoesNotExist:
# If tie does not exist, create it
tie = request.user.tie_set.create(reading=reading, tag=tag)
# add rep
add_rep(request, t=tie)
# create notification
notify(tie=tie)
d = {"reading": reading}
tags = loader.get_template("tags/tags.html")
context = RequestContext(request, d)
data = {"ties": tags.render(context)}
return HttpResponse(json.dumps(data), mimetype="application/json")
return HttpResponseRedirect(reverse("readings.views.detail", args=[reading.slug]))
示例2: new_bookmarklet
# 需要导入模块: from tags.models import Tag [as 别名]
# 或者: from tags.models.Tag import slug [as 别名]
def new_bookmarklet(request):
"""Create new reading from bookmarklet."""
if request.method == "POST":
content = request.POST.get("content", "")
image = request.POST.get("image", "")
link = request.POST.get("link", "")
titl = request.POST.get("title", "")[:80]
if request.user.is_staff:
user_pk = request.POST.get("user")
if user_pk:
try:
user = User.objects.get(pk=int(user_pk))
except ObjectDoesNotExist:
user = request.user
else:
user = request.user
else:
user = request.user
# if there is a link and title, create reading
if link and titl:
try:
# if reading with link exists, add notes to that reading
reading = Reading.objects.get(link=link)
reading_exists = True
except ObjectDoesNotExist:
reading_exists = False
titles = Reading.objects.filter(title=titl)
# if there is a reading with the title
if titles:
titl = "%s-%s" % (titl, str(titles.count()))
reading = Reading(image=image, link=link, title=titl, user=user)
reading.save()
# create vote for reading
reading.vote_set.create(user=user, value=1)
# add tag
name = request.POST.get("tag_name")
# if user added tag
if name:
try:
# check to see if user already tied a tag to this reading
existing_tie = reading.tie_set.get(user=user)
# if user has not added a tag to this reading
except ObjectDoesNotExist:
name = name.lower()
pattern = only_letters()
# If name contains only letters
if re.search(pattern, name):
# If name does not contain any banned words
blacklist = banned_words()
if not re.search(blacklist, name):
try:
# If tag exists, get tag
tag = Tag.objects.get(name=name)
except ObjectDoesNotExist:
# If tag does not exist, create tag
tag = Tag(name=name, user=user)
tag.slug = slugify(tag.name)
tag.save()
tie = user.tie_set.create(reading=reading, tag=tag)
# add rep
add_rep(request, t=tie)
# if user did not add a tag, auto tag
else:
auto_tag(request, reading)
# add rep
add_rep(request, rd=reading)
# if there is content, create note
if content.strip():
note = Note(content=content, reading=reading, user=user)
note.save()
# create first vote for note
user.vote_set.create(note=note, value=1)
if reading_exists:
facebook_graph_add_note(note.user, note.reading)
if not reading_exists:
facebook_graph_add_reading(reading.user, reading)
if request.user.is_staff:
# auto create votes for reading and reading.notes
auto_vote(request, reading)
data = {"success": 1}
return HttpResponse(json.dumps(data), mimetype="application/json")
content = request.GET.get("note", "").lstrip(" ")
link = request.GET.get("link", "")
# split title
html_title = split_title(request.GET.get("title", ""))
if request.user.is_staff:
if request.user.pk == 2:
users = admin_david_list()
else:
users = admin_user_list()
else:
users = []
d = {"content": content, "link": link, "titl": html_title, "title": "Add Reading", "users": users}
return render_to_response(
"readings/new_bookmarklet.html", add_csrf(request, d), context_instance=RequestContext(request)
)
示例3: new_reading
# 需要导入模块: from tags.models import Tag [as 别名]
# 或者: from tags.models.Tag import slug [as 别名]
def new_reading(request):
"""Create a new reading."""
NoteFormset = formset_factory(NoteForm, extra=3, formset=RequiredFormSet)
if request.method == 'POST':
if request.user.pk == 2:
form = DavidReadingForm(request.POST)
else:
form = AdminReadingForm(request.POST)
formset = NoteFormset(request.POST)
if form.is_valid() and formset.is_valid():
# save reading
reading = form.save()
# add tag
name = request.POST.get('tag_name')
# if user added a tag
if name:
name = name.lower()
pattern = only_letters()
# If name contains only letters
if re.search(pattern, name):
# If name does not contain any banned words
blacklist = banned_words()
if not re.search(blacklist, name):
try:
# If tag exists, get tag
tag = Tag.objects.get(name=name)
except ObjectDoesNotExist:
# If tag does not exist, create tag
tag = Tag(name=name, user=request.user)
tag.slug = slugify(tag.name)
tag.save()
tie = request.user.tie_set.create(reading=reading,
tag=tag)
# add rep
add_rep(request, t=tie)
# if user did not add a tag, auto tag
else:
auto_tag(request, reading)
# facebook open graph add reading
if reading.user.pk == request.user.pk:
facebook_graph_add_reading(request.user, reading)
# add rep
add_rep(request, rd=reading)
first = True
# save each note
for note_form in formset:
note = note_form.save(commit=False)
if note.content.strip():
note.reading = reading
# if this is the first note
# set note.user to reading.user
if first:
note.user = reading.user
first = False
else:
note.user = random_user()
# save note
note.save()
# create first vote for note
note.user.vote_set.create(note=note, value=1)
# add rep
add_rep(request, n=note)
# auto create votes for reading and reading.notes
auto_vote(request, reading)
messages.success(request, 'Reading created')
return HttpResponseRedirect(reverse('admins.views.reading',
args=[reading.slug]))
else:
if request.user.pk == 2:
form = DavidReadingForm()
else:
form = AdminReadingForm()
formset = NoteFormset()
di = { 'static': settings.STATIC_URL }
t = loader.get_template('javascript/bookmarklet.js')
context = RequestContext(request, di)
d = {
'bookmarklet': re.sub(r'\s', '%20', str(t.render(context))),
'form': form,
'formset': formset,
'title': 'New Reading',
}
return render_to_response('readings/new.html', add_csrf(request, d),
context_instance=RequestContext(request))