~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to wiki/models.py

  • Committer: franku
  • Author(s): GunChleoc
  • Date: 2016-12-13 18:30:38 UTC
  • mfrom: (438.1.6 pyformat_util)
  • Revision ID: somal@arcor.de-20161213183038-5cgmvfh2fkgmoc1s
adding a script to run pyformat over the code base

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
# We dont need to create a new one everytime
31
31
dmp = diff_match_patch()
32
32
 
 
33
 
33
34
def diff(txt1, txt2):
34
35
    """Create a 'diff' from txt1 to txt2."""
35
36
    patch = dmp.patch_make(txt1, txt2)
47
48
 
48
49
 
49
50
class Article(models.Model):
50
 
    """ A wiki page.
51
 
    """
 
51
    """A wiki page."""
52
52
    title = models.CharField(_(u"Title"), max_length=50)
53
53
    content = models.TextField(_(u"Content"))
54
54
    summary = models.CharField(_(u"Summary"), max_length=150,
58
58
                              null=True, blank=True)
59
59
    creator = models.ForeignKey(User, verbose_name=_('Article Creator'),
60
60
                                null=True)
61
 
    creator_ip = models.GenericIPAddressField(_("IP Address of the Article Creator"),
62
 
                                       blank=True, null=True)
 
61
    creator_ip = models.GenericIPAddressField(_('IP Address of the Article Creator'),
 
62
                                              blank=True, null=True)
63
63
    created_at = models.DateTimeField(default=datetime.now)
64
64
    last_update = models.DateTimeField(blank=True, null=True)
65
65
 
71
71
 
72
72
    tags = TagField()
73
73
 
74
 
    # Django sphinx 
 
74
    # Django sphinx
75
75
    if settings.USE_SPHINX:
76
76
        search = SphinxSearch(
77
 
            weights = {
 
77
            weights={
78
78
                'title': 100,
79
79
                'summary': 80,
80
80
                'content': 50,
81
 
                }
82
 
            )
 
81
            }
 
82
        )
83
83
 
84
84
    class Meta:
85
85
        verbose_name = _(u'Article')
101
101
                reverted=False).order_by('-revision')[0]
102
102
        except IndexError:
103
103
            return ChangeSet.objects.none()
104
 
    
 
104
 
105
105
    def all_images(self):
106
106
        return self.images.all()
107
107
 
108
108
    def new_revision(self, old_content, old_title, old_markup,
109
109
                     comment, editor_ip, editor):
110
 
        '''Create a new ChangeSet with the old content.'''
 
110
        """Create a new ChangeSet with the old content."""
111
111
 
112
112
        content_diff = diff(self.content, old_content)
113
113
 
123
123
        return cs
124
124
 
125
125
    def revert_to(self, revision, editor_ip, editor=None):
126
 
        """ Revert the article to a previuos state, by revision number.
127
 
        """
 
126
        """Revert the article to a previuos state, by revision number."""
128
127
        changeset = self.changeset_set.get(revision=revision)
129
128
        changeset.reapply(editor_ip, editor)
130
129
 
131
130
    def compare(self, from_revision, to_revision):
132
 
        """ Compares to revisions of this article.
133
 
        """
 
131
        """Compares to revisions of this article."""
134
132
        changeset = self.changeset_set.get(revision=to_revision)
135
133
        return changeset.compare_to(from_revision)
136
134
 
138
136
        return self.title
139
137
 
140
138
 
141
 
 
142
139
class ChangeSetManager(models.Manager):
143
140
 
144
141
    def all_later(self, revision):
145
 
        """ Return all changes later to the given revision.
 
142
        """Return all changes later to the given revision.
 
143
 
146
144
        Util when we want to revert to the given revision.
 
145
 
147
146
        """
148
147
        return self.filter(revision__gt=int(revision))
149
148
 
185
184
    class Meta:
186
185
        verbose_name = _(u'Change set')
187
186
        verbose_name_plural = _(u'Change sets')
188
 
        get_latest_by  = 'modified'
 
187
        get_latest_by = 'modified'
189
188
        ordering = ('-revision',)
190
 
        app_label = 'wiki'
 
189
        app_label = 'wiki'
191
190
 
192
191
    def __unicode__(self):
193
192
        return u'#%s' % self.revision
203
202
                 'title': self.article.title,
204
203
                 'revision': self.revision})
205
204
 
206
 
 
207
205
    def is_anonymous_change(self):
208
206
        return self.editor is None
209
207
 
210
208
    def reapply(self, editor_ip, editor):
211
 
        """ Return the Article to this revision.
212
 
        """
 
209
        """Return the Article to this revision."""
213
210
 
214
211
        # XXX Would be better to exclude reverted revisions
215
212
        #     and revisions previous/next to reverted ones
240
237
        article.new_revision(
241
238
            old_content=old_content, old_title=old_title,
242
239
            old_markup=old_markup,
243
 
            comment="Reverted to revision #%s" % self.revision,
 
240
            comment='Reverted to revision #%s' % self.revision,
244
241
            editor_ip=editor_ip, editor=editor)
245
242
 
246
243
        self.save()
247
244
 
248
245
        if None not in (notification, self.editor):
249
 
            notification.send([self.editor], "wiki_revision_reverted",
 
246
            notification.send([self.editor], 'wiki_revision_reverted',
250
247
                              {'revision': self, 'article': self.article})
251
248
 
252
249
    def save(self, *args, **kwargs):
253
 
        """ Saves the article with a new revision.
254
 
        """
 
250
        """Saves the article with a new revision."""
255
251
        if self.id is None:
256
252
            try:
257
253
                self.revision = ChangeSet.objects.filter(
261
257
        super(ChangeSet, self).save(*args, **kwargs)
262
258
 
263
259
    def display_diff(self):
264
 
        ''' Returns a HTML representation of the diff.
265
 
        '''
 
260
        """Returns a HTML representation of the diff."""
266
261
 
267
262
        # well, it *will* be the old content
268
263
        old_content = self.article.content
275
270
        # apply all patches to get the content of this revision
276
271
        for i, changeset in enumerate(newer_changesets):
277
272
            patches = dmp.patch_fromText(changeset.content_diff)
278
 
            if len(newer_changesets) == i+1:
 
273
            if len(newer_changesets) == i + 1:
279
274
                # we need to compare with the next revision after the change
280
275
                next_rev_content = old_content
281
276
            old_content = dmp.patch_apply(patches, old_content)[0]
285
280
        return dmp.diff_prettyHtml(diffs)
286
281
 
287
282
    def get_content(self):
288
 
        """ Returns the content of this revision.
289
 
        """
 
283
        """Returns the content of this revision."""
290
284
        content = self.article.content
291
 
        newer_changesets = ChangeSet.objects.filter(article=self.article, revision__gt=self.revision).order_by('-revision')
 
285
        newer_changesets = ChangeSet.objects.filter(
 
286
            article=self.article, revision__gt=self.revision).order_by('-revision')
292
287
        for changeset in newer_changesets:
293
288
            patches = dmp.patch_fromText(changeset.content_diff)
294
289
            content = dmp.patch_apply(patches, content)[0]
295
290
        return content
296
 
        
 
291
 
297
292
    def compare_to(self, revision_from):
298
293
        other_content = u""
299
294
        if revision_from > 0:
300
 
            other_content = ChangeSet.objects.filter(article=self.article, revision__lte=revision_from).order_by("-revision")[0].get_content()
 
295
            other_content = ChangeSet.objects.filter(
 
296
                article=self.article, revision__lte=revision_from).order_by('-revision')[0].get_content()
301
297
        diffs = dmp.diff_main(other_content, self.get_content())
302
298
        dmp.diff_cleanupSemantic(diffs)
303
299
        return dmp.diff_prettyHtml(diffs)