Exemple #1
0
 def get_short_url(self, user=None):
     if not self.id: raise UrlNotSaved
     domain = None
     if user:
         domain = redis_ob.hget("user:%s" %str(user), "custom_domain")
     if not domain: domain = settings.SHORT_URL
     return '%s%s/' %(domain, to36(int(self.id)))
Exemple #2
0
def file_upload(request):
    if not request.method == 'POST': raise Http404
    if not request.FILES: raise Http404
    if not request.FILES.has_key('file'): raise Http404
    if not os.path.exists(os.path.join(settings.MEDIA_ROOT, 'files')):
        os.makedirs(os.path.join(settings.MEDIA_ROOT, 'files'))
    
    url_id = redis_ob.incr('counter:url')
    temp_file_name = to36(url_id)
    file_data = request.FILES['file']
    api_key = request.POST.get("api_key", None)
    sub = file_data.name.split('.')[-1]
    destination = open(os.path.join(settings.MEDIA_ROOT, 'files', temp_file_name+'.'+sub), 'wb+')
    for chunk in file_data.chunks():
        destination.write(chunk)
    url = settings.SHORT_URL+"static/files/"+temp_file_name+'.'+sub
    url_object = UrlModel(url_data={'url':url,'is_file':True, 'file_name':temp_file_name+'.'+sub})
    url_object.save(url_id=url_id)
    # if authenticated user set url to his account
    if request.session.has_key("user_id"):
        user_id = request.session["user_id"]
        if redis_ob.hexists("user:%s" %str(user_id), "email"):
            redis_ob.lpush("user:urls:%s" %request.session['user_id'], "url:"+str(url_object.id))
        link = url_object.get_short_url(user_id)
    else:
        link = url_object.get_short_url()
    # if api_key is set add the url to the user account
    if api_key:
        user_id = redis_ob.get("user:api_key:%s" %api_key)
        if redis_ob.hexists("user:%s" %str(user_id), "email"):
            redis_ob.lpush("user:urls:%s" %user_id, "url:"+str(url_object.id))
    if request.POST.get("html", None) == "true":
        return HttpResponse(simplejson.dumps({"url": link, "long_url": url}), mimetype="application/javascript")
    return HttpResponse(link)
Exemple #3
0
 def thing_attr(self, thing, attr):
     if attr == 'children':
         return [to36(x) for x in thing.children]
     if attr in ('body', 'body_html'):
         return ""
     return CommentJsonTemplate.thing_attr(self, thing, attr)
Exemple #4
0
def make_fullname(typ, _id):
    return '%s_%s' % (make_typename(typ), to36(_id))
Exemple #5
0
def make_typename(typ):
    return 't%s' % to36(typ._type_id)
Exemple #6
0
 def thing_attr(self, thing, attr):
     if attr == 'children':
         return [to36(x) for x in thing.children]
     if attr in ('body', 'body_html'):
         return ""
     return CommentJsonTemplate.thing_attr(self, thing, attr)
Exemple #7
0
def make_fullname(typ, _id):
    return '%s_%s' % (make_typename(typ), to36(_id))
Exemple #8
0
def make_typename(typ):
    return 't%s' % to36(typ._type_id)
Exemple #9
0
 def thing_attr(self, thing, attr):
     if attr == "children":
         return [to36(x) for x in thing.children]
     if attr in ("body", "body_html"):
         return ""
     return CommentJsonTemplate.thing_attr(self, thing, attr)
Exemple #10
0
def make_typename(typ):
    return "t%s" % to36(typ._type_id)