'Write your forwards methods here.'

def forwards(self, orm):

    for affecteduser in orm['sentry.AffectedUserByGroup'].objects.filter(ident__isnull=False):
        if affecteduser.ident.startswith('email:'):
            email = affecteduser.ident.split('email:', 1)[(-1)]
        else:
            email = None
        (tuser, created) = orm['sentry.TrackedUser'].objects.get_or_create(project=affecteduser.project, ident=affecteduser.ident, defaults={'last_seen': affecteduser.last_seen, 'first_seen': affecteduser.first_seen, 'num_events': affecteduser.times_seen, 'email': email})
        if (not created):
            kwargs = {'num_events': (F('num_events') + affecteduser.times_seen)}
            if (affecteduser.last_seen > tuser.last_seen):
                kwargs['last_seen'] = affecteduser.last_seen
            if (affecteduser.first_seen > tuser.first_seen):
                kwargs['first_seen'] = affecteduser.first_seen
            orm['sentry.TrackedUser'].objects.filter(id=tuser.id).update(**kwargs)
        orm['sentry.AffectedUserByGroup'].objects.filter(id=affecteduser.id).update(tuser=tuser, ident=None)
