Esempio n. 1
0
    def post(self):
        req_json = json.loads(self.request.body)
        user_email = req_json[IDENTIFIER_USER_EMAIL]
        is_check_subscription = req_json[IDENTIFIER_CHECK_SUBSCRIPTION]
        if not is_check_subscription:
            print 'StreamQueryService >> checking all streams created by user[', user_email, ']'
            user_streams_list = ndb.Query(ancestor=ndb.Key('Account', user_email)).fetch()
            sorted_streams_list = sorted(user_streams_list, key=lambda stream: stream.last_add, reverse=True)
            sid_list = []
            for stream in sorted_streams_list:
                sid_list.append(stream.stream_id)

            dumpablelist = [Stream.get_stream(stream_id).dumpStream()
                            for stream_id in sid_list]

            self.respond(user_streams_list=dumpablelist, status="success")

        else:
            print 'StreamQueryService >> checking all streams subscribed by user[', user_email, ']'
            subscribed_list = Subscription.query(Subscription.user_email == user_email).fetch()
            subscribed_stream_ids = []
            for subscribed_item in subscribed_list:
                sub_stream = Stream.query(Stream.stream_id == subscribed_item.stream_id).fetch()
                if len(sub_stream) != 0:
                    subscribed_stream_ids.append(sub_stream[0].stream_id)

            dumpablelist = [Stream.get_stream(stream_id).dumpStream()
                            for stream_id in subscribed_stream_ids]

            self.respond(user_sub_streams_list=dumpablelist, status="success")
Esempio n. 2
0
    def post(self):
        req_json = json.loads(self.request.body)
        stream_id = req_json[IDENTIFIER_STREAM_ID]
        img_req_page = req_json[IDENTIFIER_IMG_REQ_PAGE]
        print 'ViewStreamService >> get stream id: ', stream_id, ', img_req_page:', img_req_page
        stream_obj = Stream()
        stream = stream_obj.get_stream(stream_id)

        # check if the stream exists
        if not stream:
            return self.respond(error="ViewStreamService >> Requested stream id %s does not exist." % stream_id)

        # now start retrieving images
        image_id_lst = []  # saving img ids
        image_res_idx_lst = []  # saving img indexes. There might be inconsistency between the requested list and this one
        blob_key_lst = []

        if not img_req_page:
            #   if the index list turns out to be empty
            return self.respond(error="ViewStreamService >> page index is empty")

        img_req_page = int(img_req_page)

        idx_lst = range(img_req_page*3, (img_req_page+1)*3, 1)

        for idx in idx_lst:
            if idx >= len(stream.image_id_lst):
                continue
            image_id_lst.append(stream.image_id_lst[idx])
            blob_key_lst.append(str(stream.blob_key_lst[idx]))
            image_res_idx_lst.append(idx)

        #   increase view count
        stream.increase_view_cnt()

        # next/prev page indexes
        if (img_req_page+1)*3 < len(stream.image_id_lst):
            next_pg_idx = img_req_page+1
        else:
            next_pg_idx = -1
        if img_req_page > 0:
            prev_pg_idx = img_req_page-1
        else:
            prev_pg_idx = -1

        self.respond(stream_owner=stream.user_email, image_res_idx_lst=image_res_idx_lst, blob_key_lst=blob_key_lst,
                     image_id_lst=image_id_lst, stream_name=stream.stream_name, stream_description=stream.description,
                     next_page_idx=next_pg_idx, prev_page_idx=prev_pg_idx,
                     status="Retrieved image indexes %r from stream %r" % (image_id_lst, stream_id))
Esempio n. 3
0
    def post(self):
        type = self.request.get("type")
        user_email = users.get_current_user().email()
        keyWord = self.request.get("keyWord")
        searchItem = keyWord.lower()
        stream_lst = Stream.query().fetch()
        return_lst = []

        if type == "title":
            for stream in stream_lst:
                if stream.stream_name != None:
                    if searchItem in stream.stream_name.lower():
                        return_lst.append(stream)
        else:
            for stream in stream_lst:
                if  stream.description != None:
                    if searchItem in stream.description.lower():
                        return_lst.append(stream)


        logout_url = users.create_login_url(self.request.uri)
        logout_linktext = 'Logout'

        sorted_streams = sorted(return_lst,key=lambda  stream: stream.last_add, reverse = True )

        template_values = {
            'streams' :  sorted_streams,
            'length': len(  sorted_streams),
            'user' : users.get_current_user(),
            'url': logout_url,
            'url_linktext': logout_linktext,
        }
        self.render('ViewAllStream.html', **template_values)
Esempio n. 4
0
 def get(self):
     stream_id = self.request.get("stream_id")
     user_email = users.get_current_user().email()
     stream_lst = Stream.query(Stream.user_email == user_email, Stream.stream_id == stream_id).fetch()
     curStream = stream_lst[0]
     curStream.deleteStream()
     self.redirect('/manage')
Esempio n. 5
0
    def get(self):
        # get parameters from the request
        search_keyword = self.request.get(IDENTIFIER_SEARCH_KEYWORD)
        search_type = self.request.get(IDENTIFIER_SEARCH_TYPE)
        search_keyword = str(search_keyword).lower()

        # query streams
        stream_lst = Stream.query().fetch()
        res_stream_lst = []
        stream_id_lst = []
        stream_name_lst = []
        cover_img_url_list = []

        if search_type == "title":
            for stream_iter in stream_lst:
                if stream_iter.stream_name:
                    if search_keyword in stream_iter.stream_name.lower():
                        res_stream_lst.append(stream_iter)
        else:
            for stream_iter in stream_lst:
                if stream_iter.description:
                    if search_keyword in stream_iter.description.lower():
                        res_stream_lst.append(stream_iter)

        res_stream_lst = sorted(res_stream_lst, key=lambda stream:stream.last_add, reverse=True)

        for stream_itr in res_stream_lst:
            stream_id_lst.append(stream_itr.stream_id)
            stream_name_lst.append(stream_itr.stream_name)
            cover_img_url_list.append(stream_itr.cover_url)

        self.respond(stream_id_lst=stream_id_lst, stream_name_lst=stream_name_lst,
                     cover_img_url_list=cover_img_url_list, status="success")
Esempio n. 6
0
    def get(self):
        trending_stream_lst = Stream.query().order(-Stream.views_cnt).fetch(3)

        template_values = {
            'trending_stream_lst': trending_stream_lst
        }
        self.render('Trending.html', **template_values)
Esempio n. 7
0
    def get(self):
        stream_obj = Stream()
        print "mViewNearbyImages:: starts~"

        loc_str = self.request.get("loc_str")
        loc = loc_str.split('_')
        lat = float(loc[0])
        lon = float(loc[1])

        print "mViewNearbyImages:: get location", lat, lon
        images = Image.query().fetch()

        sorted_imgs = sorted(images, key=lambda image: image.date, reverse=True)

        max_idx = len(sorted_imgs)

        print "mViewNearbyImages:: len(sorted_imgs)=", max_idx

        if max_idx >= NUM_IMG_PER_PAGE:
            max_idx = NUM_IMG_PER_PAGE

        img_url_lst = list()
        stream_id_lst = list()
        distance_lst = list()

        R = 6373.0
        r_m_lat = radians(lat)
        r_m_lon = radians(lon)

        for idx in xrange(0, max_idx):
            image = sorted_imgs[idx]
            img_url_lst.append(SERVICE_URL+"/view_photo/"+str(image.blob_key))
            stream_id_lst.append(stream_obj.get_stream_id_by_img_id(image.img_id))

            img_lat = radians(image.location.lat)
            img_lon = radians(image.location.lon)
            dlat = r_m_lat - img_lat
            dlon = r_m_lon - img_lon
            a = (sin(dlat / 2)) ** 2 + cos(r_m_lat) * cos(img_lat) * (sin(dlon / 2)) ** 2
            c = 2 * atan2(sqrt(a), sqrt(1 - a))
            distance = R * c

            distance_lst.append(distance)

        print "mViewNearbyImages:: ldistance_lst=", str(distance_lst)

        self.respond(img_url_lst=img_url_lst, stream_id_lst=stream_id_lst, distance_lst=distance_lst, status="success")
Esempio n. 8
0
    def get(self):

        print '[Cron::SendReportCronJob] started'

        # get the last report time
        global global_last_report_time
        if not global_last_report_time:
            global_last_report_time = datetime.now()
            return

        print '[Cron::SendReportCronJob] global_last_report_time:', global_last_report_time
        # get the sending rate
        reportRate = ReportSendingRate.query().fetch()

        if len(reportRate) != 0:
             reportRate = reportRate[0].sending_rate

        print '[Cron::SendReportCronJob] reportRate:', reportRate
        interval_in_mins = -99
        if reportRate == 0:
            # No reports. Return
            return
        elif reportRate == 1:
            interval_in_mins = 5
        elif reportRate == 2:
            interval_in_mins = 60
        elif reportRate == 3:
            interval_in_mins = 1440     # one day
        else:
            print 'Error in report rate : ', reportRate

        # comparing the sending rate with the time interval since the last report
        time_passed = (datetime.now() - global_last_report_time).seconds
        print '[Cron::SendReportCronJob] time_passed:', time_passed
        if time_passed < int(interval_in_mins)*60:
            return

        # send the report mail
        trending_stream_lst = Stream.query().order(-Stream.views_cnt).fetch(3)

        sender_mail_addr = REPORT_SENDER_NAME + "@" + APP_ID + MAILBOX_SURFIX
        subject = "Trending Streams from Connexus " + str(global_last_report_time)
        body = """ Here are the trending streams on Connexus now: \n
        1) %s - %s  \n
        2) %s - %s  \n
        3) %s - %s  \n
        """ % (trending_stream_lst[0].stream_name,
               trending_stream_lst[0].user_email,
               trending_stream_lst[1].stream_name,
               trending_stream_lst[1].user_email,
               trending_stream_lst[2].stream_name,
               trending_stream_lst[2].user_email)

        for receiver_addr in report_email_list:
            mail.send_mail(sender_mail_addr, receiver_addr, subject, body)

        print "Successfully sent the trending email to " + str(report_email_list)
        #reset the timer
        global_last_report_time = datetime.now()
Esempio n. 9
0
 def get(self):
     user_email = users.get_current_user().email()
     stream_lst = Stream.query(Stream.user_email == user_email).fetch()
     print stream_lst
     for stream in stream_lst:
         print stream
         stream.deleteStream()
     self.redirect('/manage')
Esempio n. 10
0
    def get(self):
        stream_id = self.request.get(IDENTIFIER_STREAM_ID)
        start_idx = self.request.get(VIEW_STREAM_START_INDEX)

        if not start_idx:
            start_idx = 0
        else:
            start_idx = int(start_idx)

        stream_obj = Stream()
        stream = stream_obj.get_stream(stream_id)

        # check if the stream exists
        if not stream:
            return self.respond(error="ViewStreamService >> Requested stream id %s does not exist." % stream_id)

        # now start retrieving images
        img_id_lst = []  # saving img ids
        img_url_lst = []
        stream_img_id_lst = stream.image_id_lst

        nrof_imgs_in_stream = len(stream_img_id_lst)

        last_idx = start_idx + NUM_STREAMS_PER_PAGE

        if last_idx >= nrof_imgs_in_stream:
            last_idx = nrof_imgs_in_stream-1

        idx_lst = xrange(start_idx, last_idx)

        for img_idx in idx_lst:
            img_id = stream_img_id_lst[img_idx]
            img_id_lst.append(img_id)
            image = Image.get_image(img_id)
            img_url_lst.append(SERVICE_URL+"/view_photo/"+str(image.blob_key))

        # increase view count
        stream.increase_view_cnt()

        # generate upload url
        upload_url = blobstore.create_upload_url('/ws/stream/view_imgs')

        self.respond(stream_owner=stream.user_email, stream_name=stream.stream_name, img_id_lst=img_id_lst,
                     img_url_lst=img_url_lst, last_idx=last_idx, nrof_imgs_in_stream=nrof_imgs_in_stream,
                     upload_url=upload_url, status="success")
Esempio n. 11
0
    def get(self):
        stream_id_lst=list()
        stream_name_lst=list()
        cover_img_url_list=list()
        stream_lst=list()

        is_view_all_subscribed = self.request.get(IS_VIEW_ALL_SUBSCRIBED)
        usr_id = self.request.get(IDENTIFIER_USER_EMAIL)

        if is_view_all_subscribed:
            print 'mViewAllStreamsService:: getting subscribed streams list for user', usr_id

            subscribed_list = Subscription.query(Subscription.user_email == usr_id).fetch()
            for subscribed_item in subscribed_list:
                stream_lst.append(Stream.get_stream(subscribed_item.stream_id))

        else:
            print 'mViewAllStreamsService:: getting all streams list'
            stream_lst = Stream.query().fetch()

        sorted_streams = sorted(stream_lst, key=lambda stream:stream.last_add, reverse=True)

        nrof_streams = len(sorted_streams)

        start_idx = self.request.get(VIEW_ALL_START_INDEX)
        if not start_idx or (start_idx > nrof_streams):
            start_idx = 0

        last_idx = start_idx+NUM_STREAMS_PER_PAGE
        if last_idx > nrof_streams:
            last_idx = nrof_streams-1

        for i in xrange(start_idx, last_idx+1):
            stream = sorted_streams[i]
            stream_id_lst.append(stream.stream_id)
            stream_name_lst.append(stream.stream_name)
            cover_img_url_list.append(stream.cover_url)

        print "mViewAllStreamsService:: stream_id_lst:" + str(stream_id_lst) + " |||| stream_name_lst:" + \
              str(stream_name_lst) + " |||| cover_img_url_list:" + str(cover_img_url_list) + " |||| last_idx:" + \
              str(last_idx)

        self.respond(stream_id_lst=stream_id_lst, stream_name_lst=stream_name_lst,
                     cover_img_url_list=cover_img_url_list, last_idx=last_idx, status="success")
Esempio n. 12
0
    def post(self):
        req_json = json.loads(self.request.body)
        user_email = req_json[IDENTIFIER_USER_EMAIL]
        stream_name = req_json[IDENTIFIER_STREAM_NAME]
        stream_id = uuid.uuid4()
        cover_url = req_json[IDENTIFIER_COVER_URL]
        description = req_json[IDENTIFIER_STREAM_DESC]

        new_stream = Stream(parent=ndb.Key('Account', user_email),
                            user_email=user_email,
                            stream_id=str(stream_id),
                            stream_name=stream_name,
                            views_cnt=0,
                            total_views_cnt=0,
                            cover_url=cover_url,
                            description=description)
        new_stream.put()
        print 'Successfully created new stream:', str(stream_id)
        self.respond(stream_id=str(stream_id), status="Success")
Esempio n. 13
0
    def post(self):
        user_email = self.request.get(IDENTIFIER_USER_EMAIL)
        stream_id = self.request.get(IDENTIFIER_STREAM_ID)
        description = self.request.get(IDENTIFIER_STREAM_DESC)
        location = self.request.get(IDENTIFIER_LOCATION)

        print 'UploadImageService >> get upload image request ', len(self.get_uploads())

        for upload in self.get_uploads():
            #get the blob store object
            # upload = self.get_uploads()[0]
            image_id = uuid.uuid4()
            print "UploadImageService >> Upload new image with blob_key: " + str(upload.key())

            if not location:
                # Generate a random location - Phase II requirement
                location = ndb.GeoPt(random.uniform(-90, 90), random.uniform(0, 90))
            else:
                loc_cor = location.split("_")
                if len(loc_cor)!=2:
                    print "length of the location parameter is not 2. (", location, ")"
                location = ndb.GeoPt(long(float(loc_cor[0])), long(float(loc_cor[1])))

            user_image = Image(user_email=user_email,
                               img_id=str(image_id),
                               content=description,
                               blob_key=upload.key(),
                               location=location)
            # stream_lst = Stream.query(Stream.user_id == user_id, Stream.stream_id == stream_id).fetch()
            # cur_stream = stream_lst[0]
            current_stream = Stream().get_stream(stream_id)
            if current_stream:
                print "UploadImageService >> stream id:", stream_id, ", added image id: ", str(image_id)

                current_stream.addImage(key=user_image.img_id, image=user_image)
                print "UploadImageService >> blob_key_lst after adding: ", current_stream.blob_key_lst
            else:
                print "UploadImageService >> Fail to add user photo ", user_image, "to stream ", stream_id
Esempio n. 14
0
 def get(self):
     pattern = self.request.get("term")
     stream_lst = Stream.query().fetch()
     ret_lst = []
     if pattern:
         for stream in stream_lst:
             if pattern in stream.stream_name:
                 ret_lst.append(stream.stream_name)
     ret_lst.sort()
     if len(ret_lst) == 0:
         valid = False
     else:
         valid = True
     context = {"valid": valid, "ret_lst": ret_lst}
     self.response.write(json.dumps(context))
Esempio n. 15
0
    def get(self):
        user_email = users.get_current_user().email()

        stream_lst = Stream.query().fetch()

        logout_url = users.create_login_url(self.request.uri)
        logout_linktext = 'Logout'

        sorted_streams = sorted(stream_lst,key=lambda  stream: stream.last_add, reverse = True )

        template_values = {
            'streams' : sorted_streams,
            'length': len(sorted_streams),
            'user' : users.get_current_user(),
            'url': logout_url,
            'url_linktext': logout_linktext,
        }
        self.render('ViewAllStream.html', **template_values)
Esempio n. 16
0
    def post(self):
        req_json = json.loads(self.request.body)
        stream_id = req_json[IDENTIFIER_STREAM_ID]
        current_user_email = req_json[IDENTIFIER_USER_EMAIL]
        photo_key = req_json[IDENTIFIER_PHOTO_KEY]

        response = {}
        stream = Stream.get_stream(stream_id)

        if not (stream_id and current_user_email and photo_key and stream):
            response['error'] = "Failed to find photo_key (" + photo_key + ") for user (" + current_user_email \
                                + ") under stream(" + stream_id+")."
            self.respond(**response)
        elif stream.user_id != current_user_email:
            response['error'] = "Failed to remove image user (" + current_user_email \
                                + ") is not the owner of stream(" + stream_id+")."
            self.respond(**response)
        else:
            stream.deleteImage(photo_key)
            self.respond(status="success")
Esempio n. 17
0
    def get(self):

        stream_id = self.request.get(IDENTIFIER_STREAM_ID)

        query_begin_date = self.request.get(QUERY_BEGIN_DATE)
        query_end_date = self.request.get(QUERY_END_DATE)

        begin_date = datetime.strptime(query_begin_date, '%Y-%m-%dT%H:%M:%S.%fZ')
        end_date = datetime.strptime(query_end_date, '%Y-%m-%dT%H:%M:%S.%fZ')

        stream = Stream.get_stream(stream_id)
        response = {}
        markers_lst = list()
        if not stream:
            response['error'] = "Failed to find stream(" + stream_id+")."
            self.respond(**response)
        else:
            for image_id in stream.image_id_lst:
                image = Image.get_image(image_id)
                if begin_date <= image.date <= end_date:
                    content = '<img src="'+get_serving_url(image.blob_key, size=100)+'">'
                    marker = {"latitude": image.location.lat, "longitude": image.location.lon, "content":content}
                    markers_lst.append(marker)
            self.respond(markers=markers_lst, status="success")
Esempio n. 18
0
 def get(self):
     all_streams_lst = Stream.query().fetch()
     print 'Cron job executing ... cleaning the view counts for ', len(all_streams_lst), ' streams.'
     for stream in all_streams_lst:
         stream.reset_view_cnt()