def generate_dues_attachment(self, response=None, file_obj=False): from users.tables import UserTable time_name = datetime.datetime.now().strftime("%Y%m%d") filename = f"{self}_{time_name}_dues.csv" if response is not None: dues_file = response response["Content-Disposition"] = f'attachment; filename="{filename}"' out = None else: dues_file = io.StringIO() dues_mail = MIMEBase("application", "csv") dues_mail.add_header("Content-Disposition", "attachment", filename=filename) members = annotate_role_status(self.active_actives(), combine=True) table = UserTable(data=members) writer = csv.writer(dues_file) writer.writerows(table.as_values()) if response is None and not file_obj: dues_mail.set_payload(dues_file.getvalue()) out = dues_mail elif file_obj: filepath = Path(r"exports/" + filename) with open(filepath, mode="w", newline="") as f: print(dues_file.getvalue(), file=f) out = filepath return out
def get_current_officers(self, combine=True): officers = self.members.filter( roles__start__lte=TODAY_END, roles__end__gte=TODAY_END ) previous = False date = TODAY_END if officers.count() < 2: # If there are not enough previous officers # get officers from last 8 months previous_officers = self.members.filter( roles__end__gte=TODAY_END - timedelta(30 * 8) ) officers = previous_officers | officers previous = True date = TODAY_END - timedelta(30 * 8) return annotate_role_status(officers, combine=combine, date=date), previous
def get_queryset(self): queryset = User.objects.none() q = self.request.GET.get("q", "") zip = self.request.GET.get("zip", "") if q: queryset = watson.filter(User, q) if zip: distance = self.request.GET.get("dist", "1") addressess = isinradius(zip, distance) user_pks = [ user.pk for address in addressess for user in address.user_set.all() ] if not queryset: queryset = User.objects queryset = queryset.filter(pk__in=user_pks) queryset = annotate_role_status(queryset) return queryset
def export_chapter_actives(self, request, queryset): time_name = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") zip_filename = f"ThetaTauActiveExport_{time_name}.zip" zip_io = BytesIO() qs = self.model._default_manager.filter( status__status__in=["active", "activepend", "alumnipend", "away"], status__start__lte=TODAY_END, status__end__gte=TODAY_END, ) with zipfile.ZipFile(zip_io, "w") as zf: active_chapters = Chapter.objects.exclude(active=False) total = active_chapters.count() for count, chapter in enumerate(active_chapters): print(f"Export {chapter} {count+1}/{total}") members = annotate_role_status( qs.filter( chapter=chapter, ), combine=True, ) table = UserTable( data=members, chapter=True, ) writer_file = StringIO() writer = csv.writer(writer_file) writer.writerows(table.as_values()) zf.writestr( f"{chapter}_{chapter.school}_activeexport_{time_name}.csv", writer_file.getvalue(), ) response = HttpResponse( zip_io.getvalue(), content_type="application/x-zip-compressed" ) response["Cache-Control"] = "no-cache" response["Content-Disposition"] = f"attachment; filename={zip_filename}" return response
def get_initial(self): # return whatever you'd normally use as the initial data for your formset. users_with_service = self.request.user.current_chapter.service_hours() all_members = self.request.user.current_chapter.current_members() cancel = self.request.GET.get("cancel", False) request_get = self.request.GET.copy() if cancel: request_get = QueryDict() self.filter = UserListFilter( request_get, queryset=annotate_role_status(all_members, combine=False), request=self.request, ) all_members = combine_annotations(self.filter.qs) initials = [] for user in all_members: init_dict = {"user": user.name} if user in users_with_service: user_service_hours = user.service_hours.filter( year__gte=BIENNIUM_YEARS[0] ).values("year", "term", "service_hours") if user_service_hours: for i in range(4): semester = "sp" if i % 2 else "fa" year = BIENNIUM_YEARS[i] try: service = user_service_hours.get(term=semester, year=year) except UserSemesterServiceHours.DoesNotExist: continue else: init_dict[f"service{i + 1}"] = service["service_hours"] for key in ["service1", "service2", "service3", "service4"]: if key not in init_dict: init_dict[key] = 0.0 initials.append(init_dict) return initials
def get_queryset(self): qs = self.model._default_manager.all() ordering = self.get_ordering() if ordering: if isinstance(ordering, str): ordering = (ordering,) qs = qs.order_by(*ordering) members = annotate_role_status( qs.filter( chapter=self.request.user.current_chapter, status__status__in=["active", "activepend", "alumnipend"], status__start__lte=TODAY_END, status__end__gte=TODAY_END, ), combine=False, ) pledges = annotate_role_status( qs.filter( chapter=self.request.user.current_chapter, status__status="pnm", status__start__lte=TODAY_END, status__end__gte=TODAY_END, ), combine=False, ) alumni = User.objects.none() if self.request.user.chapter_officer(): alumni = annotate_role_status( qs.filter( chapter=self.request.user.current_chapter, status__status__in=["alumni"], status__start__lte=TODAY_END, status__end__gte=TODAY_END, ), combine=False, ) qs = members | pledges | alumni cancel = self.request.GET.get("cancel", False) request_get = self.request.GET.copy() if cancel: request_get = QueryDict() if not request_get: # Create a mutable QueryDict object, default is immutable request_get = QueryDict(mutable=True) request_get.setlist( "current_status", [ "active", "pnm", "activepend", "alumnipend", ], ) if not cancel: current_status = request_get.get("current_status", "") if current_status == "": request_get.setlist( "current_status", [ "active", "pnm", "activepend", "alumnipend", ], ) self.filter = self.filter_class(request_get, queryset=qs, request=self.request) self.filter.form.helper = self.formhelper_class() qs = combine_annotations(self.filter.qs) return qs