def save(self, *args, **kwargs): """Create post event data report.""" super(PostEventForm, self).save() event = self.instance activity = Activity.objects.get(name=ACTIVITY_POST_EVENT_METRICS) reports = NGReport.objects.filter(event=event, activity=activity) if not reports: up = event.owner.userprofile attrs = { 'activity': activity, 'report_date': get_date(), 'longitude': up.lon, 'latitude': up.lat, 'location': '%s, %s, %s' % (up.city, up.region, up.country), 'link': get_event_link(event), 'is_passive': True, 'event': event, 'user': event.owner } report = NGReport.objects.create(**attrs) report.functional_areas.add(*event.categories.all()) statsd.incr('reports.create_passive_post_event_metrics')
def create_passive_attendance_report(sender, instance, **kwargs): """Automatically create a passive report after event attendance save.""" from remo.events.helpers import get_event_link if instance.user.groups.filter(name='Rep').exists(): activity = Activity.objects.get(name=ACTIVITY_EVENT_ATTEND) attrs = { 'user': instance.user, 'event': instance.event, 'activity': activity, 'report_date': instance.event.start.date(), 'longitude': instance.event.lon, 'latitude': instance.event.lat, 'location': "%s, %s, %s" % (instance.event.city, instance.event.region, instance.event.country), 'is_passive': True, 'country': instance.event.country, 'link': get_event_link(instance.event), 'activity_description': instance.event.description } report = NGReport.objects.create(**attrs) report.functional_areas.add(*instance.event.categories.all()) statsd.incr('reports.create_passive_attendance')
def create_update_passive_event_report(sender, instance, created, **kwargs): """Automatically create/update a passive report on event creation.""" from remo.events.helpers import get_event_link attrs = { 'report_date': instance.start.date(), 'longitude': instance.lon, 'latitude': instance.lat, 'location': "%s, %s, %s" % (instance.city, instance.region, instance.country), 'link': get_event_link(instance), 'activity_description': instance.description, 'is_passive': True, 'event': instance, 'campaign': instance.campaign, 'country': instance.country } if created: activity = Activity.objects.get(name=ACTIVITY_EVENT_CREATE) attrs.update({'user': instance.owner, 'activity': activity}) NGReport.objects.create(**attrs) statsd.incr('reports.create_passive_event') else: reports = (NGReport.objects.filter(event=instance) .exclude(activity__name=ACTIVITY_POST_EVENT_METRICS)) reports.update(**attrs) # Change user and mentor to the appropriate reports attrs.update({'user': instance.owner, 'mentor': instance.owner.userprofile.mentor}) reports.exclude(activity__name=ACTIVITY_EVENT_ATTEND).update(**attrs) statsd.incr('reports.update_passive_event')
def save(self, *args, **kwargs): """Create post event data report.""" event = super(PostEventForm, self).save() activity = Activity.objects.get(name=ACTIVITY_POST_EVENT_METRICS) reports = NGReport.objects.filter(event=event, activity=activity) if not reports: up = event.owner.userprofile attrs = { "activity": activity, "report_date": get_date(), "longitude": up.lon, "latitude": up.lat, "location": "%s, %s, %s" % (up.city, up.region, up.country), "link": get_event_link(event), "is_passive": True, "event": event, "user": event.owner, } report = NGReport.objects.create(**attrs) report.functional_areas.add(*event.categories.all()) statsd.incr("reports.create_passive_post_event_metrics") return event
def create_update_passive_event_report(sender, instance, created, **kwargs): """Automatically create/update a passive report on event creation.""" attrs = { 'report_date': instance.start.date(), 'longitude': instance.lon, 'latitude': instance.lat, 'location': "%s, %s, %s" % (instance.city, instance.region, instance.country), 'link': get_event_link(instance), 'activity_description': instance.description} if created: activity = Activity.objects.get(name=ACTIVITY_EVENT_CREATE) attrs.update({ 'user': instance.owner, 'event': instance, 'activity': activity, 'is_passive': True}) report = NGReport.objects.create(**attrs) report.functional_areas.add(*instance.categories.all()) statsd.incr('reports.create_passive_event') else: NGReport.objects.filter(event=instance).update(**attrs) statsd.incr('reports.update_passive_event')
def create_update_passive_event_report(sender, instance, created, **kwargs): """Automatically create/update a passive report on event creation.""" from remo.events.helpers import get_event_link attrs = { 'report_date': instance.start.date(), 'longitude': instance.lon, 'latitude': instance.lat, 'location': "%s, %s, %s" % (instance.city, instance.region, instance.country), 'link': get_event_link(instance), 'activity_description': instance.description} if created: activity = Activity.objects.get(name=ACTIVITY_EVENT_CREATE) attrs.update({ 'user': instance.owner, 'event': instance, 'activity': activity, 'is_passive': True}) report = NGReport.objects.create(**attrs) report.functional_areas.add(*instance.categories.all()) statsd.incr('reports.create_passive_event') else: reports = (NGReport.objects.filter(event=instance) .exclude(activity__name=ACTIVITY_POST_EVENT_METRICS)) reports.update(**attrs) statsd.incr('reports.update_passive_event')
def update_passive_report_event(sender, instance, **kwargs): """Automatically update passive report's event related fields.""" attrs = { 'report_date': instance.start, 'longitude': instance.lon, 'latitude': instance.lat, 'location': instance.venue, 'event': instance, 'link': get_event_link(instance)} NGReport.objects.filter(event=instance).update(**attrs)
def test_create_passive_event_attendance_report_owner(self): """Test creating a passive attendance report for event owner.""" user = UserFactory.create(groups=['Rep', 'Mentor'], userprofile__initial_council=True) event = EventFactory.create(owner=user) report = NGReport.objects.get(event=event, user=user) eq_(report.mentor, user.userprofile.mentor) eq_(report.activity.name, EVENT_ATTENDANCE_ACTIVITY) eq_(report.latitude, event.lat) eq_(report.longitude, event.lon) eq_(report.location, event.venue) eq_(report.is_passive, True) eq_(report.link, get_event_link(event)) eq_(report.activity_description, event.description) self.assertQuerysetEqual(report.functional_areas.all(), [e.name for e in event.categories.all()], lambda x: x.name)
def create_update_passive_event_report(sender, instance, created, **kwargs): """Automatically create/update a passive report on event creation.""" from remo.events.helpers import get_event_link attrs = { 'report_date': instance.start.date(), 'longitude': instance.lon, 'latitude': instance.lat, 'location': "%s, %s, %s" % (instance.city, instance.region, instance.country), 'link': get_event_link(instance), 'activity_description': instance.description, 'is_passive': True, 'event': instance, 'campaign': instance.campaign, 'country': instance.country } if created: activity = Activity.objects.get(name=ACTIVITY_EVENT_CREATE) attrs.update({'user': instance.owner, 'activity': activity}) NGReport.objects.create(**attrs) statsd.incr('reports.create_passive_event') else: reports = (NGReport.objects.filter(event=instance).exclude( activity__name=ACTIVITY_POST_EVENT_METRICS)) reports.update(**attrs) # Change user and mentor to the appropriate reports attrs.update({ 'user': instance.owner, 'mentor': instance.owner.userprofile.mentor }) reports.exclude(activity__name=ACTIVITY_EVENT_ATTEND).update(**attrs) statsd.incr('reports.update_passive_event')
def create_passive_attendance_report(sender, instance, **kwargs): """Automatically create a passive report after event attendance save.""" if instance.user.groups.filter(name='Rep').exists(): activity = Activity.objects.get(name=EVENT_ATTENDANCE_ACTIVITY) attrs = { 'user': instance.user, 'event': instance.event, 'activity': activity, 'report_date': instance.event.start, 'mentor': instance.user.userprofile.mentor, 'longitude': instance.event.lon, 'latitude': instance.event.lat, 'location': instance.event.venue, 'is_passive': True, 'link': get_event_link(instance.event), 'activity_description': instance.event.description} report = NGReport.objects.create(**attrs) report.functional_areas.add(*instance.event.categories.all())
def test_create(self): """Test creating a passive report after creating an event.""" activity = Activity.objects.get(name=ACTIVITY_EVENT_CREATE) event = EventFactory.build() event.owner = UserFactory.create() event.save() report = NGReport.objects.get(event=event, user=event.owner, activity=activity) location = '%s, %s, %s' % (event.city, event.region, event.country) eq_(report.mentor, event.owner.userprofile.mentor) eq_(report.activity.name, ACTIVITY_EVENT_CREATE) eq_(report.latitude, event.lat) eq_(report.longitude, event.lon) eq_(report.location, location) eq_(report.is_passive, True) eq_(report.link, get_event_link(event)) eq_(report.activity_description, event.description) self.assertQuerysetEqual(report.functional_areas.all(), [e.name for e in event.categories.all()], lambda x: x.name)
def test_create_event_report(self): """Test creating a passive report after creating an event.""" activity = Activity.objects.get(name=ACTIVITY_EVENT_CREATE) event = EventFactory.build() event.owner = UserFactory.create() event.save() report = NGReport.objects.get(event=event, user=event.owner, activity=activity) location = '%s, %s, %s' % (event.city, event.region, event.country) eq_(report.mentor, event.owner.userprofile.mentor) eq_(report.activity.name, ACTIVITY_EVENT_CREATE) eq_(report.latitude, event.lat) eq_(report.longitude, event.lon) eq_(report.location, location) eq_(report.is_passive, True) eq_(report.link, get_event_link(event)) eq_(report.activity_description, event.description) self.assertQuerysetEqual(report.functional_areas.all(), [e.name for e in event.categories.all()], lambda x: x.name)
def test_create_attendance_report_owner(self): """Test creating a passive attendance report for event owner.""" activity = Activity.objects.get(name=ACTIVITY_EVENT_ATTEND) user = UserFactory.create(groups=['Rep', 'Mentor'], userprofile__initial_council=True) event = EventFactory.create(owner=user) report = NGReport.objects.get(event=event, user=user, activity=activity) location = '%s, %s, %s' % (event.city, event.region, event.country) eq_(report.mentor, user.userprofile.mentor) eq_(report.activity.name, ACTIVITY_EVENT_ATTEND) eq_(report.latitude, event.lat) eq_(report.longitude, event.lon) eq_(report.location, location) eq_(report.is_passive, True) eq_(report.link, get_event_link(event)) eq_(report.activity_description, event.description) self.assertQuerysetEqual(report.functional_areas.all(), [e.name for e in event.categories.all()], lambda x: x.name)
def test_attendee(self): """Test creating a passive report after attending an event.""" activity = Activity.objects.get(name=ACTIVITY_EVENT_ATTEND) event = EventFactory.create() user = UserFactory.create(groups=['Rep', 'Mentor'], userprofile__initial_council=True) AttendanceFactory.create(event=event, user=user) report = NGReport.objects.get(event=event, user=user, activity=activity) location = '%s, %s, %s' % (event.city, event.region, event.country) eq_(report.mentor, user.userprofile.mentor) eq_(report.activity.name, ACTIVITY_EVENT_ATTEND) eq_(report.latitude, event.lat) eq_(report.longitude, event.lon) eq_(report.location, location) eq_(report.is_passive, True) eq_(report.link, get_event_link(event)) eq_(report.activity_description, event.description) self.assertQuerysetEqual(report.functional_areas.all(), [e.name for e in event.categories.all()], lambda x: x.name)
def create_passive_attendance_report(sender, instance, **kwargs): """Automatically create a passive report after event attendance save.""" from remo.events.helpers import get_event_link if instance.user.groups.filter(name='Rep').exists(): activity = Activity.objects.get(name=ACTIVITY_EVENT_ATTEND) attrs = { 'user': instance.user, 'event': instance.event, 'activity': activity, 'report_date': instance.event.start.date(), 'longitude': instance.event.lon, 'latitude': instance.event.lat, 'location': "%s, %s, %s" % (instance.event.city, instance.event.region, instance.event.country), 'is_passive': True, 'link': get_event_link(instance.event), 'activity_description': instance.event.description} report = NGReport.objects.create(**attrs) report.functional_areas.add(*instance.event.categories.all()) statsd.incr('reports.create_passive_attendance')
def forwards(self, orm): """Data migration from old report system to continuous reporting.""" Activity = orm['reports.Activity'] Attendance = orm['events.Attendance'] Event = orm['events.Event'] NGReport = orm['reports.NGReport'] Report = orm['reports.Report'] UserProfile = orm['profiles.UserProfile'] event_attendance = Activity.objects.get(name=ACTIVITY_EVENT_ATTEND) event_creation = Activity.objects.get(name=ACTIVITY_EVENT_CREATE) month_planning = Activity.objects.get_or_create(name='Month planning')[0] month_recap = Activity.objects.get_or_create(name='Month recap')[0] recruitment_effort = Activity.objects.get_or_create(name='Recruitment effort')[0] # Delete all temporary NGReport objects NGReport.objects.all().delete() # Delete streak data from user profiles for profile in UserProfile.objects.all(): profile.current_streak_start = None profile.longest_streak_start = None profile.longest_streak_end = None profile.save() # Migrate events in old reports each one in a new report for event in Event.objects.all(): args = {} args['user'] = event.owner args['report_date'] = event.start.date() args['mentor'] = event.owner.userprofile.mentor args['activity'] = event_creation args['longitude'] = event.lon args['latitude'] = event.lat args['location'] = '%s, %s, %s' % (event.city, event.region, event.country) args['event'] = event args['link'] = get_event_link(event) args['activity_description'] = event.description args['is_passive'] = True try: report = NGReport.objects.create(**args) report.functional_areas.add(*event.categories.all()) except Exception as e: print 'Error encountered while migrating Event object: %d' % event.id print e for attendance in Attendance.objects.filter(event=event, user__groups__name='Rep'): args = {} args['user'] = attendance.user args['report_date'] = attendance.event.start.date() args['mentor'] = attendance.user.userprofile.mentor args['activity'] = event_attendance args['longitude'] = attendance.event.lon args['latitude'] = attendance.event.lat args['location'] = '%s, %s, %s' % (attendance.event.city, attendance.event.region, attendance.event.country) args['event'] = attendance.event args['link'] = get_event_link(attendance.event) args['activity_description'] = attendance.event.description args['is_passive'] = True try: report = NGReport.objects.create(**args) report.functional_areas.add(*attendance.event.categories.all()) except Exception as e: print 'Error encountered while migrating Report object: %d' % attendance.id print e for report in Report.objects.all(): # Migrate recruit_comment to a new activity: "Recruitment effort" if report.recruits_comments: year = report.month.year month = report.month.month args = {} args['user'] = report.user args['report_date'] = date(year, month, 1) - timedelta(days=1) args['mentor'] = report.mentor args['activity'] = recruitment_effort args['longitude'] = report.user.userprofile.lon args['latitude'] = report.user.userprofile.lat args['location'] = '%s, %s, %s' % (report.user.userprofile.city, report.user.userprofile.region, report.user.userprofile.country) args['activity_description'] = report.recruits_comments try: ngreport = NGReport.objects.create(**args) ngreport.functional_areas.add(*report.user.userprofile.functional_areas.all()) except Exception as e: print 'Error encountered while migrating Report object: %d' % report.id print e # Migrate past activities to a new activity: "Month Recap" if report.past_items: year = report.month.year month = report.month.month args = {} args['user'] = report.user args['report_date'] = date(year, month, 1) - timedelta(days=1) args['mentor'] = report.mentor args['activity'] = month_recap args['longitude'] = report.user.userprofile.lon args['latitude'] = report.user.userprofile.lat args['location'] = '%s, %s, %s' % (report.user.userprofile.city, report.user.userprofile.region, report.user.userprofile.country) args['activity_description'] = report.past_items try: ngreport = NGReport.objects.create(**args) ngreport.functional_areas.add(*report.user.userprofile.functional_areas.all()) except Exception as e: print 'Error encountered while migrating Report object: %d' % report.id print e # Migrate future activities to a new activity: "Month planning" if report.future_items: args = {} args['user'] = report.user args['report_date'] = report.month args['mentor'] = report.mentor args['activity'] = month_planning args['longitude'] = report.user.userprofile.lon args['latitude'] = report.user.userprofile.lat args['location'] = '%s, %s, %s' % (report.user.userprofile.city, report.user.userprofile.region, report.user.userprofile.country) args['activity_description'] = report.future_items try: ngreport = NGReport.objects.create(**args) ngreport.functional_areas.add(*report.user.userprofile.functional_areas.all()) except Exception as e: print 'Error encountered while migrating Report object: %d' % report.id print e # Migrate report activities with links for activity in report.reportlink_set.all(): year = report.month.year month = report.month.month args = {} args['user'] = activity.report.user args['report_date'] = date(year, month, 1) - timedelta(days=1) args['mentor'] = activity.report.mentor args['activity'] = month_recap args['longitude'] = activity.report.user.userprofile.lon args['latitude'] = activity.report.user.userprofile.lat args['location'] = '%s, %s, %s' % (activity.report.user.userprofile.city, activity.report.user.userprofile.region, activity.report.user.userprofile.country) args['link'] = activity.link args['link_description'] = activity.description try: ngreport = NGReport.objects.create(**args) ngreport.functional_areas.add(*report.user.userprofile.functional_areas.all()) except Exception as e: print 'Error encountered while migrating ReportLink objects: %d' % activity.id print e