Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mgaitan / waliki / 360

6 Mar 2018 - 9:26 coverage: 75.0%. Remained the same
360

Pull #151

travis-ci

9181eb84f9c35729a3bad740fb7f9d93?size=18&default=identiconweb-flow
Fixed attachment issue in sync_waliki

As in 4554a9d87 : filename param was removed from model
Pull Request #151: Fixed attachment issue in sync_waliki

0 of 1 new or added line in 1 file covered. (0.0%)

1218 of 1624 relevant lines covered (75.0%)

12.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.51
/waliki/management/commands/sync_waliki.py
1
import os
17×
2
from optparse import make_option
17×
3
from django.conf import settings
17×
4
from django.core.management.base import BaseCommand
17×
5
from django.core.files import File
17×
6
from waliki.settings import WALIKI_DATA_DIR, WALIKI_UPLOAD_TO
17×
7
from waliki.models import Page
17×
8
if 'waliki.attachments' in settings.INSTALLED_APPS:
17×
9
    from waliki.attachments.models import Attachment
17×
10
else:
11
    Attachment = None
!
12

13

14
class Command(BaseCommand):
17×
15
    help = """Syncronize pages (and attachments) between files and the database"""
17×
16

17
    def add_arguments(self, parser):
17×
18
        parser.add_argument(
17×
19
            '--extensions',
20
            default=['.rst', '.md'],
21
            nargs='+',
22
            help="Look for files with this extensions"
23
        )
24
        parser.add_argument(
17×
25
            '--ignored_dirs',
26
            default=['.git'],
27
            nargs='+',
28
            help="List of directories to ignore"
29
        )
30

31
    def handle(self, *args, **options):
17×
32
        extensions = options['extensions']
17×
33
        ignored_dirs = options['ignored_dirs']
17×
34
        for root, dirs, files in os.walk(WALIKI_DATA_DIR):
17×
35
            [dirs.remove(d) for d in ignored_dirs if d in dirs]
17×
36
            for filename in files:
17×
37
                if os.path.splitext(filename)[1] not in extensions:
17×
38
                    continue
!
39
                path = os.path.join(root.replace(WALIKI_DATA_DIR, ''), filename).strip('/')
17×
40

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

45
        # Deleted pages?
46
        for page in Page.objects.all():
17×
47
            if not os.path.exists(page.abspath):
17×
48
                self.stdout.write('Deleted page %s (missing %s)' % (page.get_absolute_url(), page.path))
17×
49
                page.delete()
17×
50

51
        if Attachment:
17×
52
            class FakeAttachment(object):
17×
53
                def __init__(self, page):
17×
54
                    self.page = page
17×
55

56
            for page in Page.objects.all():
17×
57
                path = os.path.join(settings.MEDIA_ROOT, WALIKI_UPLOAD_TO(FakeAttachment(page), ''))
17×
58
                if not os.path.exists(path):
17×
59
                    continue
17×
60
                for filename in os.listdir(path):
!
61
                    if not os.path.isfile(os.path.join(path, filename)):
!
62
                        continue
!
63
                    file = WALIKI_UPLOAD_TO(FakeAttachment(page), filename)
!
64
                    if page.attachments.filter(file=file):
!
65
                        continue
!
NEW
66
                    attachment = Attachment.objects.create(page=page, file=file)
!
67
                    self.stdout.write('Created attachment %s for %s' % (attachment, page.slug))
!
68

69
            for attachment in Attachment.objects.all():
17×
70
                if not os.path.exists(os.path.join(settings.MEDIA_ROOT, attachment.file.name)):
!
71
                    self.stdout.write('Missing %s from %s. Deleted attachment object' % (attachment, attachment.page.slug))
!
72
                    attachment.delete()
!
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2023 Coveralls, Inc