summaryrefslogtreecommitdiff
path: root/frontend/views.py
blob: 552f0d9f0140d4ab68c9acb88c6e324a2963031f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import re

from django.conf import settings
from django.http import HttpResponse
from django.shortcuts import render_to_response

from backend.models import SJLike
from backend.models import SJRoom
from backend.models import SJContent
from backend.models import SJSearch
from backend.models import SJUserProfile

from backend.views import is_number
from backend.views import is_image

from django.db.models import Q

LIMIT = 40

BLOCKED_DOMAINS = {
    'dvdbeaver.com': True,
    '4chan.org': True,
    'yahoo.com': True,
    'adultswim.com': True,
}


def bg(request):
    """
    """
    query = Q(content_type='background')
    if request.GET.get('start', None):
        if is_number(request.GET['start']):
            query &= Q(id__lt=int(request.GET['start']))
    limit = LIMIT
    if request.GET.get('limit', None):
        if is_number(request.GET['limit']):
            limit = int(request.GET['limit'])
    backgrounds = SJContent.objects.filter(query).order_by('-id')[0:limit]

    filtered_backgrounds = []
    for b in backgrounds:
        s = b.settings
        url = s['url']
        if not url.startswith('http:'):
            continue
        domain = ''
        try:
            domain = '.'.join(url.split('/')[2].split('.')[-2:])
        except:
            continue
        if domain in BLOCKED_DOMAINS:
            continue
        b.domain = domain
        filtered_backgrounds.append(b)

    return render_to_response(
        'backgrounds.html',
        {
             'server_host': settings.SERVER_HOST,
             'server_port': settings.SERVER_PORT,
             'lowest_id': backgrounds[limit - 1].id,
             'backgrounds': filtered_backgrounds,
             'domain': domain,
        }
    )