def ajax_lock(request, object_pk): model = request.GET.get("model", None) last_timestamp_field = request.GET.get("last_timestamp_field", None) timestamp = float(request.GET.get("timestamp", 0)) user = request.user app_label, name = model.split('.', 1) try: lock = Lock.objects.get(content_type__name=name, content_type__app_label=app_label, object_pk=object_pk) except Lock.DoesNotExist: lock = None ret = {} content_type = ContentType.objects.get(app_label=app_label, name=name) obj = content_type.get_object_for_this_type(pk=object_pk) # Claritick specifics if obj.client and (not obj.client in request.user.clients): raise PermissionDenied if lock: if lock.user == user: lock.save() ret = LOCK_STATUS_UPDATED elif lock.is_expired: lock.user = user # get lock ret = LOCK_STATUS_EXPIRED else: ret = LOCK_STATUS_LOCKED else: lock = Lock(content_object=obj, user=user, last_modif_field=last_timestamp_field) ret = LOCK_STATUS_EXPIRED if ret == LOCK_STATUS_EXPIRED: # check for update if last_timestamp_field and timestamp: last_timestamp = getattr(obj, last_timestamp_field) from_date = datetime.fromtimestamp(timestamp) if last_timestamp - from_date < timedelta(seconds=LOCK_DELTA_MODIFICATION): ret = LOCK_STATUS_CREATED # get lock now lock.save() data = '{"status": %s' % (unicode(ret),) if ret == LOCK_STATUS_LOCKED: data += ', "locker": "%s"' % (unicode(lock.user),) data += '}' return HttpResponse(data, content_type="application/json; charset=utf-8")
def edit(request, wiki): """ Edit a page """ page_locked = False # Check if a lock exists try: lock = Lock.objects.get(path=request.path) # Check if the lock exists since more than PAGE_EDIT_LOCK_DURATION seconds dt = datetime.datetime.utcnow().replace(tzinfo=utc) - lock.timestamp if dt.total_seconds() >= settings.PAGE_EDIT_LOCK_DURATION: # The lock has expired # Reset it to the current user lock.user = request.user lock.timestamp = datetime.datetime.utcnow().replace(tzinfo=utc) lock.save() else: page_locked = True except Lock.DoesNotExist: lock = Lock() lock.path = request.path lock.user = request.user lock.timestamp = datetime.datetime.utcnow().replace(tzinfo=utc) lock.save() w = get_object_or_404(Wiki, slug=wiki) repo = Repository(w.gitdir) path = _git_path(request, wiki) page_name = path if request.method == u'POST': form = EditPageForm(request.POST) if form.is_valid(): new_path = '-'.join(form.cleaned_data[u'path'].split(' ')) # dirty hack in order to get the correct $AUTHOR in # our commit message os.environ['GIT_AUTHOR_NAME'] = u'{0} {1}'.format( request.user.first_name, request.user.last_name).encode('utf-8') os.environ['GIT_AUTHOR_EMAIL'] = request.user.email os.environ['USERNAME'] = str(request.user.username) commit = form.cleaned_data[u'comment'] or None repo.set_content(new_path, form.cleaned_data[u'content'], commit_msg=commit) # send email notification notify(repo, wiki) del (os.environ['GIT_AUTHOR_NAME']) del (os.environ['GIT_AUTHOR_EMAIL']) del (os.environ['USERNAME']) return redirect(u'{0}/{1}'.format(reverse(u'page', args=[wiki]), path)) else: if repo.exists(path) and not repo.is_dir(path): content, page_name = repo.get_content(path) form = EditPageForm({ u'path': path, u'content': content, u'comment': None }) else: form = EditPageForm() docs = Document.objects.filter(wikipath=u'{0}/{1}'.format(wiki, path)) data = { u'menu_url': reverse(u'tree', args=[wiki]), u'page_name': u'Edit: {0}'.format(page_name), u'page_locked': page_locked, u'attachements': { u'images': docs.filter(is_image=True), u'documents': docs.filter(is_image=False) }, u'edit_path': path, u'wiki': w, u'form': form, } if page_locked: data[u'lock'] = lock return render_to_response(u'edit.html', data, context_instance=RequestContext(request))
def edit(request, wiki): """ Edit a page """ page_locked = False # Check if a lock exists try: lock = Lock.objects.get(path=request.path) # Check if the lock exists since more than PAGE_EDIT_LOCK_DURATION seconds dt = datetime.datetime.utcnow().replace(tzinfo=utc) - lock.timestamp if dt.total_seconds() >= settings.PAGE_EDIT_LOCK_DURATION: # The lock has expired # Reset it to the current user lock.user = request.user lock.timestamp = datetime.datetime.utcnow().replace(tzinfo=utc) lock.save() else: page_locked = True except Lock.DoesNotExist: lock = Lock() lock.path = request.path lock.user = request.user lock.timestamp = datetime.datetime.utcnow().replace(tzinfo=utc) lock.save() w = get_object_or_404(Wiki, slug=wiki) repo = Repository(w.gitdir) path = _git_path(request, wiki) page_name = path if request.method == u'POST': form = EditPageForm(request.POST) if form.is_valid(): new_path = '-'.join(form.cleaned_data[u'path'].split(' ')) # dirty hack in order to get the correct $AUTHOR in # our commit message os.environ['GIT_AUTHOR_NAME'] = u'{0} {1}'.format(request.user.first_name, request.user.last_name).encode('utf-8') os.environ['GIT_AUTHOR_EMAIL'] = request.user.email os.environ['USERNAME'] = str(request.user.username) commit = form.cleaned_data[u'comment'] or None repo.set_content(new_path, form.cleaned_data[u'content'], commit_msg=commit) # send email notification notify(repo, wiki) del(os.environ['GIT_AUTHOR_NAME']) del(os.environ['GIT_AUTHOR_EMAIL']) del(os.environ['USERNAME']) return redirect(u'{0}/{1}'.format(reverse(u'page', args=[wiki]), path)) else: if repo.exists(path) and not repo.is_dir(path): content, page_name = repo.get_content(path) form = EditPageForm({u'path': path, u'content': content, u'comment': None}) else: form = EditPageForm() docs = Document.objects.filter(wikipath=u'{0}/{1}'.format(wiki, path)) data = { u'menu_url': reverse(u'tree', args=[wiki]), u'page_name': u'Edit: {0}'.format(page_name), u'page_locked': page_locked, u'attachements': { u'images': docs.filter(is_image=True), u'documents': docs.filter(is_image=False) }, u'edit_path': path, u'wiki': w, u'form': form, } if page_locked: data[u'lock'] = lock return render_to_response(u'edit.html', data, context_instance=RequestContext(request))