Пример #1
0
    def add_participant(self, user_id):
        if self.slots <= 0:
            raise ValueError("no slots left")

        db.execute('BEGIN')
        query = ('INSERT INTO lunch_participants (lunch_id, user_id) '
                'VALUES (%s, %s)')
        params = (int(self.id), int(user_id))
        id = db.execute(query, *params)
        try:
            db.execute('UPDATE lunch SET slots=slots-1 WHERE id = %s',
                       int(self.id))
        except:
            db.execute('ROLLBACK')
            return None

        db.execute('COMMIT')
        return id
Пример #2
0
    def create(cls, food, place, lunch_date, creator_id, slots, tags=[]):
        query = ('INSERT INTO lunch '
                '(food, place, lunch_date, creator_id, slots) '
                'VALUES (%s, %s, %s, %s, %s)')
        params = (food, place, lunch_date, creator_id, slots)
        db.execute('BEGIN')
        id = db.execute(query, *params)

        new_tags = []
        try:
            for tag in tags:
                new_tags.append(LunchTag.create(id, tag))
        except:
            db.execute('ROLLBACK')
            return None
        db.execute('COMMIT')
        return cls(id, food, place, lunch_date, creator_id, tags)
Пример #3
0
 def create(cls, lunch_id, tag):
     query = 'INSERT INTO lunch_tags (lunch_id, tag) VALUES (%s, %s)'
     params = (lunch_id, tag)
     id = db.execute(query, *params)
     return cls(id, lunch_id, tag)
Пример #4
0
 def create(cls, id, name, avatar_url, access_key, access_secret, handle):
     query = ('INSERT INTO users (id, name, avatar_url, access_key, '
              'access_secret, handle) VALUES (%s, %s, %s, %s, %s, %s)')
     params = (id, name, avatar_url, access_key, access_secret, handle)
     db.execute(query, *params)
     return cls(id, name, avatar_url, access_key, access_secret, handle)