snipt/snipts/utils.py

45 lines
1.1 KiB
Python
Raw Permalink Normal View History

2015-07-24 18:39:25 -07:00
import uuid
2012-02-12 21:42:25 -08:00
from django.contrib.auth import authenticate, login
2012-02-11 07:23:09 -08:00
from django.template.defaultfilters import slugify
2012-05-15 17:10:50 -07:00
from pygments.lexers import get_all_lexers
2015-07-24 18:39:25 -07:00
from registration.signals import user_registered
2014-04-24 08:16:45 -07:00
2012-02-11 07:23:09 -08:00
def slugify_uniquely(value, model, slugfield="slug"):
2015-07-24 09:22:28 -07:00
suffix = False
2012-02-11 07:23:09 -08:00
potential = base = slugify(value)[:255]
while True:
if suffix:
if value:
potential = "-".join([base, str(suffix)])
else:
potential = str(suffix)
2012-02-11 07:23:09 -08:00
if not model.objects.filter(**{slugfield: potential}).count():
return potential
2019-01-23 15:52:55 -08:00
suffix = str(uuid.uuid4()).split("-")[0]
2012-02-12 21:42:25 -08:00
2015-07-24 18:28:31 -07:00
2012-02-12 21:42:25 -08:00
def activate_user(user, request, **kwargs):
2019-01-23 15:52:55 -08:00
user = authenticate(
username=request.POST["username"], password=request.POST["password1"]
)
2012-02-12 21:42:25 -08:00
login(request, user)
2015-07-24 18:28:31 -07:00
2012-05-15 17:10:50 -07:00
def get_lexers_list():
lexers = list(get_all_lexers())
2012-05-20 19:05:29 -07:00
for l in lexers:
2019-01-23 15:52:55 -08:00
if l[0] == "ANTLR With Java Target":
2012-05-20 19:05:29 -07:00
lexers.remove(l)
2019-01-23 15:52:55 -08:00
lexers.append(("Markdown", ("markdown",)))
2012-05-15 17:10:50 -07:00
lexers = sorted(lexers)
2012-05-20 19:05:29 -07:00
2012-05-15 17:10:50 -07:00
return lexers
2012-02-12 21:42:25 -08:00
2019-01-23 15:52:55 -08:00
2012-02-12 21:42:25 -08:00
user_registered.connect(activate_user)