views.py 20.6 KB
Newer Older
1 2 3 4 5 6
# -*- coding: utf-8 -*-

# Python imports
import datetime
import time
import urllib
7 8
from urllib.parse import urlparse
from html.parser import HTMLParser
9
import json
10
import re
11 12 13 14
import os

# PyPi imports

alexandre's avatar
alexandre committed
15
import markdown
gijs's avatar
gijs committed
16
from markdown.extensions.toc import TocExtension
17
from py_etherpad import EtherpadLiteClient
18
import dateutil.parser
eric's avatar
eric committed
19
import pytz
20 21

# Framework imports
22
from django.shortcuts import render, get_object_or_404
23

24
from django.http import HttpResponse, HttpResponseRedirect
25
from django.template import RequestContext
26
from django.template.defaultfilters import slugify
27 28
from django.urls import reverse
from django.template.context_processors import csrf
29
from django.core.exceptions import PermissionDenied
30 31
from django.contrib.auth.decorators import login_required
from django.utils.translation import ugettext_lazy as _
gijs's avatar
gijs committed
32
from django.db import IntegrityError
gijs's avatar
gijs committed
33
from django.core.paginator import Paginator
34

35
# Django Apps import
36 37 38 39

from etherpadlite.models import *
from etherpadlite import forms
from etherpadlite import config
40
from django.contrib.sites.shortcuts import get_current_site
41

42
from ethertoff.management.commands.index import snif
gijs's avatar
gijs committed
43
from ethertoff.templatetags.wikify import wikifyPath, ensureTrailingSlash
44

45 46
# By default, the homepage is the pad called ‘start’ (props to DokuWiki!)
try:
47
    from ethertoff.settings import HOME_PAD
48
except ImportError:
49
    HOME_PAD = 'About.md'
50
try:
51
    from ethertoff.settings import BACKUP_DIR
52 53
except ImportError:
    BACKUP_DIR = None
54

gijs's avatar
gijs committed
55 56
from ethertoff.settings import PAD_FORCE_EXTENSION, PAD_ALLOWED_EXTENSIONS, PAD_DEFAULT_EXTENSION, PADS_PER_PAGE, PAD_NAMESPACE_SEPARATOR

57 58 59 60 61 62
"""
Set up an HTMLParser for the sole purpose of unescaping
Etherpad’s HTML entities.
cf http://fredericiana.com/2010/10/08/decoding-html-entities-to-text-in-python/
"""

63
h = HTMLParser()
64 65
unescape = h.unescape

66 67 68 69 70
"""
Create a regex for our include template tag
"""
include_regex = re.compile("{%\s?include\s?\"([\w._-]+)\"\s?%}")

gijs's avatar
gijs committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
def makeLeaf ():
    return { 'folders': {}, 'pads': [] }

def insertAt (path=[], tree=[], pad=''):
    if len(path) > 1:
        key = path.pop(0)
        if not key in tree['folders']:
            tree['folders'][key] = makeLeaf()

        tree['folders'][key] = insertAt(path, tree['folders'][key], pad)
    else:
        tree['pads'].append(pad)

    return tree

def insertPad (pad, tree):
    if PAD_NAMESPACE_SEPARATOR in pad.display_slug:
        path = pad.display_slug.split(PAD_NAMESPACE_SEPARATOR)
    else:
        path = []
    
    return insertAt(path, tree, pad)
gijs's avatar
gijs committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

# Perhaps move to the model?
def makePadPublic (pad, n=0):
    if not pad.is_public:
        epclient = EtherpadLiteClient(pad.server.apikey, pad.server.apiurl)
        tail = '' if n == 0 else '-{}'.format(n)
        publicid = pad.name+tail

        try:
            res = epclient.sendClientsMessage(pad.padid, "Please continue editing in the publicversion of this pad")
            print(res)
            res = epclient.copyPad(pad.padid, publicid)
            pad.is_public = True
            pad.publicpadid = publicid
            pad.save()

            return pad

        except ValueError:
            return makePadPublic(pad, n+1)

def makePadPrivate(pad):
    if pad.is_public:
        epclient = EtherpadLiteClient(pad.server.apikey, pad.server.apiurl)
        res = epclient.movePad(pad.publicpadid, pad.padid, force=True)

        pad.is_public = False
        pad.publicpadid = ''
        pad.save()
        return pad
gijs's avatar
gijs committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

# FIXME: better name
def treatPadName (name, n=0):
    name, ext = os.path.splitext(name)

    if n > 0:
        name = '{}-{}'.format(name, n)

    if PAD_FORCE_EXTENSION:
        if ext not in PAD_ALLOWED_EXTENSIONS:
            ext = PAD_DEFAULT_EXTENSION

        name = '{}{}'.format(name, ext)
    
    return name
gijs's avatar
gijs committed
138 139 140 141

def createPad (slug, server, group, n=0):
    if n < 25:
        try:
gijs's avatar
gijs committed
142
            safe_slug = treatPadName(slug, n)
gijs's avatar
gijs committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
            pad = Pad(
                name=slugify(safe_slug)[:42], # This is the slug internally used by etherpad
                display_slug=safe_slug, # This is the slug we get to change afterwards
                display_name=safe_slug,     # this is just for backwards compatibility
                server=group.server,
                group=group
            )

            pad.save()
            return pad
        except IntegrityError:
            return createPad(slug=slug, server=server, group=group, n=n+1)

    return False

eric's avatar
eric committed
158
@login_required(login_url='/accounts/login')
gijs's avatar
gijs committed
159
def padCreate(request, prefix=''):
160
    """
161
    Create a pad
162
    """    
163 164 165 166 167
    
    # normally the ‘pads’ context processor should have made sure that these objects exist:
    author = PadAuthor.objects.get(user=request.user)
    group = author.group.all()[0]
    
168 169 170
    if request.method == 'POST':  # Process the form
        form = forms.PadCreate(request.POST)
        if form.is_valid():
gijs's avatar
gijs committed
171 172
            slug = re.sub(r'\s+', '_', form.cleaned_data['name'])
            pad = createPad(slug=slug, server=group.server, group=group)
gijs's avatar
gijs committed
173

eric's avatar
eric committed
174
            return HttpResponseRedirect(reverse('pad-write', args=(pad.display_slug,) ))
175
    else:  # No form to process so create a fresh one
176
        form = forms.PadCreate({'group': group.groupID, 'name': wikifyPath(ensureTrailingSlash(prefix) if prefix else '')})
177 178 179

    con = {
        'form': form,
180
        'pk': group.pk,
181
        'title': _('Create pad in %(grp)s') % {'grp': group}
182 183
    }
    con.update(csrf(request))
184 185
    return render(
        request,
eric's avatar
eric committed
186
        'pad-create.html',
187 188 189 190
        con,
    )


gijs's avatar
gijs committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
@login_required(login_url='/etherpad')
def padDelete(request, pk):
    """Delete a given pad
    """
    pad = get_object_or_404(Pad, pk=pk)

    # Any form submissions will send us back to the profile
    if request.method == 'POST':
        if 'confirm' in request.POST:
            pad.delete()
        return HttpResponseRedirect('/manage/')

    con = {
        'action': reverse('pad-delete', kwargs={'pk': pk}),
        'question': _('Really delete the pad {}?'.format(str(pad))),
        'title': _('Deleting {}'.format(str(pad))),
gijs's avatar
gijs committed
207
        'label': _('Delete')
gijs's avatar
gijs committed
208 209 210 211 212 213 214 215
    }
    con.update(csrf(request))
    return render(
        request,
        'pads/confirm.html',
        con
    )

gijs's avatar
gijs committed
216 217 218
@login_required(login_url='/etherpad')
def padPublic(request, pk):
    """Delete a given pad
gijs's avatar
gijs committed
219
    """
gijs's avatar
gijs committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    pad = get_object_or_404(Pad, pk=pk)

    # Any form submissions will send us back to the profile
    if request.method == 'POST':
        if 'confirm' in request.POST:
            makePadPublic(pad)
        return HttpResponseRedirect('/manage/')

    con = {
        'action': reverse('pad-public', kwargs={'pk': pk}),
        'question': _('Really make {} public?'.format(str(pad))),
        'title': _('Making {} public'.format(str(pad))),
        'label': _('Make public')
    }
    con.update(csrf(request))
    return render(
        request,
        'pads/confirm.html',
        con
    )


@login_required(login_url='/etherpad')
def padPrivate(request, pk):
    """Delete a given pad
245
    """
gijs's avatar
gijs committed
246
    pad = get_object_or_404(Pad, pk=pk)
247

gijs's avatar
gijs committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    # Any form submissions will send us back to the profile
    if request.method == 'POST':
        if 'confirm' in request.POST:
            makePadPrivate(pad)
        return HttpResponseRedirect('/manage/')

    con = {
        'action': reverse('pad-private', kwargs={'pk': pk}),
        'question': _('Really make {} private?'.format(str(pad))),
        'title': _('Making {} private'.format(str(pad))),
        'label': _('Make private')
    }
    con.update(csrf(request))
    return render(
        request,
        'pads/confirm.html',
        con
    )

def pad(request, pk=None, slug=None):
268
    if slug:
269
        pad = get_object_or_404(Pad, display_slug=slug)
270 271
    else:
        pad = get_object_or_404(Pad, pk=pk)
gijs's avatar
gijs committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308

    if pad.is_public:
        return pad_write_public(request, pad)
    else:
        return pad_write(request, pad)

def pad_write_public(request, pad): # pad_write
    padLink = pad.server.url + 'p/' + pad.publicpadid
    server = urlparse(pad.server.url)
    
    if request.user.is_authenticated:
        author = PadAuthor.objects.get(user=request.user)
        uname = str(author.user)
    else:
        uname = None

    # Set up the response
    return render(
        request,
        'pad-public.html',
        {
            'pad': pad,
            'link': padLink,
            'server': server,
            'error': False,
            'mode' : 'write-public',
            'uname': None
        },
    )

@login_required(login_url='/accounts/login')
def pad_write(request, pad):
    # pad_write
    
    """
     Create and session and display an embedded pad
    """
gijs's avatar
gijs committed
309
    padLink = pad.server.url + 'p/' + pad.group.groupID + '$' + \
310
        urllib.parse.quote(pad.name)
311 312 313 314
    server = urlparse(pad.server.url)
    author = PadAuthor.objects.get(user=request.user)

    if author not in pad.group.authors.all():
315 316
        response = render(
            request,
eric's avatar
eric committed
317
            'pad.html',
318 319 320 321
            {
                'pad': pad,
                'link': padLink,
                'server': server,
322
                'uname': "{}".format(author.user),
323
                'error': _('You are not allowed to view or edit this pad')
gijs's avatar
gijs committed
324 325
            },
            context_instance=RequestContext(request)
326 327 328 329 330 331 332 333 334
        )
        return response

    # Create the session on the etherpad-lite side
    expires = datetime.datetime.utcnow() + datetime.timedelta(
        seconds=config.SESSION_LENGTH
    )
    epclient = EtherpadLiteClient(pad.server.apikey, pad.server.apiurl)

gijs's avatar
gijs committed
335 336 337
    # Try to use existing session as to allow editing multiple pads at once
    newSessionID = False

338
    try:
gijs's avatar
gijs committed
339 340 341 342 343 344 345 346
        if not 'sessionID' in request.COOKIES:
            newSessionID = True

            result = epclient.createSession(
                pad.group.groupID,
                author.authorID,
                time.mktime(expires.timetuple()).__str__()
            )
347
    except Exception as e:
gijs's avatar
gijs committed
348
        response =  render(
349
            request,
eric's avatar
eric committed
350
            'pad.html',
351 352 353 354
            {
                'pad': pad,
                'link': padLink,
                'server': server,
355
                'uname': "{}".format(author.user),
356
                'error': _('etherpad-lite session request returned:') +
357
                ' "' + e.reason if isinstance(e, UnicodeError) else str(e) + '"'
358
            }
359 360 361 362
        )
        return response

    # Set up the response
363 364
    response = render(
        request,
eric's avatar
eric committed
365
        'pad.html',
366 367 368 369
        {
            'pad': pad,
            'link': padLink,
            'server': server,
370
            'uname': "{}".format(author.user),
371 372
            'error': False,
            'mode' : 'write'
gijs's avatar
gijs committed
373
        },
374 375
    )

gijs's avatar
gijs committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
    if newSessionID:
        # Delete the existing session first
        if ('padSessionID' in request.COOKIES):
            if 'sessionID' in request.COOKIES.keys():
                try:
                    epclient.deleteSession(request.COOKIES['sessionID'])
                except ValueError:
                    response.delete_cookie('sessionID', server.hostname)
            response.delete_cookie('padSessionID')

        # Set the new session cookie for both the server and the local site
        response.set_cookie(
            'sessionID',
            value=result['sessionID'],
            expires=expires,
            domain=server.hostname,
            httponly=False
        )
        response.set_cookie(
            'padSessionID',
            value=result['sessionID'],
            expires=expires,
            httponly=False
        )
        
401
    return response
402

403 404
def xhtml(request, slug):
    return pad_read(request, "r", slug + '.md')
405

406
def pad_read(request, mode="r", slug=None):
407 408
    """Read only pad
    """
409 410 411
    
    # FIND OUT WHERE WE ARE,
    # then get previous and next
eric's avatar
eric committed
412 413 414 415 416
    try:
        articles = json.load(open(os.path.join(BACKUP_DIR, 'index.json')))
    except IOError:
        articles = []
    
417
    SITE = get_current_site(request)
gijs's avatar
gijs committed
418 419
    # FIXME: construct url based on settings?
    #href = "http://%s" % SITE.domain + request.path
420
    
gijs's avatar
gijs committed
421 422
    href = request.path

423 424
    prev = None
    next = None
425 426
    for i, article in enumerate(articles):
        if article['href'] == href:
427
            if i != 0:        # The first is the most recent article, there is no newer
428
                next = articles[i-1]
429
            if i != len(articles) - 1:
430
                prev = articles[i+1]
431 432

    # Initialize some needed values
433
    pad = get_object_or_404(Pad, display_slug=slug)
434

gijs's avatar
gijs committed
435
    padID = pad.publicpadid if pad.is_public else pad.group.groupID + '$' + urllib.parse.quote(pad.name.replace('::', '_'))
436
    epclient = EtherpadLiteClient(pad.server.apikey, pad.server.apiurl)
437

438 439 440 441
    # Etherpad gives us authorIDs in the form ['a.5hBzfuNdqX6gQhgz', 'a.tLCCEnNVJ5aXkyVI']
    # We link them to the Django users DjangoEtherpadLite created for us
    authorIDs = epclient.listAuthorsOfPad(padID)['authorIDs']
    authors = PadAuthor.objects.filter(authorID__in=authorIDs)
442

443 444 445 446 447
    authorship_authors = []
    for author in authors:
        authorship_authors.append({ 'name'  : author.user.first_name if author.user.first_name else author.user.username,
                                    'class' : 'author' + author.authorID.replace('.','_') })
    authorship_authors_json = json.dumps(authorship_authors, indent=2)
448

449
    name, extension = os.path.splitext(slug)
450 451 452

    meta = {}

453 454 455 456 457 458 459 460 461 462 463
    if not extension:
        # Etherpad has a quasi-WYSIWYG functionality.
        # Though is not alwasy dependable
        text = epclient.getHtml(padID)['html']
        # Quick and dirty hack to allow HTML in pads
        text = unescape(text)
    else:
        # If a pad is named something.css, something.html, something.md etcetera,
        # we don’t want Etherpads automatically generated HTML, we want plain text.
        text = epclient.getText(padID)['text']
        if extension in ['.md', '.markdown']:
gijs's avatar
gijs committed
464 465
            md = markdown.Markdown(extensions=['extra', 'meta', TocExtension(baselevel=2), 'attr_list'])
            text = md.convert(text)
466 467 468 469
            try:
                meta = md.Meta
            except AttributeError:   # Edge-case: this happens when the pad is completely empty
                meta = None
470 471 472 473
    
    # Convert the {% include %} tags into a form easily digestible by jquery
    # {% include "example.html" %} -> <a id="include-example.html" class="include" href="/r/include-example.html">include-example.html</a>
    def ret(matchobj):
474
        return '<a id="include-%s" class="include pad-%s" href="%s">%s</a>' % (slugify(matchobj.group(1)), slugify(matchobj.group(1)), reverse('pad-read', args=("r", matchobj.group(1)) ), matchobj.group(1))
475 476 477 478
    
    text = include_regex.sub(ret, text)
    
    
479
    # Create namespaces from the url of the pad
480 481
    # 'pedagogy::methodology' -> ['pedagogy', 'methodology']
    namespaces = [p.rstrip('-') for p in pad.display_slug.split('::')]
482

483
    meta_list = []
484

485 486
    # One needs to set the ‘Static’ metadata to ‘Public’ for the page to be accessible to outside visitors
    if not meta or not 'status' in meta or not meta['status'][0] or not meta['status'][0].lower() in ['public']:
487
        if not request.user.is_authenticated:
eric's avatar
eric committed
488
            pass #raise PermissionDenied
489 490
    
    if meta and len(meta.keys()) > 0:
491 492
        
        # The human-readable date is parsed so we can sort all the articles
493
        if 'date' in meta:
eric's avatar
eric committed
494
            meta['date_iso'] = []
495 496
            meta['date_parsed'] = []
            for date in meta['date']:
eric's avatar
eric committed
497 498 499 500 501 502
                date_parsed = dateutil.parser.parse(date)
                # If there is no timezone we assume it is in Brussels:
                if not date_parsed.tzinfo:
                    date_parsed = pytz.timezone('Europe/Brussels').localize(date_parsed) 
                meta['date_parsed'].append(date_parsed)
                meta['date_iso'].append( date_parsed.isoformat() )
503 504 505 506

        meta_list = list(meta.items())

        print(meta_list)
507

508
    tpl_params = { 'pad'                : pad,
eric's avatar
eric committed
509 510
                   'meta'               : meta,      # to access by hash, like meta.author
                   'meta_list'          : meta_list, # to access all meta info in a (key, value) list
511
                   'text'               : text,
512 513
                   'prev_page'          : prev,
                   'next_page'          : next,
514
                   'mode'               : mode,
515 516 517
                   'namespaces'         : namespaces,
                   'authorship_authors_json' : authorship_authors_json,
                   'authors'            : authors }
518

519
    if not request.user.is_authenticated:
520 521
        request.session.set_test_cookie()
        tpl_params['next'] = reverse('pad-write', args=(slug,) )
522

523
    if mode == "r":
524
        return render(request, "pad-read.html", tpl_params)
525
    elif mode == "s":
526
        return render(request, "pad-slide.html", tpl_params)
527
    elif mode == "p":
528
        return render(request, "pad-print.html", tpl_params)
529 530


531

532
def home(request):
533 534
    return all(request)
    
535
    try:
536
        articles = json.load(open(os.path.join(BACKUP_DIR, 'index.json')))
eric's avatar
eric committed
537 538 539 540 541 542 543
    except IOError: # If there is no index.json generated, we go to the defined homepage
        try:
            Pad.objects.get(display_slug=HOME_PAD)
            return pad_read(request, slug=HOME_PAD)
        except Pad.DoesNotExist: # If there is no homepage defined we go to the login:
            return HttpResponseRedirect(reverse('login'))
    
544
    sort = 'date'
eric's avatar
eric committed
545 546
    if 'sort' in request.GET:
        sort = request.GET['sort']
547 548 549 550

    hash = {}
    for article in articles:
        if sort in article:
551
            if isinstance(article[sort], str):
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
                subject = article[sort]
                if not subject in hash:
                    hash[subject] = [article]
                else:
                    hash[subject].append(article)
            else:
                for subject in article[sort]:
                    if not subject in hash:
                        hash[subject] = [article]
                    else:
                        hash[subject].append(article)
    tpl_articles = []
    for subject in sorted(hash.keys()):
        # Add the articles sorted by date ascending:
        tpl_articles.append({
            'key' : subject,
            'values': sorted(hash[subject], key=lambda a: a['date'] if 'date' in a else 0)
        })

eric's avatar
eric committed
571 572
    tpl_params = { 'articles': tpl_articles,
                   'sort': sort }
573
    return render(request, "home.html", tpl_params)
574

575 576 577 578 579 580 581 582 583
@login_required(login_url='/accounts/login')
def publish(request):
    tpl_params = {}
    if request.method == 'POST':
        tpl_params['published'] = True
        tpl_params['message'] = snif()
    else:
        tpl_params['published'] = False
        tpl_params['message'] = ""
584
    return render(request, "publish.html", tpl_params)
585

gijs's avatar
gijs committed
586 587 588 589 590
@login_required(login_url='/accounts/login')
# def manage(request, page=1):
def manage(request, path=[]):
    if len(path) > 0:
        path = path.split('/')
gijs's avatar
gijs committed
591
        pads = Pad.objects.filter(display_slug__startswith='::'.join(path) + '::').order_by('name')
gijs's avatar
gijs committed
592
    else:
gijs's avatar
gijs committed
593
        pads = Pad.objects.all().order_by('name')
gijs's avatar
gijs committed
594 595 596 597 598 599 600 601 602 603 604
    # paginator = Paginator(pads, PADS_PER_PAGE)

    tree = makeLeaf()

    for pad in pads:
        tree = insertPad(pad, tree)
    
    if len(path) > 0:
        for key in path:
            tree = tree['folders'][key]

gijs's avatar
gijs committed
605 606 607 608
    crumbs = [(path[i], path[:i+1]) for i in range(len(path))]

    folders = [key for key in tree['folders'].keys()]

gijs's avatar
gijs committed
609
    folders.sort(key=str.lower)
gijs's avatar
gijs committed
610 611

    return render(request, "manage-tree.html", {'tree': tree, 'folderPath': path, 'crumbs': crumbs, 'folders': folders })
gijs's avatar
gijs committed
612
    
613
def all(request):
614 615 616 617 618 619 620 621
    if request.user.is_authenticated:
        return manage(request)
    else:
        return all_public(request)

def all_public(request):
    publicpads = Pad.objects.filter(is_public=True)
    return render(request, "all-public.html", { 'publicpads': publicpads })
622

623 624 625
@login_required(login_url='/accounts/login')
def all_private(request):
    return render(request, "all.html")
gijs's avatar
gijs committed
626

627
def padOrFallbackPath(request, slug, fallbackPath, mimeType):
628
    try:
629
        pad = Pad.objects.get(display_slug=slug)
630
        padID = pad.group.groupID + '$' + urllib.parse.quote(pad.name.replace('::', '_'))
631
        epclient = EtherpadLiteClient(pad.server.apikey, pad.server.apiurl)
632
        return HttpResponse(epclient.getText(padID)['text'], content_type=mimeType)
633
    except:
634
        # If there is no pad called "css", loads a default css file
635 636
        f = open(fallbackPath, 'r')
        contents = f.read()
637
        f.close()
638 639 640 641
        return HttpResponse(contents, content_type=mimeType)

def css(request):
    return padOrFallbackPath(request, 'screen.css', 'ethertoff/static/css/screen.css', 'text/css')
642

svilayphiou's avatar
svilayphiou committed
643
def cssprint(request):
644
    return padOrFallbackPath(request, 'laser.css', 'ethertoff/static/css/laser.css', 'text/css')
svilayphiou's avatar
svilayphiou committed
645 646

def offsetprint(request):
647
    return padOrFallbackPath(request, 'offset.css', 'ethertoff/static/css/offset.css', 'text/css')
648 649

def css_slide(request):
650
    return padOrFallbackPath(request, 'slidy.css', 'ethertoff/static/css/slidy.css', 'text/css')