Skip to content

Commit

Permalink
Merge branch 'sync_command'
Browse files Browse the repository at this point in the history
  • Loading branch information
mgaitan committed Sep 25, 2014
2 parents 590dd7c + a11ff55 commit 8e2b864
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
6 changes: 6 additions & 0 deletions waliki/_markups.py
Expand Up @@ -98,3 +98,9 @@ def find_markup_class_by_name(name):
for markup in (ReStructuredTextMarkup, MarkdownMarkup, TextileMarkup):
if markup.name.lower() == name.lower():
return markup


def find_markup_class_by_extension(extension):
for markup in (ReStructuredTextMarkup, MarkdownMarkup, TextileMarkup):
if extension.lower() in markup.file_extensions:
return markup
Empty file added waliki/management/__init__.py
Empty file.
Empty file.
39 changes: 39 additions & 0 deletions waliki/management/commands/sync_waliki.py
@@ -0,0 +1,39 @@
import os
from optparse import make_option
from django.core.management.base import BaseCommand # CommandError
from waliki.settings import WALIKI_DATA_DIR
from waliki.models import Page


class Command(BaseCommand):
help = 'Syncronize pages between files and the database'

option_list = (
make_option('--extensions',
dest='extensions',
default=".rst, .md",
help="Look for files with this extensions, separated by comma. Default: '.rst, .md'"),
make_option('--ignored_dirs',
dest='ignored_dirs',
default=".git",
help="List of directories to ignore, separated by comman. Default: '.git'"),
) + BaseCommand.option_list

def handle(self, *args, **options):
extensions = [ext.strip() for ext in options['extensions'].split(',')]
ignored_dirs = [d.strip() for d in options['ignored_dirs'].split(',')]
for root, dirs, files in os.walk(WALIKI_DATA_DIR):
[dirs.remove(d) for d in ignored_dirs if d in dirs]
for filename in files:
if os.path.splitext(filename)[1] not in extensions:
continue
path = os.path.join(root.replace(WALIKI_DATA_DIR, ''), filename).strip('/')

if not Page.objects.filter(path=path).exists():
page = Page.from_path(path)
self.stdout.write('Created page %s for %s' % (page.get_absolute_url(), path))

for page in Page.objects.all():
if not os.path.exists(page.abspath):
self.stdout.write('Deleted page %s (missing %s)' % (page.get_absolute_url(), page.path))
page.delete()
15 changes: 15 additions & 0 deletions waliki/models.py
Expand Up @@ -4,11 +4,13 @@
from django.db.models import Q
from django.conf import settings
from django.core.urlresolvers import reverse
from django.utils.six import string_types
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import Permission, Group
from django.contrib.auth import get_user_model

from . import _markups
from .utils import get_slug
from .settings import WALIKI_DEFAULT_MARKUP, WALIKI_MARKUPS_SETTINGS, WALIKI_DATA_DIR


Expand Down Expand Up @@ -45,6 +47,19 @@ def save(self, *args, **kwargs):
self.path = self.slug + self._markup.file_extensions[0]
super(Page, self).save(*args, **kwargs)

@classmethod
def from_path(cls, path, markup=None):

filename, ext = os.path.splitext(path)
if markup and isinstance(markup, string_types):
markup = _markups.find_markup_class_by_name(markup)
else:
markup = _markups.find_markup_class_by_extension(ext)
page = Page(path=path, slug=get_slug(filename), markup=markup.name)
page.title = page._get_part('get_document_title')
page.save()
return page

@property
def raw(self):
filename = self.abspath
Expand Down

0 comments on commit 8e2b864

Please sign in to comment.