コード例 #1
0
ファイル: views.py プロジェクト: barillari/Squarechan
def get_current_loc(request):
    """ get the current location object from the cookie or the
    request, latter overriding former """
    loc = string_to_location(request.COOKIES.get('loc', None))
    rloc = string_to_location(request.REQUEST.get('loc', None))
    if rloc: # request overrides cookie
        return rloc
    # header overrides cookie but not request
    hloc = string_to_location(request.META.get(LOCATION_HTTP_HEADER, None))
    if hloc:
        return hloc
    return loc
コード例 #2
0
ファイル: views.py プロジェクト: barillari/Squarechan
def post_ajax(request):
    """
    actually make a post via ajax. note that this kind of post
    can't have an image unless it's uploaded separately.
    """
    parsedpost = urlparse.parse_qs(str(request.POST['form']))
    # FIXME: is the csrf stuff needed?
    csrftoken = request.COOKIES.get('csrftoken', None)
    if not csrftoken:
        return HttpResponse(json.dumps(['fail', 'csrf token missing']))
    if csrftoken != parsedpost.get('csrfmiddlewaretoken', [None])[0]:
        return HttpResponse(json.dumps(\
                ['fail', 'csrf token mismatch %s %s' \
                     % (csrftoken, parsedpost['csrfmiddlewaretoken'])]))
    owner_token = parsedpost.get('owner_token', [None])[0]
    if not owner_token:
        owner_token = get_owner_token(request, cookie_only=True)
    #if not owner_token: # fixme: shouldn not require this...
    #    return HttpResponse(json.dumps(['fail', 'owner_token missing']))

    #rounding = default_int(parsedpost.get('round', [None])[0], c.DEFAULT_ROUNDING)
    # FIXME: if rounding-editing is re-enabled, we should use the above not this
    rounding = get_current_rounding(request)

    loc = string_to_location(parsedpost.get('loc', [None])[0])
    if not loc:
        return HttpResponse(json.dumps(['fail', 'location invalid']))
    content = parsedpost.get('content', [None])[0]
    if not content:
        return HttpResponse(json.dumps(['fail', 'post was blank']))
    if len(content) > MAX_POST_LEN:
        return HttpResponse(json.dumps(['fail', 
                   'post must be under %d characters' % MAX_POST_LEN]))
#    content = raw_extract(content)
    content = content.decode('utf-8', 'ignore')
    antidupetoken = parsedpost.get('antidupetoken', None)
    if not antidupetoken:
        return HttpResponse(json.dumps(['fail', 'antidupetoken missing']))
    if antidupetoken == request.session.get('antidupetoken',''):
        return HttpResponse(json.dumps(['fail', 'duplicate post ignored']))
    reply_to = default_int(parsedpost.get('reply_to', [None])[0], -1)
    reply_to = reply_to if reply_to and reply_to != -1 else None
    if reply_to == None:
        return HttpResponse(json.dumps(['fail', 'All thread-creating posts'
                                        ' must contain an image.']))
    else:
        found = Post.objects.filter(id=reply_to, censored=False)
        if len(found) == 1:
            reply_to = found[0]
        else:
            return HttpResponse(\
                json.dumps(['fail', 
                            'internal error: invalid reply_to id.']))
    make_real_post(request, owner_token, loc, content, 
                   rounding, reply_to, 
                   source=MOBILE_WEB if use_mobile(request) else WEB)
    request.session['antidupetoken'] = antidupetoken
    return HttpResponse(json.dumps(['ok', '']))
コード例 #3
0
ファイル: forms.py プロジェクト: barillari/Squarechan
 def clean(self):
     " clean "
     rounding = self.cleaned_data.get('rounding', None) 
     if rounding != None and rounding < 0:
         self._errors['rounding'] = self.error_class(["Rounding must be non-negative."])
         raise forms.ValidationError("Rounding radius must be non-negative.")
         
     if not string_to_location(self.cleaned_data.get('loc', None)):
         self._errors['content'] = self.error_class(["Invalid location."])
         raise forms.ValidationError("Location invalid.")
     # FIXME: require pic for OPs?
     if not (self.cleaned_data.get('content', None) \
                 or self.files.get('picture_file', None)):
         self._errors['content'] = \
             self.error_class(["You must supply a picture or some text."])
         raise forms.ValidationError("Please provide a picture or some text.")
     return self.cleaned_data