Example #1
0
 def _get_contacts(self, type):
     try:
         return self.contacts.filter(Contact.type == type)\
             .filter(Contact.belongs_to_user == True)\
             .order_by(db.desc(Contact.is_primary))
     except sqlalchemy.orm.exc.DetachedInstanceError:
         return None
Example #2
0
 def _get_contacts(self, type):
     try:
         return self.contacts.filter(Contact.type == type)\
             .filter(Contact.belongs_to_user == True)\
             .order_by(db.desc(Contact.is_primary))
     except sqlalchemy.orm.exc.DetachedInstanceError:
         return None
Example #3
0
 def events_cancelled(self):
     return self.events\
         .filter(Event.cancelled_at != None)\
         .order_by(db.desc(Event.cancelled_at), Event.name)
Example #4
0
 def events_active(self):
     return self.events\
         .filter(Event.cancelled_at == None)\
         .order_by(db.desc(Event.updated_at), Event.name)
Example #5
0
class Event(db.Model):
    """Event."""

    default_end_time_offset = 2  # in hours

    id = db.Column(db.Integer, primary_key=True)
    facebook_id = db.Column(db.String(100), nullable=True)
    google_id = db.Column(db.String(100), nullable=True)
    name = db.Column(db.String(200), nullable=False)
    description = db.Column(db.Text())
    venue = db.Column(db.String(200))
    user_id = db.Column(db.Integer(),
                        db.ForeignKey('user.id', ondelete='cascade'),
                        nullable=False)
    user = db.relationship('User',
                           backref=db.backref('events',
                                              cascade='all',
                                              lazy='dynamic'))
    attendance = db.relationship('Attendance', cascade='all')
    created_at = db.Column(db.DateTime(),
                           nullable=False,
                           default=lambda: times.now())
    updated_at = db.Column(db.DateTime(),
                           nullable=False,
                           default=lambda: times.now(),
                           onupdate=lambda: times.now())
    cancelled_at = db.Column(db.DateTime())
    starts_at = db.Column(db.DateTime())
    _ends_at = db.Column('ends_at', db.DateTime())

    __mapper_args__ = {
        'order_by': db.desc(created_at),
    }

    def set_attendance(self, contact, type):
        att = Attendance.query.filter(Attendance.contact_id == contact.id)\
            .filter(Attendance.event_id == self.id)\
            .first()
        if not att:
            att = Attendance(contact=contact, event=self)
            att.type = type
            db.session.add(att)
        else:
            att.type = type

    def set_invitation_sent(self, contact):
        att = Attendance.query.filter(Attendance.contact_id == contact.id)\
            .filter(Attendance.event_id == self.id)\
            .first()
        if att:
            att.invitation_sent = True

    @property
    def contacts(self):
        return Contact.query.join(Attendance)\
            .filter(Attendance.event_id == self.id)

    def is_facebook_involved(self):
        fb_contacts = list(FacebookContact.query.join(Attendance)\
            .filter(Attendance.event_id == self.id))
        return bool(fb_contacts or self.facebook_id)

    def is_google_involved(self):
        g_contacts = list(GoogleContact.query.join(Attendance)\
            .filter(Attendance.event_id == self.id))
        return bool(g_contacts or self.google_id)

    def is_email_involved(self):
        return bool(list(EmailContact.query.join(Attendance)\
            .filter(Attendance.event_id == self.id)))

    @property
    def contacts_facebook_to_invite(self):
        return FacebookContact.query.join(Attendance)\
            .filter(Attendance.event_id == self.id)\
            .filter(FacebookContact.belongs_to_user == False)\
            .filter(Attendance.invitation_sent == False)

    @property
    def contacts_google_to_invite(self):
        return GoogleContact.query.join(Attendance)\
            .filter(Attendance.event_id == self.id)\
            .filter(GoogleContact.belongs_to_user == False)\
            .filter(Attendance.invitation_sent == False)

    @property
    def contacts_email_to_invite(self):
        return EmailContact.query.join(Attendance)\
            .filter(Attendance.event_id == self.id)\
            .filter(EmailContact.belongs_to_user == False)\
            .filter(Attendance.invitation_sent == False)

    @property
    def contacts_invited(self):
        return self.contacts.filter(Attendance.type == 'invited')

    @contacts_invited.setter
    def contacts_invited(self, contacts):
        for contact in contacts:
            self.set_attendance(contact, 'invited')

    @property
    def contacts_invited_ids_str(self):
        return ','.join([c.id for c in self.contacts_invited])

    @contacts_invited_ids_str.setter
    def contacts_invited_ids_str(self, ids_str):
        ids = set(filter(None, ids_str.split(',')))
        self.contacts_invited = list(Contact.query.filter(Contact.id.in_(ids)))

    @property
    def contacts_going(self):
        return self.contacts.filter(Attendance.type == 'going')

    @property
    def contacts_maybe(self):
        return self.contacts.filter(Attendance.type == 'maybe')

    @property
    def contacts_declined(self):
        return self.contacts.filter(Attendance.type == 'declined')

    @property
    def is_cancelled(self):
        return self.cancelled_at is not None

    @property
    def ends_at(self):
        if self._ends_at:
            return self._ends_at
        if self.starts_at:
            return (self.starts_at +
                    datetime.timedelta(hours=self.default_end_time_offset))
        return None

    @ends_at.setter
    def ends_at(self, value):
        self._ends_at = value

    @property
    def verbose_name(self):
        verbose_name = self.name
        if self.is_cancelled:
            verbose_name += ' (cancelled)'
        return verbose_name

    @classmethod
    def fetch_or_404(self, id):
        """Returns event by given ID or aborts the request."""
        return Event.query.filter(Event.id == id).first_or_404()

    def __repr__(self):
        return '<Event %r>' % self.name
Example #6
0
 def events_cancelled(self):
     return self.events\
         .filter(Event.cancelled_at != None)\
         .order_by(db.desc(Event.cancelled_at), Event.name)
Example #7
0
 def events_active(self):
     return self.events\
         .filter(Event.cancelled_at == None)\
         .order_by(db.desc(Event.updated_at), Event.name)