joeni

joeni_org.py

1
from django import template
2
from django.utils.safestring import mark_safe
3
import subprocess  # To call Pandoc
4
import requests
5
6
register = template.Library()
7
8
@register.filter
9
def org(value):
10
    """ Takes an input, and transpiles it as org-mode syntax to HTML syntax. """
11
    # TODO Write bug handling code and exception handling
12
    f = open('/tmp/django-temp.org', 'w')
13
    f.write(value)
14
15
    f.close()
16
    f = open('/tmp/django-temp.org', 'r')
17
    #f2 = open('/tmp/django-output.html', 'w+')
18
    subprocess.run(["pandoc", "--from=org", "--to=html", "-o" "/tmp/django-output.html"], stdin=f)
19
    #f2.close()
20
    f2 = open('/tmp/django-output.html', 'r')
21
    result = f2.read()
22
    f.close()
23
    f2.close()
24
    return mark_safe(result)
25
    #print("OK!")
26
    #return mark_safe(subprocess.check_output(["iconv", "-t", "utf-8", "/tmp/django-temp.org", "|", "pandoc", "--from=org", "--to=html"]))#, "/tmp/django-temp.org"]))
27
#return mark_safe(subprocess.check_output(["pandoc", "--from=org", "--to=html", value]))
28
29
@register.simple_tag
30
def pingping():
31
    return "" # TODO REMOVE
32
    link = "https://uhasselt-pxl.mynetpay.be/Account/Login"
33
    # Get CSRF token
34
    first_call = requests.get(link)
35
    text = first_call.text
36
    begin = text.find('__RequestVerificationToken')
37
    begin = text.find('value="', begin)
38
    end = text.find('" ', begin)
39
    token = text[begin+len('value="'):end]
40
    cookies = first_call.cookies
41
42
    username = ""
43
    password = ""
44
45
    response = requests.post(link, data={
46
        'Username':username,
47
        'LoginType':'Student',
48
        'Password':password,
49
        '__RequestVerificationToken':token,
50
        }, cookies=cookies)
51
52
    html_response = response.text
53
    start = html_response.find(": € ")
54
    offset = len(": € ")
55
    return html_response[start+offset:start+offset+5]
56