Example #1
0
    def retrieveScoreGardensUrls(self):
        result={}
        try:
            
             day=self.myjson["Day"]
                                 
        except Exception as e:
             #print "Content-type: text/html\n" 
             result["message"]="Error%s"%e.message
             return (json.JSONEncoder().encode(result))

        
        if day is not None:
            if day=="Today":
                today_date=datetime.date.today()
                date_str="%s"%today_date 
            else:
                date_str="%s"%day
        else:
             result["message"]="Error: The option '%s' is invalid"%day
             return (json.JSONEncoder().encode(result))
             
             
             
        
        
        myjson={'Fname':'Dummy','Lname':'Dummy','Username':'******'}
        obj=RetrieveIntermediary(myjson)
        res=obj.retrieveIntermediaryInDB()
        
        intermediaries_tuple=json.loads(res)
        intermediaries_emails=""
        intermediary_names=""
        #orig_emails=[]
        beneficiary_id=0
        beneficiary_names=""
        posn=0
        gardens=[]
        competitors_counter=0
        
        garden_label=date_str.replace("-","_")   
        first_posn=0
        second_posn=0

        key2="D"    
        trees=0
        flowers=0
        fishnum=0
        fish_size=0.0
        total_plants=""
        url=""
        usage_points=""
        bonus_points=""
        badge=""
        badge_url=""
        team_members=""
      
        team_name=""
        team_features=[]
        for record in intermediaries_tuple.items():
             
             
             key,user =record
            
             if(user["D2"]=="None"):
                 continue
             else:
                  
                 
                  #orig_emails.append(user["D1"]) #keep original email addresses
                  orig_email=user["D1"]
             
                  user["D1"]=user["D1"].replace("@","_at_")
                  user["D1"]=user["D1"].replace(".","_dot_")
                  
                  intermediary_email=user["D1"]
                  intermediary_names=user["D0"]
                  beneficiary_names=user["D2"][0:user["D2"].index('.')]# get the name only
                  myjson={'Fname':'Dummy','Lname':'Dummy','Username':orig_email}
                  obj=RetrieveIntermediary(myjson)
                  result2=obj.isAssignedBeneficiary()
                  
                  beneficiary_tuple=json.loads(result2)
                 
                  
                  team_identifier="%s_%s"%(beneficiary_tuple["Id"],garden_label)
                  beneficiary_id=team_identifier  #Beneficiary 

                
                  file_path="django_facebook/images/garden/%s/%s_%s.jpeg"%(intermediary_email,beneficiary_tuple["Id"],garden_label)
                  
                  file_name="%s_%s"%(beneficiary_id,garden_label)
                  url=file_path # url


                  
                  team_name=beneficiary_tuple["TeamName"] # Team Name
                  
                  one_team_members="(%s, %s)"%(intermediary_names,beneficiary_names) # put together team members
            


                  team_members=one_team_members  #Team Members
                 
                
                   
                  varmyjson={'Day':day}
		  
                                   
                  clickPointsObj=RetrievePoints(varmyjson,orig_email,self.last_date_specified)
                  resclickpoints=clickPointsObj.retrieveIntermediaryClickPoints()
                  resclickpoints=json.loads(resclickpoints)
                  
                  clickpoints=int(resclickpoints["points"])
                  
                  clickpoints=clickpoints*1000

                
                   
                  usage_points=clickpoints  #Usage Points
                  
                  ressteps=clickPointsObj.getSteps(beneficiary_tuple["Id"])
                  ressteps=json.loads(ressteps)
                   

                  stepspoints=int(ressteps["steps"]/ressteps["dates_counter"])
                 
                  
                  
                  bonus_points=stepspoints  #Bonus points

                  total_points=usage_points+bonus_points
                  
                     
                  
                  resbadge=clickPointsObj.retrieveIndividualBadge()
                  resbadge=json.loads(resbadge)
                  
                  #get badge name for this individual
                  badge=resbadge["R00"]["D3"] #Badge

                  #get a badge url for this person
                  badge_url=resbadge["R00"]["D1"] #Badge URL

                                    

                  trees=int(10*(11-resbadge["R00"]["D4"])) # the number of trees in a garden
                  fishnum=int(11-resbadge["R00"]["D4"]) # the number of fish in a tank
                  fishsize=float(float(fishnum)/float(10)) # fish size is determined by how many fish are in the tank. The more fish the bigger the size. This weill also be used to detrmine the quality of a fish tank
                  


                  resmealcounter=self.countRecordedMeals(beneficiary_tuple["Id"])
                  resmealcounter=json.loads(resmealcounter)

                  flowers=(10*resmealcounter["NumberOfMeals"]) # number of flowers in the garden

                  
                  #print file_name,trees, flowers
             
                  #total=trees+flowers
                  
                  total_plants=fishnum*10 # these are the total points



                  obj=SaveComment(myjson,beneficiary_tuple["Id"],orig_email)
              
                  res=obj.getComments("Aquarium")

                  aquariumcomments=json.loads(res)



                  #newaquariumcomments=json.loads(newaquariumcomments)

                    
                

              
                  res=obj.getComments("Garden")

                  gardencomments=json.loads(res)


                  one_team_features=TeamFeatures(team_name,team_members,url,trees,flowers,total_plants,usage_points,bonus_points,badge,badge_url,beneficiary_tuple["Id"],fishnum,fishsize,aquariumcomments,gardencomments,total_points)
                  team_features.append(one_team_features)


                  posn=posn+1
                  
        posn=0      
      
    
           
        #bubblesort(total_plants,team_name,team_members,urls,beneficiary_ids,tree_array,flower_array,usage_points,bonus_points,badges,badges_urls,fish_array,fish_size_array)
        bubblesort(team_features)
        posn=0
        file_path_alt="django_facebook/images/garden/blank.jpg"
        for one_team in team_features:
            urls_tuple={}
            if first_posn<10:
              key1="R0"
            else:
              key1="R"     
              
            urls_tuple[key2+"%s"%second_posn]="%s"%one_team.team_name # D0 team name
            second_posn=second_posn+1  

            urls_tuple[key2+"%s"%second_posn]="%s"%one_team.team_members # D1 team members
            second_posn=second_posn+1 
            
            urls_tuple[key2+"%s"%second_posn]=one_team.url #D2 url for garden
            if one_team.total_plants==0:
              urls_tuple[key2+"%s"%second_posn]=file_path_alt
              
            second_posn=second_posn+1
            
            

            urls_tuple[key2+"%s"%second_posn]="%s"%one_team.trees  #D3 Number of trees
            second_posn=second_posn+1
            
            urls_tuple[key2+"%s"%second_posn]="%s"%one_team.flowers  # D4 Number of flowers
            second_posn=second_posn+1
            
            urls_tuple[key2+"%s"%second_posn]="%s"%one_team.total_plants  #D5 Total number of plants including flowers and tree
            second_posn=second_posn+1
            

            urls_tuple[key2+"%s"%second_posn]="%s"%one_team.usage_points #D6 Number of days an application has been used multiply by 1000 by a team. 
            second_posn=second_posn+1
 
 
            urls_tuple[key2+"%s"%second_posn]="%s"%one_team.bonus_points  #D7 average steps walked by an individual
            second_posn=second_posn+1

            urls_tuple[key2+"%s"%second_posn]="%s"%one_team.badge #D8 Badge name
            second_posn=second_posn+1
            
            urls_tuple[key2+"%s"%second_posn]="%s"%one_team.badge_url #D9 Badge Url
            second_posn=second_posn+1


            urls_tuple[key2+"%s"%second_posn]="%s"%one_team.beneficiary_id  #D10 Beneficiary ID
            second_posn=second_posn+1



            urls_tuple[key2+"%s"%second_posn]="%s"%one_team.fishnum # D11 Number of fish in the tank.
            second_posn=second_posn+1


            urls_tuple[key2+"%s"%second_posn]="%s"%one_team.fishsize #D12 size of fish in the tank
            second_posn=second_posn+1




            urls_tuple[key2+"%s"%second_posn]=OrderedDict(sorted(one_team.aquariumcomments.items(), key=lambda t: t[0])) #D13 Aquarium comments
            second_posn=second_posn+1
            

            urls_tuple[key2+"%s"%second_posn]=OrderedDict(sorted(one_team.gardencomments.items(), key=lambda t: t[0])) #D14 Garden comments
            second_posn=second_posn+1


            urls_tuple[key2+"%s"%second_posn]="%s"%one_team.total_points #D15 Total points
            second_posn=second_posn+1


            

            
            second_posn=0
            result[key1+"%s"%first_posn]=(OrderedDict(sorted(urls_tuple.items(), key=lambda t: t[0])))
            first_posn=first_posn+1
            posn=posn+1
        
             
        return (json.JSONEncoder().encode(OrderedDict(sorted(result.items(), key=lambda t: t[0]))))