← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~widelands-dev/widelands-website/reenable_wiki_edit_lock into lp:widelands-website

 

kaputtnik has proposed merging lp:~widelands-dev/widelands-website/reenable_wiki_edit_lock into lp:widelands-website.

Commit message:
Reenable the wiki edit lock

Requested reviews:
  Widelands Developers (widelands-dev)

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands-website/reenable_wiki_edit_lock/+merge/361388

A leftover from the update to Django 1.8:

Show a message on top of the page, if another user is editing the same Wikiarticle. The message is shown to the user who wants to start editing after another user has already started editing it.
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands-website/reenable_wiki_edit_lock into lp:widelands-website.
=== modified file 'README.txt'
--- README.txt	2018-11-26 17:39:05 +0000
+++ README.txt	2019-01-04 15:37:11 +0000
@@ -77,6 +77,7 @@
 Now creating the tables in the database:
 
    $ ./manage.py migrate
+   $ ./manage.py createcachetable
 
 Create a superuser:
 

=== modified file 'settings.py'
--- settings.py	2018-12-18 11:38:34 +0000
+++ settings.py	2019-01-04 15:37:11 +0000
@@ -346,6 +346,19 @@
 STAR_RATINGS_STAR_WIDTH = 14
 STAR_RATINGS_RANGE = 10
 
+###############
+# Set a cache #
+###############
+# See https://docs.djangoproject.com/en/1.11/topics/cache/
+# The cache is used for 'Online users' and the wiki edit lock
+
+CACHES = {
+    'default': {
+        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
+        'LOCATION': 'wl_cache',
+    }
+}
+
 try:
     from local_settings import *
 except ImportError:

=== modified file 'templates/wiki/edit.html'
--- templates/wiki/edit.html	2018-11-22 11:08:51 +0000
+++ templates/wiki/edit.html	2019-01-04 15:37:11 +0000
@@ -47,6 +47,11 @@
 
 {% block content_main %}
 <div class="blogEntry">
+	{% if messages %}
+	    {% for message in messages %}
+		    <p class="errormessage">{{ message }}</p>
+	    {% endfor %}
+	{% endif %}
 	<p>
 	You can edit the wiki pages contents using the syntax described on the <a href="/wiki/WikiSyntax">WikiSyntax</a>.
 	</p>

=== modified file 'wiki/admin.py'
--- wiki/admin.py	2018-09-18 06:42:18 +0000
+++ wiki/admin.py	2019-01-04 15:37:11 +0000
@@ -17,7 +17,7 @@
     # Do not show 'Action' to prevent deleting:
     actions = None
     search_fields = ['title']
-    list_display = ('title', 'creator', 'last_update',)
+    list_display = ('title', 'creator', 'last_update')
     list_filter = ('title',)
     ordering = ['-last_update']
     fieldsets = (

=== modified file 'wiki/views.py'
--- wiki/views.py	2018-10-14 19:42:00 +0000
+++ wiki/views.py	2019-01-04 15:37:11 +0000
@@ -11,6 +11,7 @@
 from django.shortcuts import get_object_or_404, render, redirect
 from django.contrib.contenttypes.models import ContentType
 from django.contrib import messages
+
 from wiki.forms import ArticleForm
 from wiki.models import Article, ChangeSet, dmp
 
@@ -20,6 +21,8 @@
 from markdownextensions.semanticwikilinks.mdx_semanticwikilinks import WIKILINK_RE
 
 from wl_utils import get_real_ip
+from wl_utils import get_valid_cache_key
+
 import re
 import urllib
 
@@ -70,33 +73,29 @@
 
 class ArticleEditLock(object):
     """A soft lock to edting an article."""
-    # TODO(Franku): This Class is currently not used
-    # If we want this it has to be checked for the changes
-    # related to django 1.8.
-    # A javascript alert box is maybe a better solution
     def __init__(self, title, request, message_template=None):
         self.title = title
-        self.user_ip = get_real_ip(request)
-        self.created_at = datetime.now()
+        self.user = request.user
+        self.created_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
 
         if message_template is None:
             message_template = ('Possible edit conflict:'
-                                ' another user started editing this article at %s')
+                                ' Another user started editing this article at %s')
 
         self.message_template = message_template
-
         cache.set(title, self, WIKI_LOCK_DURATION * 60)
 
     def create_message(self, request):
-        """Send a message to the user if there is another user editing this
+        """Show a message to the user if there is another user editing this
         article."""
         if not self.is_mine(request):
             user = request.user
-            user.message_set.create(
-                message=self.message_template % self.created_at)
+            messages.add_message(request,
+                                 messages.INFO,
+                                 self.message_template % self.created_at)
 
     def is_mine(self, request):
-        return self.user_ip == get_real_ip(request)
+        return self.user == request.user
 
 
 def has_read_perm(user, group, is_member, is_private):
@@ -284,6 +283,11 @@
                 form.group = group
 
             new_article, changeset = form.save()
+
+            lock = cache.get(get_valid_cache_key(title))
+            if lock is not None:
+                # Clean the lock
+                cache.delete(get_valid_cache_key(title))
             
             if notification and not changeset.reverted:
                 # Get observers for this article and exclude current editor
@@ -300,12 +304,10 @@
             return redirect(new_article)
 
     elif request.method == 'GET':
-
-        # TODO(Franku): Never worked IMHO
-        # lock = cache.get(title, None)
-        # if lock is None:
-        #     lock = ArticleEditLock(title, request)
-        # lock.create_message(request)
+        lock = cache.get(get_valid_cache_key(title))
+        if lock is None:
+            lock = ArticleEditLock(get_valid_cache_key(title), request)
+        lock.create_message(request)
         initial = {}
         if group_slug is not None:
             initial.update({'content_type': group_ct.id,

=== modified file 'wl_utils.py'
--- wl_utils.py	2018-04-07 09:48:02 +0000
+++ wl_utils.py	2019-01-04 15:37:11 +0000
@@ -50,3 +50,10 @@
     def contribute_to_related_class(self, cls, related):
         setattr(cls, related.get_accessor_name(),
                 AutoReverseOneToOneDescriptor(related))
+
+
+# Memory based cache does not allow whitespace or control characters in keys
+# We are using a database cache atm, so this is just a forethought to
+# prevent failures when switching to a memory based cache
+def get_valid_cache_key(key):
+    return key.replace(' ', '_')


Follow ups