def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["current_kind"] = self.request.GET.get("kind") if not self.request.GET.get("q"): context["current_kind"] = context["current_kind"] or KIND.default() ngo = kwargs.get("ngo", context.get("ngo")) if not ngo: return context needs = ngo.needs.filter(resolved_on=None) if "need" in kwargs: needs = needs.exclude(pk=kwargs["need"].pk) context["current_need"] = kwargs["need"] for kind in KIND.to_list(): kind_needs = needs.filter(kind=kind) needs_paginator = paginator.Paginator(kind_needs, NEEDS_PER_PAGE) page = self.request.GET.get(f"{kind}_page") # Catch invalid page numbers try: needs_page_obj = needs_paginator.page(page) except (paginator.PageNotAnInteger, paginator.EmptyPage): needs_page_obj = needs_paginator.page(1) context[f"{kind}_page_obj"] = needs_page_obj return context
def get_needs(self): filters = { "resolved_on": None, } kind = self.request.GET.get("kind") if not self.request.GET.get("q"): kind = kind or KIND.default() if kind: filters["kind"] = kind return NGONeed.objects.filter(**filters).order_by("created")
def get_needs(self): if hasattr(self, "needs"): return self.needs filters = { "resolved_on": None, } kind = self.request.GET.get("kind") if not self.request.GET.get("q"): kind = kind or KIND.default() if kind: filters["kind"] = kind self.needs = (NGONeed.objects.filter(**filters).order_by("ngo__name"). select_related("ngo").prefetch_related("resource_tags")) return self.needs
def get_needs(self): if hasattr(self, "needs"): return self.needs filters = { "resolved_on": None, } kind = self.request.GET.get("kind") if not self.request.GET.get("q"): kind = kind or KIND.default() if kind: filters["kind"] = kind self.needs = (NGONeed.objects.filter(**filters).order_by("ngo__name"). select_related("ngo").prefetch_related("resource_tags")) if kind and kind == KIND.MONEY: self.needs = self.needs.exclude(ngo__name=settings.RED_CROSS_NAME, kind=KIND.MONEY) return self.needs
def handle(self, *args, **kwargs): if not User.objects.filter(username="******").exists(): User.objects.create_user("admin", "*****@*****.**", "admin", is_staff=True, is_superuser=True) if not User.objects.filter(username="******").exists(): User.objects.create_user("user", "*****@*****.**", "user", is_staff=True) if not User.objects.filter(username="******").exists(): User.objects.create_user("dsu", "*****@*****.**", "dsu", is_staff=True) if not User.objects.filter(username="******").exists(): User.objects.create_user("ffc", "*****@*****.**", "ffc", is_staff=True) admin_user = User.objects.get(username="******") ngo_user = User.objects.get(username="******") dsu_user = User.objects.get(username="******") ffc_user = User.objects.get(username="******") admin_group, _ = Group.objects.get_or_create(name=ADMIN_GROUP_NAME) ngo_group, _ = Group.objects.get_or_create(name=NGO_GROUP_NAME) dsu_group, _ = Group.objects.get_or_create(name=DSU_GROUP_NAME) ffc_group, _ = Group.objects.get_or_create(name=FFC_GROUP_NAME) # models.NamedCredentials: ['add', 'change', 'delete', 'view'], GROUPS_PERMISSIONS = { NGO_GROUP_NAME: { NGO: ["change", "view"], NGOHelper: ["view"], NGONeed: ["add", "change", "view"], NGOReportItem: ["add", "change", "delete", "view"], NGOAccount: ["add", "change", "delete", "view"], }, DSU_GROUP_NAME: { PendingRegisterNGORequest: ["view", "change"], RegisterNGORequest: ["view"], RegisterNGORequestVote: ["view", "change"], }, FFC_GROUP_NAME: { PendingRegisterNGORequest: ["view", "change"], RegisterNGORequest: ["view"], RegisterNGORequestVote: ["view", "change"], }, } for group_name in GROUPS_PERMISSIONS: # Get or create group group, created = Group.objects.get_or_create(name=group_name) # Loop models in group for model_cls in GROUPS_PERMISSIONS[group_name]: # Loop permissions in group/model for perm_index, perm_name in enumerate(GROUPS_PERMISSIONS[group_name][model_cls]): # Generate permission name as Django would generate it codename = perm_name + "_" + model_cls._meta.model_name try: # Find permission object and add to group perm = Permission.objects.get(codename=codename) group.permissions.add(perm) self.stdout.write("Adding " + codename + " to group " + group.__str__()) except Permission.DoesNotExist: self.stdout.write(codename + " not found") admin_user.groups.add(admin_group) admin_user.save() ngo_user.groups.add(ngo_group) ngo_user.save() dsu_user.groups.add(dsu_group) dsu_user.save() ffc_user.groups.add(ffc_group) ffc_user.save() tags = [] for resource in RESOURCE_TAGS: tag, _ = ResourceTag.objects.get_or_create(name=resource) tags.append(tag) NGO.objects.filter( pk__in=NGO.objects.exclude(name__in=["Code4Romania", "Crucea Roșie"]) .order_by("created") .values_list("pk")[100:] ).delete() NGONeed.objects.filter(kind="money").exclude(ngo__name__in=["Code4Romania", "Crucea Roșie"]).delete() for details in NGOS: ngo, _ = NGO.objects.get_or_create(**details) owner = random.choice([ngo_user, admin_user, None]) if owner: ngo.users.add(owner) ngo.save() for _ in range(20): need = NGONeed.objects.create( **{ "ngo": ngo, "kind": random.choice(KIND.to_list()[:-1]), "urgency": random.choice(URGENCY.to_list()), "description": fake.text(), "title": fake.text(), "resolved_on": random.choice([None, timezone.now()]), "city": random.choice(["Arad", "Timisoara", "Oradea", "Cluj", "Bucuresti"]), "county": random.choice(["ARAD", "TIMIS", "BIHOR", "CLUJ", "SECTOR 1", "SECTOR 2"]), } ) for _ in range(len(RESOURCE_TAGS)): need.resource_tags.add(random.choice(tags)) NGONeed.objects.filter(pk__in=ngo.needs.order_by("created").values_list("pk")[20:]).delete() for ngo in NGO.objects.all(): for _ in range(random.choice([3, 4, 5, 6, 10])): PaymentOrder.objects.create( ngo=ngo, first_name=fake.name().split(" ")[0], last_name=fake.name().split(" ")[-1], phone=fake.phone_number(), email=fake.email(), address=fake.address(), details="ddd", amount=random.choice([100, 200, 300, 400, 500, 150]), date=fake.date_between(start_date="-30y", end_date="today"), success=True, ) PaymentOrder.objects.filter( pk__in=PaymentOrder.objects.filter(ngo=ngo).order_by("created").values_list("pk")[10:] ).delete() for _ in range(random.choice([3, 4, 5, 6, 10])): NGOReportItem.objects.create( ngo=ngo, date=fake.date_between(start_date="-30y", end_date="today"), title=f"Achizitionat {random.choice(RESOURCE_TAGS)}", amount=random.choice([100, 200, 300, 400, 500, 150]), ) NGOReportItem.objects.filter( pk__in=NGOReportItem.objects.filter(ngo=ngo).order_by("created").values_list("pk")[10:] ).delete()