/
utils.py
50 lines (43 loc) · 1.61 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
from django.db.models.signals import pre_save
from django.template.defaultfilters import slugify
from dulwich.client import get_transport_and_path
from dulwich.repo import Repo
import os
import shutil
import sys
class Slugifier(object):
def __init__(self, model, target, source):
self.model = model
self.target = target
self.source = source
field = self.model._meta.get_field_by_name(self.target)[0]
self.max_length = field.max_length
def __call__(self, instance, **kwargs):
if getattr(instance, self.target):
return
slug = slugify(getattr(instance, self.source))[:self.max_length]
current = slug
index = 0
while self.model.objects.filter(**{self.target: current}).exists():
suffix = '-%s' % index
current = '%s%s' % (slug[:-len(suffix)], suffix)
index += 1
setattr(instance, self.target, current)
def auto_slug(model, target, source):
"""
class MyModel(models.Model):
name = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, null=True)
auto_slug(MyModel, 'slug', 'name')
"""
pre_save.connect(Slugifier(model, target, source), sender=model, weak=False)
def update_git(repourl, repodir):
client, host_path = get_transport_and_path(repourl)
# todo: make fetch work
if os.path.exists(repodir):
shutil.rmtree(repodir)
repo = Repo.init(repodir, mkdir=True)
remote_refs = client.fetch(host_path, repo,
progress=sys.stdout.write)
repo["HEAD"] = remote_refs["HEAD"]