def get(self):
        View.more = True
        template = JINJA_ENVIRONMENT.get_template('templates/trending.html')
        countView_query = CountViewModel.query().order(-CountViewModel.count).fetch()
        index = 0
        infos = []
        if len(countView_query)> 0:
            for view in countView_query:
                if index < 3:
                    index += 1
                    stream_query = StreamModel.query(StreamModel.name == view.name).fetch()
                    if len(stream_query)>0:
                        stream = stream_query[0]
                        infos.append((stream.name, stream.coverpageURL, stream.url, str(view.count)))
        count_query = CountModel.query(CountModel.name=="Trending").fetch()
        freq = "No reports"
        if len(count_query)==0:
            count = CountModel(name="Trending", count=0, freq=0)
            count.put()
        else:
            count = count_query[0]
            freq = freq_dict[count.freq]

        template_values = {
            'infos': infos,
            'freq': freq
        }
        self.response.write(template.render(template_values))
    def get(self):
        View.more = True
        self.response.write(TRENDING_PAGE_TEMPLATE)
        countView_query = CountViewModel.query().order(-CountViewModel.count).fetch()
        index = 0
        self.response.write('<table border="0" style="width:100%">')
        if len(countView_query)> 0:
            for view in countView_query:
                if index < 3:
                    index += 1
                    stream_query = StreamModel.query(StreamModel.name == view.name).fetch()
                    if len(stream_query)>0:
                        stream = stream_query[0]
                        self.response.write(STREAM_ENTRY_TEMPLATE % (stream.url, stream.coverpageURL, stream.name, str(view.count) + " views in past hour"))

        self.response.write('</table><hr>')

        self.response.write(TRENDING_REPORT_TEMPLATE)
        count_query = CountModel.query(CountModel.name=="Trending").fetch()
        if len(count_query)==0:
            count = CountModel(name="Trending", count=0, freq=0)
            self.response.write( "Present frequency: No reports")
            count.put()
        else:
            count = count_query[0]
            self.response.write("Present frequency: "+freq_dict[count.freq])
 def get(self):
     sender = EMAIL_SENDER + "@"+SERVICE_DOMAIN+MAILBOX
     cd_query = CountModel.query(CountModel.name=="Trending").fetch()
     if (len(cd_query)>0):
         cd = cd_query[0]
         if cd.freq != 0:
             cd.count = cd.count + 1
             if (cd.count == cd.freq):
                 cd.count = 0
                 subject = DEFAULT_TRENDING_SUBJECT + EMAIL_SENDER
                 mail.send_mail(sender=sender, to=TAEmail1, subject=subject, body=DEFAULT_TRENDING_MESSAGE)
                 mail.send_mail(sender=sender, to=TAEmail2, subject=subject, body=DEFAULT_TRENDING_MESSAGE)
                 mail.send_mail(sender=sender, to="*****@*****.**", subject=subject, body=DEFAULT_TRENDING_MESSAGE)
                 mail.send_mail(sender=sender, to="*****@*****.**", subject=subject, body=DEFAULT_TRENDING_MESSAGE)
             cd.put()
 def post(self):
     user = users.get_current_user()
     returnURL = self.request.headers['Referer']
     frequency = self.request.get("frequency")
     count_query = CountModel.query(CountModel.name=="Trending").fetch()
     if len(count_query)>0:
         count = count_query[0]
         if frequency == "No reports":
             count.f = 0
             count.freq = 0
         elif frequency == "Every 5 minutes":
             count.f = 1
             count.freq = 1
         elif frequency == "Every 1 hour":
             count.f = 12
             count.freq = 12
         elif frequency == "Every day":
             count.f = 288
             count.freq = 288
         count.put()
     self.redirect(returnURL)