Commit df636ccf authored by gijs's avatar gijs

Added unique on display_slug

parent 455ab5b5
# Generated by Django 2.1.1 on 2018-09-19 13:54
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('etherpadlite', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='pad',
name='display_slug',
field=models.CharField(blank=True, max_length=255, unique=True, verbose_name='Name as used in URL (use :: for namespacing)'),
),
]
......@@ -162,7 +162,7 @@ class Pad(models.Model):
"""Schema and methods for etherpad-lite pads
"""
name = models.CharField(max_length=50)
display_slug = models.CharField(max_length=256, blank=True, verbose_name="Name as used in URL (use :: for namespacing)")
display_slug = models.CharField(max_length=255, blank=True, verbose_name="Name as used in URL (use :: for namespacing)", unique=True)
# I’m putting this back. We don’t use it anymore, but putting in the migration is
# more hassle then it’s worth
......
......@@ -28,6 +28,7 @@ from django.template.context_processors import csrf
from django.core.exceptions import PermissionDenied
from django.contrib.auth.decorators import login_required
from django.utils.translation import ugettext_lazy as _
from django.db import IntegrityError
# Django Apps import
......@@ -65,6 +66,43 @@ Create a regex for our include template tag
"""
include_regex = re.compile("{%\s?include\s?\"([\w._-]+)\"\s?%}")
def savePad(pad, slug, n=0):
if n < 25:
try:
if n > 0:
base, ext = os.path.splitext(slug)
pad.display_slug = '{base}-{n}{ext}'.format(base=base, ext=ext, n=n)
pad.name=slugify(pad.display_slug)[:42]
pad.save()
except IntegrityError:
savePad(pad, slug, n+1)
return pad
return False
def createPad (slug, server, group, n=0):
if n < 25:
try:
if n > 0:
base, ext = os.path.splitext(slug)
safe_slug = '{base}-{n}{ext}'.format(base=base, ext=ext, n=n)
else:
safe_slug = slug
pad = Pad(
name=slugify(safe_slug)[:42], # This is the slug internally used by etherpad
display_slug=safe_slug, # This is the slug we get to change afterwards
display_name=safe_slug, # this is just for backwards compatibility
server=group.server,
group=group
)
pad.save()
return pad
except IntegrityError:
return createPad(slug=slug, server=server, group=group, n=n+1)
return False
@login_required(login_url='/accounts/login')
def padCreate(request):
"""
......@@ -86,14 +124,8 @@ def padCreate(request):
else:
n = '{}{}'.format(n, default_extension)
pad = Pad(
name=slugify(n)[:42], # This is the slug internally used by etherpad
display_slug=n, # This is the slug we get to change afterwards
display_name=n, # this is just for backwards compatibility
server=group.server,
group=group
)
pad.save()
pad = createPad(slug=n, server=group.server, group=group)
return HttpResponseRedirect(reverse('pad-write', args=(pad.display_slug,) ))
else: # No form to process so create a fresh one
form = forms.PadCreate({'group': group.groupID})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment