def get_mentee_reports_for_month(user, dateobj=None): """Return a dictionary with Mentee reports for month in dateobj. If dateobj==None return the reports of the previous month. """ if not dateobj: one_month_before = go_back_n_months(date.today(), first_day=True) else: one_month_before = dateobj two_months_before = go_back_n_months(one_month_before, first_day=True) mentees = get_mentees(user) mentees_list = {'month': one_month_before.strftime("%B %Y"), 'reports': []} for mentee in mentees: month_first_report = get_month_first_report(mentee) current_report = get_object_or_none(Report, user=mentee, month=one_month_before) previous_report = get_object_or_none(Report, user=mentee, month=two_months_before) if not previous_report and two_months_before >= month_first_report: status = 'notfilled' elif current_report and current_report.empty: status = 'empty' else: status = '' mentees_list['reports'].append({'user': mentee, 'status': status, 'report': current_report}) return mentees_list
def get_nominee(full_name): first_name, last_name = full_name.rsplit(' ', 1) q_params = {'first_name': first_name, 'last_name': last_name, 'groups__name': 'Rep'} user = get_object_or_none(User, **q_params) if not user: q_params['first_name'], q_params['last_name'] = full_name.split(' ', 1) return get_object_or_none(User, **q_params)
def fetch_bugs(components=COMPONENTS, days=None): """Fetch all bugs from Bugzilla. Loop over components and fetch bugs updated the last days. Link Bugzilla users with users on this website, when possible. """ now = timezone.now() if not days: days = (now - get_last_updated_date()).days + 1 for component in COMPONENTS: url = URL.format(username=settings.REMOZILLA_USERNAME, password=settings.REMOZILLA_PASSWORD, component=quote(component), fields=','.join(BUGZILLA_FIELDS), timedelta=days) response = requests.get(url) if response.status_code != 200: raise ValueError('Invalid response from server.') bugs = json.loads(response.text) for bdata in bugs['bugs']: bug, created = Bug.objects.get_or_create(bug_id=bdata['id']) bug.summary = bdata.get('summary', '') bug.creator = get_object_or_none(User, email=bdata['creator']['name']) bug.bug_creation_time = datetime.strptime(bdata['creation_time'], '%Y-%m-%dT%H:%M:%SZ') bug.component = bdata['component'] bug.whiteboard = bdata.get('whiteboard', '') bug.cc.clear() for person in bdata.get('cc', []): cc_user = get_object_or_none(User, email=person['name']) if cc_user: bug.cc.add(cc_user) bug.assigned_to = get_object_or_none(User, email=(bdata['assigned_to']\ ['name'])) bug.status = bdata['status'] bug.resolution = bdata.get('resolution', '') bug.due_date = bdata.get('cf_due_date', None) if 'last_change_time' in bdata: bug.bug_last_change_time = datetime.strptime( bdata['last_change_time'], '%Y-%m-%dT%H:%M:%SZ') bug.save() set_last_updated_date(now)
def email_mentor_notification(sender, instance, raw, **kwargs): """Notify mentor when his/her mentee changes mentor on his/her profile.""" if not instance.mentor: return user_profile = get_object_or_none(UserProfile, user=instance.user) if not user_profile or not user_profile.mentor or raw: return if user_profile.mentor != instance.mentor: subject = '[Reps] Mentor reassignment.' email_template = 'emails/mentor_change_notification.jinja' mentors_recipients = [user_profile.mentor.id, instance.mentor.id] rep_recipient = [instance.user.id] ctx_data = {'rep_user': instance.user, 'new_mentor': instance.mentor} send_remo_mail.delay(recipients_list=mentors_recipients, subject=subject, email_template=email_template, data=ctx_data, headers={'Reply-To': instance.user.email}) send_remo_mail.delay(recipients_list=rep_recipient, subject=subject, email_template=email_template, data=ctx_data, headers={'Reply-To': instance.mentor.email}) statsd.incr('profiles.change_mentor')
def _check_if_user_owns_page(): if owner_field and model: obj = get_object_or_none( model, **{filter_field: kwargs[filter_field]}) if obj and getattr(obj, owner_field) == request.user: return True return False
def get_mentee_reports_for_month(user, dateobj=None): """Return a dictionary with Mentee reports for month in dateobj. If dateobj==None return the reports of the previous month. """ if not dateobj: dateobj = go_back_n_months(date.today(), first_day=True) mentees = get_mentees(user) mentees_list = {'month': dateobj.strftime("%B %Y"), 'reports': []} for mentee in mentees: report = get_object_or_none(Report, user=mentee, month=dateobj) if report is None: status = 'notfilled' elif report.overdue: status = 'overdue' else: status = '' mentees_list['reports'].append({'user': mentee, 'status': status, 'report': report}) return mentees_list
def _check_if_user_owns_page(): if owner_field and model: obj = get_object_or_none(model, **{filter_field: kwargs[filter_field]}) if obj and getattr(obj, owner_field) == request.user: return True return False
def report_add_event(sender, instance, raw, **kwargs): """Add event to report.""" if raw or not instance.user.groups.filter(name='Rep').exists(): return date = datetime.datetime(year=instance.event.end.year, month=instance.event.end.month, day=1) report, created = Report.objects.get_or_create(user=instance.user, month=date) link = (settings.SITE_URL + reverse('events_view_event', kwargs={'slug': instance.event.slug})) # Import here to avoid circular dependencies. from utils import participation_type_to_number participation_type = participation_type_to_number( get_attendee_role_event(instance.user, instance.event)) report_event = get_object_or_none(ReportEvent, report=report, link=link) if not report_event: report_event = ReportEvent(report=report, link=link, name=instance.event.name, description=instance.event.description) report_event.participation_type = participation_type report_event.save()
def email_mentor_notification(sender, instance, raw, **kwargs): """Notify mentor when his/her mentee changes mentor on his/her profile.""" if not instance.mentor: return user_profile = get_object_or_none(UserProfile, user=instance.user) if not user_profile or not user_profile.mentor or raw: return if user_profile.mentor != instance.mentor: subject = '[Reps] Mentor reassignment.' email_template = 'emails/mentor_change_notification.txt' mentors_recipients = [user_profile.mentor.id, instance.mentor.id] rep_recipient = [instance.user.id] ctx_data = {'rep_user': instance.user, 'new_mentor': instance.mentor} send_remo_mail.delay(recipients_list=mentors_recipients, subject=subject, email_template=email_template, data=ctx_data, headers={'Reply-To': instance.user.email}) send_remo_mail.delay(recipients_list=rep_recipient, subject=subject, email_template=email_template, data=ctx_data, headers={'Reply-To': instance.mentor.email}) statsd.incr('profiles.change_mentor')
def report_remove_event(sender, instance, **kwargs): """Remove event from report.""" date = datetime.datetime(year=instance.event.end.year, month=instance.event.end.month, day=1) report = get_object_or_none(Report, user=instance.user, month=date) link = settings.SITE_URL + reverse("events_view_event", kwargs={"slug": instance.event.slug}) if report: report.reportevent_set.filter(link=link).delete()
def report_remove_event(sender, instance, **kwargs): """Remove event from report.""" date = datetime.datetime(year=instance.event.end.year, month=instance.event.end.month, day=1) report = get_object_or_none(Report, user=instance.user, month=date) link = (settings.SITE_URL + reverse('events_view_event', kwargs={'slug': instance.event.slug})) if report: report.reportevent_set.filter(link=link).delete()
def update_mentor_action_items(sender, instance, raw, **kwargs): """Update action items when a mentor change occurs.""" user_profile = get_object_or_none(UserProfile, user=instance.user) if user_profile and not raw: if user_profile.mentor and user_profile.mentor != instance.mentor: action_name = WAITING_MENTOR_VALIDATION_ACTION action_model = ContentType.objects.get_for_model(Bug) action_items = ActionItem.objects.filter(content_type=action_model, name=action_name, user=user_profile.mentor) action_items.update(user=instance.mentor)
def update_passive_report_event_owner(sender, instance, **kwargs): """Automatically update passive reports event owner.""" if instance.id: event = get_object_or_none(Event, pk=instance.id) if event and event.owner != instance.owner: attrs = { 'user': event.owner, 'event': instance, 'activity': Activity.objects.get(name=ACTIVITY_EVENT_CREATE)} mentor = instance.owner.userprofile.mentor NGReport.objects.filter(**attrs).update(user=instance.owner, mentor=mentor)
def current_report(request, edit=False): display_name = request.user.userprofile.display_name previous_month = utils.go_back_n_months(datetime.date.today(), first_day=True) month_name = utils.number2month(previous_month.month) report = utils.get_object_or_none( Report, user__userprofile__display_name=display_name, month=previous_month) view = 'reports_view_report' if edit or not report: view = 'reports_edit_report' redirect_url = reverse(view, kwargs={'display_name': display_name, 'year': previous_month.year, 'month': month_name}) return redirect(redirect_url)
def fetch_bugs(components=COMPONENTS, days=None): """Fetch all bugs from Bugzilla. Loop over components and fetch bugs updated the last days. Link Bugzilla users with users on this website, when possible. """ now = timezone.now() if not days: days = (now - get_last_updated_date()).days + 1 for component in components: offset = 0 url = URL.format(username=settings.REMOZILLA_USERNAME, password=settings.REMOZILLA_PASSWORD, component=quote(component), fields=','.join(BUGZILLA_FIELDS), timedelta=days, offset=offset, limit=LIMIT) while True: response = requests.get(url) if response.status_code != 200: raise ValueError('Invalid response from server.') bugs = json.loads(response.text) if not bugs['bugs']: break for bdata in bugs['bugs']: bug, created = Bug.objects.get_or_create(bug_id=bdata['id']) bug.summary = bdata.get('summary', '') creator_name = bdata['creator']['name'] bug.creator = get_object_or_none(User, email=creator_name) creation_time = datetime.strptime(bdata['creation_time'], '%Y-%m-%dT%H:%M:%SZ') bug.bug_creation_time = creation_time bug.component = bdata['component'] bug.whiteboard = bdata.get('whiteboard', '') bug.cc.clear() for person in bdata.get('cc', []): cc_user = get_object_or_none(User, email=person['name']) if cc_user: bug.cc.add(cc_user) bug.assigned_to = (get_object_or_none( User, email=(bdata['assigned_to']['name']))) bug.status = bdata['status'] bug.resolution = bdata.get('resolution', '') bug.due_date = bdata.get('cf_due_date', None) if 'last_change_time' in bdata: bug.bug_last_change_time = datetime.strptime( bdata['last_change_time'], '%Y-%m-%dT%H:%M:%SZ') flags = bdata.get('flags', []) bug.flag_status = next( (item['status'] for item in flags if item['status'] == '?'), '') bug.flag_name = next( (item['name'] for item in flags if item['name'] == 'remo-review'), '') comments = bdata.get('comments', []) if comments and comments[0].get('text', ''): bug.first_comment = comments[0]['text'] bug.save() offset += LIMIT url = urlparams(url, offset=offset) set_last_updated_date(now)
def fetch_bugs(components=COMPONENTS, days=None): """Fetch all bugs from Bugzilla. Loop over components and fetch bugs updated the last days. Link Bugzilla users with users on this website, when possible. """ login_url = LOGIN_URL.format(username=settings.REMOZILLA_USERNAME, password=settings.REMOZILLA_PASSWORD) response = requests.get(login_url).json() error = response.get('error') # Check the server response and get the token if error: raise ValueError('Invalid response from server, {0}.' .format(response['error'])) token = response['token'] now = timezone.now() if not days: changed_date = get_last_updated_date() else: changed_date = now - timedelta(int(days)) for component in components: offset = 0 url = URL.format(token=token, component=quote(component), fields=','.join(BUGZILLA_FIELDS), timestamp=changed_date, offset=offset, limit=LIMIT) while True: bugs = requests.get(url).json() error = bugs.get('error') # Check the server response and get the token if error: raise ValueError('Invalid response from server, {0}.' .format(bugs['message'])) remo_bugs = bugs.get('bugs', []) if not remo_bugs: break for bdata in remo_bugs: # Get comments for current bug comment_url = COMMENT_URL.format(id=bdata['id'], token=token) comments = requests.get(comment_url).json() error = comments.get('error') if error: raise ValueError('Invalid response from server, {0}.' .format(comments['message'])) bug, created = Bug.objects.get_or_create(bug_id=bdata['id']) bug.summary = unicode(bdata.get('summary', '')) creator_email = bdata['creator'] bug.creator = get_object_or_none(User, email=creator_email) bug.bug_creation_time = ( parse_bugzilla_time(bdata['creation_time'])) bug.component = bdata['component'] bug.whiteboard = bdata.get('whiteboard', '') bug.cc.clear() for email in bdata.get('cc', []): cc_user = get_object_or_none(User, email=email) if cc_user: bug.cc.add(cc_user) bug.assigned_to = get_object_or_none( User, email=bdata['assigned_to']) bug.status = bdata['status'] bug.resolution = bdata.get('resolution', '') bug.bug_last_change_time = parse_bugzilla_time( bdata.get('last_change_time')) automated_voting_trigger = 0 bug.budget_needinfo.clear() bug.council_member_assigned = False bug.pending_mentor_validation = False for flag in bdata.get('flags', []): if ((flag['status'] == '?' and flag['name'] == 'remo-approval')): automated_voting_trigger += 1 if 'Council Reviewer Assigned' in bug.whiteboard: bug.council_member_assigned = True if ((flag['status'] == '?' and flag['name'] == 'needinfo' and 'requestee' in flag and flag['requestee'] == (settings.REPS_COUNCIL_ALIAS))): automated_voting_trigger += 1 if flag['status'] == '?' and flag['name'] == 'remo-review': bug.pending_mentor_validation = True if (flag['status'] == '?' and flag['name'] == 'needinfo' and 'requestee' in flag): email = flag['requestee'] user = get_object_or_none(User, email=email) if user: bug.budget_needinfo.add(user) if ((automated_voting_trigger == 2 and waffle.switch_is_active('automated_polls'))): bug.council_vote_requested = True unicode_id = unicode(bdata['id']) bug_comments = comments['bugs'][unicode_id]['comments'] if bug_comments and bug_comments[0].get('text', ''): # Enforce unicode encoding. bug.first_comment = unicode(bug_comments[0]['text']) bug.save() offset += LIMIT url = urlparams(url, offset=offset) set_last_updated_date(now)
def fetch_bugs(components=COMPONENTS, days=None): """Fetch all bugs from Bugzilla. Loop over components and fetch bugs updated the last days. Link Bugzilla users with users on this website, when possible. """ login_url = LOGIN_URL.format(username=settings.REMOZILLA_USERNAME, password=settings.REMOZILLA_PASSWORD) response = requests.get(login_url).json() error = response.get('error') # Check the server response and get the token if error: raise ValueError('Invalid response from server, {0}.'.format( response['error'])) token = response['token'] now = timezone.now() if not days: changed_date = get_last_updated_date() else: changed_date = now - timedelta(int(days)) for component in components: offset = 0 url = URL.format(token=token, component=quote(component), fields=','.join(BUGZILLA_FIELDS), timestamp=changed_date, offset=offset, limit=LIMIT) while True: bugs = requests.get(url).json() error = bugs.get('error') # Check the server response and get the token if error: raise ValueError('Invalid response from server, {0}.'.format( bugs['message'])) remo_bugs = bugs.get('bugs', []) if not remo_bugs: break for bdata in remo_bugs: # Get comments for current bug comment_url = COMMENT_URL.format(id=bdata['id'], token=token) comments = requests.get(comment_url).json() error = comments.get('error') if error: raise ValueError( 'Invalid response from server, {0}.'.format( comments['message'])) bug, created = Bug.objects.get_or_create(bug_id=bdata['id']) bug.summary = unicode(bdata.get('summary', '')) creator_email = bdata['creator'] bug.creator = get_object_or_none(User, email=creator_email) bug.bug_creation_time = (parse_bugzilla_time( bdata['creation_time'])) bug.component = bdata['component'] bug.whiteboard = bdata.get('whiteboard', '') bug.cc.clear() for email in bdata.get('cc', []): cc_user = get_object_or_none(User, email=email) if cc_user: bug.cc.add(cc_user) bug.assigned_to = get_object_or_none( User, email=bdata['assigned_to']) bug.status = bdata['status'] bug.resolution = bdata.get('resolution', '') bug.bug_last_change_time = parse_bugzilla_time( bdata.get('last_change_time')) automated_voting_trigger = 0 bug.budget_needinfo.clear() bug.council_member_assigned = False bug.pending_mentor_validation = False for flag in bdata.get('flags', []): if ((flag['status'] == '?' and flag['name'] == 'remo-approval')): automated_voting_trigger += 1 if 'Council Reviewer Assigned' in bug.whiteboard: bug.council_member_assigned = True if ((flag['status'] == '?' and flag['name'] == 'needinfo' and 'requestee' in flag and flag['requestee'] == (settings.REPS_COUNCIL_ALIAS))): automated_voting_trigger += 1 if flag['status'] == '?' and flag['name'] == 'remo-review': bug.pending_mentor_validation = True if (flag['status'] == '?' and flag['name'] == 'needinfo' and 'requestee' in flag): email = flag['requestee'] user = get_object_or_none(User, email=email) if user: bug.budget_needinfo.add(user) if ((automated_voting_trigger == 2 and waffle.switch_is_active('automated_polls'))): bug.council_vote_requested = True unicode_id = unicode(bdata['id']) bug_comments = comments['bugs'][unicode_id]['comments'] if bug_comments and bug_comments[0].get('text', ''): # Enforce unicode encoding. bug.first_comment = unicode(bug_comments[0]['text']) bug.save() offset += LIMIT url = urlparams(url, offset=offset) set_last_updated_date(now)
def get_nominee(full_name): first_name, last_name = full_name.rsplit(' ', 1) q_params = {'first_name': first_name, 'last_name': last_name, 'groups__name': 'Rep'} return get_object_or_none(User, **q_params)
def save(self, *args, **kwargs): """Override save for custom functionality.""" # Make last report notificaton date NULL on new report today = get_date() if self.pk is None and self.user.userprofile.first_report_notification: start = today - datetime.timedelta(weeks=4) end = today + datetime.timedelta(weeks=4) if self.report_date in daterange(start, end): self.user.userprofile.first_report_notification = None self.user.userprofile.second_report_notification = None self.user.userprofile.save() # User streaks functionality one_week = datetime.timedelta(7) current_start = self.user.userprofile.current_streak_start or None longest_start = self.user.userprofile.longest_streak_start or None longest_end = self.user.userprofile.longest_streak_end or None # Save the mentor of the user if no mentor is defined. if not self.mentor: self.mentor = self.user.userprofile.mentor # Save the country if possible saved_report = get_object_or_none(NGReport, id=self.id) if (saved_report and (saved_report.latitude != self.latitude or saved_report.longitude != self.longitude)): country = None try: country = self.location.split(',')[-1].strip() except IndexError: pass if country in COUNTRIES_LIST: self.country = country super(NGReport, self).save() # Resolve the verified action items. action_item_name = u'{0} {1}'.format(VERIFY_ACTION, self.user.get_full_name()) if self.verified_activity: ActionItem.resolve(instance=self, user=self.mentor, name=action_item_name) else: # Create all the action items, if any ActionItem.create(self) if self.is_future_report: return # If there is already a running streak and the report date # is within this streak, update the current streak counter. if (current_start and self.report_date < current_start and self.report_date in daterange((current_start - one_week), current_start)): current_start = self.report_date # If there isn't any current streak, and the report date # is within the current week, let's start the counting. elif not current_start and self.report_date in daterange(get_date(-7), today): current_start = self.report_date # Longest streak section # If longest streak already exists, let's update it. if longest_start and longest_end: # Compare the number of reports registered during # the current streak and the number of reports # during the longest streak. If current streak is bigger # than the previous longest streak, update the longest streak. longest_streak_count = NGReport.objects.filter( report_date__range=(longest_start, longest_end), user=self.user).count() current_streak_count = NGReport.objects.filter( report_date__range=(current_start, today), user=self.user).count() if current_start and current_streak_count > longest_streak_count: longest_start = current_start longest_end = today # This happens only when a user appends a report, dated in the # range of longest streak counters and it's out of the range # of current streak counter. elif self.report_date in daterange(longest_start - one_week, longest_end + one_week): if self.report_date < longest_start: longest_start = self.report_date elif self.report_date > longest_end: longest_end = self.report_date else: # Longest streak counters are empty, let's setup their value longest_start = self.report_date longest_end = self.report_date # Assign the calculated values, to user's profile. self.user.userprofile.current_streak_start = current_start self.user.userprofile.longest_streak_start = longest_start self.user.userprofile.longest_streak_end = longest_end self.user.userprofile.save()
def fetch_bugs(components=COMPONENTS, days=None): """Fetch all bugs from Bugzilla. Loop over components and fetch bugs updated the last days. Link Bugzilla users with users on this website, when possible. """ now = timezone.now() if not days: days = (now - get_last_updated_date()).days + 1 for component in components: offset = 0 url = URL.format(username=settings.REMOZILLA_USERNAME, password=settings.REMOZILLA_PASSWORD, component=quote(component), fields=','.join(BUGZILLA_FIELDS), timedelta=days, offset=offset, limit=LIMIT) while True: response = requests.get(url) if response.status_code != 200: raise ValueError('Invalid response from server.') bugs = json.loads(response.text) if not bugs['bugs']: break for bdata in bugs['bugs']: bug, created = Bug.objects.get_or_create(bug_id=bdata['id']) bug.summary = bdata.get('summary', '') creator_name = bdata['creator']['name'] bug.creator = get_object_or_none(User, email=creator_name) creation_time = datetime.strptime(bdata['creation_time'], '%Y-%m-%dT%H:%M:%SZ') bug.bug_creation_time = creation_time bug.component = bdata['component'] bug.whiteboard = bdata.get('whiteboard', '') bug.cc.clear() for person in bdata.get('cc', []): cc_user = get_object_or_none(User, email=person['name']) if cc_user: bug.cc.add(cc_user) bug.assigned_to = ( get_object_or_none(User, email=(bdata['assigned_to']['name']))) bug.status = bdata['status'] bug.resolution = bdata.get('resolution', '') bug.due_date = bdata.get('cf_due_date', None) if 'last_change_time' in bdata: bug.bug_last_change_time = datetime.strptime( bdata['last_change_time'], '%Y-%m-%dT%H:%M:%SZ') flags = bdata.get('flags', []) bug.flag_status = next((item['status'] for item in flags if item['status'] == '?'), '') bug.flag_name = next((item['name'] for item in flags if item['name'] == 'remo-review'), '') comments = bdata.get('comments', []) if comments and comments[0].get('text', ''): bug.first_comment = comments[0]['text'] bug.save() offset += LIMIT url = urlparams(url, offset=offset) set_last_updated_date(now)
def fetch_bugs(components=COMPONENTS, days=None): """Fetch all bugs from Bugzilla. Loop over components and fetch bugs updated the last days. Link Bugzilla users with users on this website, when possible. # TODO: This can trigger a does not exist error because the task was picked # by the worker before the transaction was complete. Needs fixing after the # upgrade to a Django version > 1.8 """ now = timezone.now() if not days: changed_date = get_last_updated_date() else: changed_date = now - timedelta(int(days)) for component in components: offset = 0 url = URL.format(api_key=settings.REMOZILLA_API_KEY, component=quote(component), fields=','.join(BUGZILLA_FIELDS), timestamp=changed_date, offset=offset, limit=LIMIT) while True: bugs = requests.get(url).json() error = bugs.get('error') # Check the server response for errors if error: raise ValueError('Invalid response from server, {0}.'.format( bugs['message'])) remo_bugs = bugs.get('bugs', []) if not remo_bugs: break for bdata in remo_bugs: # Get comments for current bug comment_url = COMMENT_URL.format( id=bdata['id'], api_key=settings.REMOZILLA_API_KEY) comments = requests.get(comment_url).json() error = comments.get('error') if error: raise ValueError( 'Invalid response from server, {0}.'.format( comments['message'])) bug, created = Bug.objects.get_or_create(bug_id=bdata['id']) bug.summary = bdata.get('summary', '') creator_email = bdata['creator'] bug.creator = get_object_or_none(User, email=creator_email) bug.bug_creation_time = parse_bugzilla_time( bdata['creation_time']) bug.component = bdata['component'] bug.whiteboard = bdata.get('whiteboard', '') bug.cc.clear() for email in bdata.get('cc', []): cc_user = get_object_or_none(User, email=email) if cc_user: bug.cc.add(cc_user) bug.assigned_to = get_object_or_none( User, email=bdata['assigned_to']) bug.status = bdata['status'] bug.resolution = bdata.get('resolution', '') bug.bug_last_change_time = parse_bugzilla_time( bdata.get('last_change_time')) automated_voting_trigger = 0 bug.budget_needinfo.clear() bug.council_member_assigned = False bug.pending_mentor_validation = False for flag in bdata.get('flags', []): if flag['status'] == '?' and flag['name'] == BUG_APPROVAL: automated_voting_trigger += 1 if BUG_WHITEBOARD in bug.whiteboard: bug.council_member_assigned = True if ((flag['status'] == '?' and flag['name'] == 'needinfo' and 'requestee' in flag and flag['requestee'] == (settings.REPS_REVIEW_ALIAS))): automated_voting_trigger += 1 if flag['status'] == '?' and flag['name'] == BUG_REVIEW: bug.pending_mentor_validation = True if (flag['status'] == '?' and flag['name'] == 'needinfo' and 'requestee' in flag): email = flag['requestee'] user = get_object_or_none(User, email=email) if user: bug.budget_needinfo.add(user) if automated_voting_trigger == 2 and waffle.switch_is_active( 'automated_polls'): bug.council_vote_requested = True unicode_id = str(bdata['id']) bug_comments = comments['bugs'][unicode_id]['comments'] if bug_comments and bug_comments[0].get('text', ''): # Enforce unicode encoding. bug.first_comment = bug_comments[0]['text'] bug.save() offset += LIMIT url = urlparams(url, offset=offset) set_last_updated_date(now)
def clean_categories(self): category_id = self.cleaned_data['categories'] return get_object_or_none(FunctionalArea, id=category_id)
def save(self, *args, **kwargs): """Override save for custom functionality.""" # Make last report notificaton date NULL on new report today = get_date() if self.pk is None and self.user.userprofile.first_report_notification: start = today - datetime.timedelta(weeks=4) end = today + datetime.timedelta(weeks=4) if self.report_date in daterange(start, end): self.user.userprofile.first_report_notification = None self.user.userprofile.second_report_notification = None self.user.userprofile.save() # User streaks functionality one_week = datetime.timedelta(7) current_start = self.user.userprofile.current_streak_start or None longest_start = self.user.userprofile.longest_streak_start or None longest_end = self.user.userprofile.longest_streak_end or None # Save the mentor of the user if no mentor is defined. if not self.mentor: self.mentor = self.user.userprofile.mentor # Save the country if possible saved_report = get_object_or_none(NGReport, id=self.id) if (saved_report and (saved_report.latitude != self.latitude or saved_report.longitude != self.longitude)): country = None try: country = self.location.split(',')[-1].strip() except IndexError: pass if country in COUNTRIES_LIST: self.country = country super(NGReport, self).save() if self.is_future_report: return # If there is already a running streak and the report date # is within this streak, update the current streak counter. if (current_start and self.report_date < current_start and self.report_date in daterange( (current_start - one_week), current_start)): current_start = self.report_date # If there isn't any current streak, and the report date # is within the current week, let's start the counting. elif (not current_start and self.report_date in daterange(get_date(-7), today)): current_start = self.report_date # Longest streak section # If longest streak already exists, let's update it. if longest_start and longest_end: # Compare the number of reports registered during # the current streak and the number of reports # during the longest streak. If current streak is bigger # than the previous longest streak, update the longest streak. longest_streak_count = NGReport.objects.filter( report_date__range=(longest_start, longest_end), user=self.user).count() current_streak_count = NGReport.objects.filter( report_date__range=(current_start, today), user=self.user).count() if current_start and current_streak_count > longest_streak_count: longest_start = current_start longest_end = today # This happens only when a user appends a report, dated in the # range of longest streak counters and it's out of the range # of current streak counter. elif self.report_date in daterange(longest_start - one_week, longest_end + one_week): if self.report_date < longest_start: longest_start = self.report_date elif self.report_date > longest_end: longest_end = self.report_date else: # Longest streak counters are empty, let's setup their value longest_start = self.report_date longest_end = self.report_date # Assign the calculated values, to user's profile. self.user.userprofile.current_streak_start = current_start self.user.userprofile.longest_streak_start = longest_start self.user.userprofile.longest_streak_end = longest_end self.user.userprofile.save()
def fetch_bugs(components=COMPONENTS, days=None): """Fetch all bugs from Bugzilla. Loop over components and fetch bugs updated the last days. Link Bugzilla users with users on this website, when possible. """ now = timezone.now() if not days: days = (now - get_last_updated_date()).days + 1 for component in components: offset = 0 url = URL.format(username=settings.REMOZILLA_USERNAME, password=settings.REMOZILLA_PASSWORD, component=quote(component), fields=','.join(BUGZILLA_FIELDS), timedelta=days, offset=offset, limit=LIMIT) while True: response = requests.get(url) if response.status_code != 200: raise ValueError('Invalid response from server.') bugs = json.loads(response.text) if not bugs['bugs']: break for bdata in bugs['bugs']: bug, created = Bug.objects.get_or_create(bug_id=bdata['id']) bug.summary = bdata.get('summary', '') creator_name = bdata['creator']['name'] bug.creator = get_object_or_none(User, email=creator_name) bug.bug_creation_time = ( parse_bugzilla_time(bdata['creation_time'])) bug.component = bdata['component'] bug.whiteboard = bdata.get('whiteboard', '') bug.cc.clear() for person in bdata.get('cc', []): cc_user = get_object_or_none(User, email=person['name']) if cc_user: bug.cc.add(cc_user) bug.assigned_to = get_object_or_none( User, email=bdata['assigned_to']['name']) bug.status = bdata['status'] bug.resolution = bdata.get('resolution', '') bug.bug_last_change_time = parse_bugzilla_time( bdata.get('last_change_time')) automated_voting_trigger = 0 bug.budget_needinfo.clear() bug.council_member_assigned = False bug.pending_mentor_validation = False for flag in bdata.get('flags', []): if ((flag['status'] == '?' and flag['name'] == 'remo-approval')): automated_voting_trigger += 1 if 'Council Reviewer Assigned' in bug.whiteboard: bug.council_member_assigned = True if ((flag['status'] == '?' and flag['name'] == 'needinfo' and 'requestee' in flag and flag['requestee']['name'] == ( settings.REPS_COUNCIL_ALIAS))): automated_voting_trigger += 1 if ((flag['status'] == '?' and flag['name'] == 'remo-review')): bug.pending_mentor_validation = True if ((flag['status'] == '?' and flag['name'] == 'needinfo' and 'requestee' in flag and bug.component == 'Budget Requests')): email = flag['requestee']['name'] user = get_object_or_none(User, email=email) if user: bug.budget_needinfo.add(user) if ((automated_voting_trigger == 2 and waffle.switch_is_active('automated_polls'))): bug.council_vote_requested = True comments = bdata.get('comments', []) if comments and comments[0].get('text', ''): bug.first_comment = comments[0]['text'] bug.save() offset += LIMIT url = urlparams(url, offset=offset) set_last_updated_date(now)
def fetch_bugs(components=COMPONENTS, days=None): """Fetch all bugs from Bugzilla. Loop over components and fetch bugs updated the last days. Link Bugzilla users with users on this website, when possible. """ now = timezone.now() if not days: days = (now - get_last_updated_date()).days + 1 for component in components: offset = 0 url = URL.format(username=settings.REMOZILLA_USERNAME, password=settings.REMOZILLA_PASSWORD, component=quote(component), fields=','.join(BUGZILLA_FIELDS), timedelta=days, offset=offset, limit=LIMIT) while True: response = requests.get(url) if response.status_code != 200: raise ValueError('Invalid response from server.') bugs = json.loads(response.text) if not bugs['bugs']: break for bdata in bugs['bugs']: bug, created = Bug.objects.get_or_create(bug_id=bdata['id']) bug.summary = bdata.get('summary', '') creator_name = bdata['creator']['name'] bug.creator = get_object_or_none(User, email=creator_name) bug.bug_creation_time = ( parse_bugzilla_time(bdata['creation_time'])) bug.component = bdata['component'] bug.whiteboard = bdata.get('whiteboard', '') bug.cc.clear() for person in bdata.get('cc', []): cc_user = get_object_or_none(User, email=person['name']) if cc_user: bug.cc.add(cc_user) bug.assigned_to = get_object_or_none( User, email=bdata['assigned_to']['name']) bug.status = bdata['status'] bug.resolution = bdata.get('resolution', '') bug.bug_last_change_time = parse_bugzilla_time( bdata.get('last_change_time')) automated_voting_trigger = 0 for flag in bdata.get('flags', []): if ((flag['status'] == '?' and flag['name'] == 'remo-approval')): automated_voting_trigger += 1 if ((flag['status'] == '?' and flag['name'] == 'needinfo' and 'requestee' in flag and flag['requestee']['name'] == ( settings.REPS_COUNCIL_ALIAS))): automated_voting_trigger += 1 if ((automated_voting_trigger == 2 and waffle.switch_is_active('automated_polls'))): bug.council_vote_requested = True comments = bdata.get('comments', []) if comments and comments[0].get('text', ''): bug.first_comment = comments[0]['text'] bug.save() offset += LIMIT url = urlparams(url, offset=offset) set_last_updated_date(now)