655321
655321

Reputation: 421

How do I create a dynamic list of variables for a GAE datastore query?

I'm using the GAE datastore to handle email jobs for a study with rolling enrollment. Each row (in emailJobs) represents a participant. There are 12 variables I want to query. Each one represents a follow-up period (from 1 to 12). The variables hold the date a person was sent an email for that specific follow-up period. Since people start the study at different times not everyone will be at the same follow-up period, at any given time.

I want to identify the people who are within a certain follow-up period and see if they got all the follow-up emails they should have gotten at that point in the study.

I'm trying to reduce the time it takes to query the datastore by only focusing on the variables of interest and ignoring variables that are definitely None types.

The way I'm trying to address this is by creating a dynamic list of variables which I will target in my query.

fu1_email_sent, fu2_email_sent, ...fu12_email_sent

Here's the abridged version of the code:

class emailJobs(db.Model):
    """ Models a list of email jobs for each participant """

    date_modified = db.DateTimeProperty(auto_now_add=True, name='Date modified')
    author = db.StringProperty() # Google account which made changes (for testing)

    triggerid = db.StringProperty()  # TriggerResponseID (connects participants across both panels)
    recipientid_po = db.StringProperty() # RecipientID - Original Panel
    recipientid_pdi = db.StringProperty() # RecipientID - De-identified Panel

    unsubscribed = db.IntegerProperty() # Subscription status 

    recipientlang = db.StringProperty()  # RecipientLanguage
    consent_date = db.DateTimeProperty() # CONSENTDATE datetime (timestamp)
    test = db.StringProperty() # Identifies test data (TESTDATA = 1)
    fuperiod = db.IntegerProperty() #Follow-up period (0 - 13: 0-before follow-up, 13-past fu12)

    last_fu_sent = db.DateTimeProperty() # Datetime last follow-up email was sent
    fu1_email_sent = db.DateTimeProperty() # ****************
    fu2_email_sent = db.DateTimeProperty()
    fu3_email_sent = db.DateTimeProperty()
    fu4_email_sent = db.DateTimeProperty()
    fu5_email_sent = db.DateTimeProperty()
    fu6_email_sent = db.DateTimeProperty()
    fu7_email_sent = db.DateTimeProperty()
    fu8_email_sent = db.DateTimeProperty()
    fu9_email_sent = db.DateTimeProperty()
    fu10_email_sent = db.DateTimeProperty()
    fu11_email_sent = db.DateTimeProperty()
    fu12_email_sent = db.DateTimeProperty()



class sendMissedFU(webapp2.RequestHandler):
    """ Queries Datastore for follow-up dates that were missed, then adds them to a task queue so that an email can be sent out """
    def get(self):

        now = datetime.datetime.now()
        now_dt = now.date() #today's date to compare with follow-up dates

        q = emailJobs.all()
        q.filter('consent_date !=', None ) # weeds out anyone who doesn't have a consent date
        q.order('consent_date')

        for part in q:

            trigid = part.triggerid
            lang = part.recipientlang
            guid = part.recipientid_po

            consent = part.consent_date
            consent_date = consent.date()
            calcdTime = now_dt - consent_date
            dayssince = calcdTime.days
            fup = dayssince/30


            fuemailsent_dict = {}
            fuemailsent_list = []

            for i in range(1, fup + 1):
                i = str(i)
                followup = 'fu' + i + '_email_sent'

                fuemailsent_list.append(followup)

                fu_check = part.followup

            if fu_check == None:
                fuemailsent_dict[followup] = 0  
            else:
                fuemailsent_dict[followup] = 1

The error I'm getting is:

AttributeError: 'emailJobs' object has no attribute 'followup'

It doesn't like: fu_check = part.followup ...but I don't know how else to do this.

Upvotes: 0

Views: 109

Answers (1)

Tim Hoffman
Tim Hoffman

Reputation: 12986

 for i in range(1, fup + 1):
        i = str(i)
        followup = 'fu' + i + '_email_sent'

        fuemailsent_list.append(followup)

        fu_check = part.followup

Means you are trying to access an attribute called 'followup'. with line fu_check = part.followup. To use the name stored in the variable followup use getattr

fu_check = getattr(part,followup)

Upvotes: 1

Related Questions