Jesse
Jesse

Reputation: 277

Data modelling Appengine

Currently I have 3 datastore classes

User Event Workshop

Each workshop is referenced to an Event and each event only has four workshops

In the user class I have a list property where I reference the keys of the workshops that each user is a part of.

I want each user to only be part of one workshop event.

@classmethod
def event_attendance(self, user):
    workshop1 = self.workshops.filter('workshop_number =', 1).get(keys_only = True)
    users1 = User.all().filter('attendance =', workshop1.key()).fetch(149)
    if user in users1:
        return True
    else:
        workshop2 = self.workshops.filter('workshop_number =', 2).get(keys_only = True)
        users2 = User.all().filter('attendance =', workshop2.key()).fetch(149)
        if user in users2:
            return True
        else:
            workshop3 = self.workshops.filter('workshop_number =', 3).get(keys_only = True)
            users3 = User.all().filter('attendance =', workshop3.key()).fetch(149)
            if user in users3:
               return True
            else:
                workshop4 = self.workshops.filter('workshop_number =', 4).get(keys_only = True)
                users4 = User.all().filter('attendance =', workshop4.key()).fetch(149)
                if user in users4:
                    return True
                else:
                    return False

here I go over an event to see wether the user is enrolled in either of the workshops and was with the method below find out if they were enrolled in a workshop in this event to find which one.

@classmethod
def My_Event(self, user):
    workshop1 = self.workshops.filter('workshop_number =', 1).get(keys_only = True)
    users1 = User.all().filter('attendance =', workshop1.key()).fetch(149)
    if user in users1:
        return workshop1
    else:
        workshop2 = self.workshops.filter('workshop_number =', 2).get(keys_only = True)
        users2 = User.all().filter('attendance =', workshop2.key()).fetch(149)
        if user in users2:
            return workshop2
        else:
            workshop3 = self.workshops.filter('workshop_number =', 3).get(keys_only = True)
            users3 = User.all().filter('attendance =', workshop3.key()).fetch(149)
            if user in users3:
               return workshop3
            else:
                workshop4 = self.workshops.filter('workshop_number =', 4).get(keys_only = True)
                users4 = User.all().filter('attendance =', workshop4.key()).fetch(149)
                if user in users4:
                    return workshop4

I am very new to appengine in fact new to programming (mostly self taught)

Considering I have to go over every event in this way to see if a user is attached, this feels very expensive, is there a way of doing this more effectively with my current data modelling structure or should I change it???

Upvotes: 0

Views: 71

Answers (1)

T. Steinrücken
T. Steinrücken

Reputation: 469

First, you use @classmethod. Make sure this is what you want (this looks pretty strange in that context). For the rest: In both functions, you have your users-object. Why dont you just fetch that user, inspect its attendance attribute, and fetch only the workshops listed there? (If fetching the workshops is needed at all, for "def event_attendance" checking if the user has a non-empty list of attendance's might do the trick)

Upvotes: 0

Related Questions