Esempio n. 1
0
    def api_slot_machine(self, request, **kwargs):
        self.method_check(request, allowed=['post', 'get'])

        if request.method == "POST":
            # They rated a previous one
            moment_id = request.POST.get("moment_id", "")
            rated_moment = Moment.objects.filter(pk = moment_id)
            if len(rated_moment) == 0:
                return self.create_response(request, {'success': False, 'msg': 'Invalid moment_id: %s' % moment_id}, response_class=HttpResponseBadRequest)
            try:
                stars = int(request.POST.get("stars", 0))
                assert(stars >= 1)
                assert(stars <= 10)
            except:
                return self.create_response(request, {'success': False, 'msg': 'Invalid stars (1-10): %s' % stars}, response_class=HttpResponseBadRequest)

            rating = Rating(moment = rated_moment[0], upload_ip = get_real_ip(request), stars = stars)
            rating.save()

            # Get the ratings for the supplied moment
            meta = self._get_ratings(rated_moment)
        else:
            meta = {}

        # Pull one randomly, exclude what they've rated
        existing_ratings = Rating.objects.filter(upload_ip = get_real_ip(request)).values_list("moment_id", flat = True)
        random_moment = Moment.objects.exclude(pk__in = existing_ratings).order_by('?')[:1]
        if len(random_moment) == 0:
            return self.create_response(request, {'success': False, 'code': 'allseen'})

        bundle = self.build_bundle(obj=random_moment[0], request=request)
        bundle = self.full_dehydrate(bundle)
        bundle.data["previous_results"] = meta
        return self.create_response(request, bundle)
Esempio n. 2
0
    def api_upload(self, request, **kwargs):
        self.method_check(request, allowed=['post'])
        filename = request.POST.get("file", "");
        if filename == "":
            return self.create_response(request, {'success': False, 'msg': 'file is required'}, response_class=HttpResponseBadRequest)

        fullfile = os.path.normpath(os.path.join(UPLOADED_FILES_DIR, filename))
        # Avoid anything trixy.
        assert(UPLOADED_FILES_DIR in fullfile)

        my_moment = Moment()
        photo = File(open(fullfile, 'rw'))
        my_moment.photo = photo
        my_moment.upload_ip = get_real_ip(request)
        expires_at = request.POST.get("expires_at", "")
        # TODO: dynamically parse strings like "1 day" to timedeltas
        if expires_at == "1 day":
            my_moment.expires = datetime.datetime.now() + datetime.timedelta(days=1)
        elif expires_at == "1 week":
            my_moment.expires = datetime.datetime.now() + datetime.timedelta(weeks=1)
        elif expires_at == "1 month":
            my_moment.expires = datetime.datetime.now() + datetime.timedelta(months=1)

        if request.POST.get("public") == "0":
            my_moment.public = 0

        my_moment.save()
        os.remove(fullfile)

        bundle = self.build_bundle(obj=my_moment, request=request)
        bundle = self.full_dehydrate(bundle)
        return self.create_response(request, bundle)
 def _(*args, **kwargs):
     args = list(args)
     request.ip = get_real_ip(request.environ)
     request.bid = request.cookies.get('bid')
     request.user = request.environ.get('user', None)
     request.is_weixin = is_weixin(request)
     request.query_params = get_query_params(request)
     g.scheme = request.environ['wsgi.url_scheme']
     request.form
     args.insert(1, request)
     return f(*args, **kwargs)
Esempio n. 4
0
 def _(*args, **kwargs):
     args = list(args)
     request.ip = get_real_ip(request.environ)
     request.bid = request.cookies.get('bid')
     request.user = request.environ.get('user', None)
     request.is_weixin = is_weixin(request)
     request.query_params = get_query_params(request)
     g.scheme = request.environ['wsgi.url_scheme']
     request.form
     args.insert(1, request)
     return f(*args, **kwargs)
Esempio n. 5
0
    def reconnet(self):

        while True:
            self.adsl_stop()
            time.sleep(5)
            self.adsl_start()
            new_ip = get_real_ip()
            logger.info("new ip after adsl: %s" % new_ip)
            if new_ip and new_ip != self.using_ip and self.is_ban(new_ip):
                # new_ip may be none, indicate adsl-start does not success
                self.using_ip = new_ip
                self.redis.set(IP, self.using_ip)
                break
Esempio n. 6
0
    def start(self):
        for name, server in self.name_servers.items():
            client = LinuxVpnConnection(self.username, self.password, name,
                                        server)
            self.clients.append(client)

        if self.using_vpn is None:
            self.using_vpn = random.choice(self.clients)
        self.set_is_pptp(1)
        self.using_vpn.start()
        self.add_default_route()
        self.using_ip = get_real_ip()
        self.set_vpn_ip(self.using_ip)
        self.set_is_pptp(0)
Esempio n. 7
0
    def run(self):
        self.start()
        while True:
            logger.debug("check pptp signal")
            is_going_pptp = int(self.redis.get("pptp"))
            is_pptp = self.get_ip_pptp()
            if is_going_pptp or self.is_died:
                logger.info("old vpn ip: %s" % self.using_ip)
                self.set_is_pptp(1)
                while True:
                    self.reconnect()
                    new_using_ip = get_real_ip()
                    print new_using_ip, self.using_ip
                    if new_using_ip != self.using_ip:
                        break
                    logger.info(u"adsl get same ip, we need adsl again")
                logger.info("new vpn ip: %s" % new_using_ip)
                self.set_vpn_ip(new_using_ip)
                self.using_ip = new_using_ip
                self.set_is_pptp(0)
                self.redis.set("pptp", 0)

            time.sleep(1)
Esempio n. 8
0
 def hydrate(self, bundle):
     # Set the upload_ip
     bundle.obj.upload_ip = get_real_ip(bundle.request)
Esempio n. 9
0
 def hydrate(self, bundle):
     # populate the moment
     bundle.obj.moment = Moment.objects.get(pk = bundle.data.get("moment_id", -1))
     # Auto populate the upload_ip
     bundle.obj.upload_ip = get_real_ip(bundle.request)
     return bundle
Esempio n. 10
0
 def __init__(self):
     self.redis = redis.Redis()
     self.using_ip = get_real_ip()
     self.redis.set(IP, self.using_ip)
     self.init_redis()