def setUp(self): self.club = Club(name="NTNUI") self.club.save() self.group = Group(name="NTNUI Fotball", club=self.club) self.group.save() self.user = User.objects.create_superuser("testuser", email="*****@*****.**", password="******") self.user.save() self.interest = Interest(session_id="c00k13M0n5t3r", group=self.group) self.interest.save() self.factory = APIRequestFactory() self.interests = Interest.objects.all()
def test_query_param_club_no_groups(self): Club(name="NothingToSee") request = self.factory.get("/groups/", {"club": "NothingToSee"}) response = get_response(request) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data.get("results")), 0)
def init(request): club_category = ClubCategory(name="Techno") club_category.save() club = Club(name="HIFI", category=club_category) club.save() class_group = ClassGroup(label="L3") class_group.save() user = User( first_name="Rémi", last_name="Jarasson", username="******", birthdate=datetime(1991, 01, 17), email="*****@*****.**", ) user.save() app = Application(name="Pouet", slug="pouet", api_key="1234", club=club) app.save() session = Session( token="2345", time_start=datetime.now(), time_end=datetime(2011, 12, 25), user=user, application=app ) session.save() p1 = Permission(name="permission.get") p2 = Permission(name="permission.new") p3 = Permission(name="global.permission.delete") p1.save() p2.save() p3.save() app.permissions.add(p1) app.permissions.add(p3) rank = Rank(name="Président") rank.save() rank.permissions.add(p1) rank.permissions.add(p3) rank.save() user.groups.create(rank=rank, club=club) return HttpResponse("Initialization.")
def doImportClubs(request): csvText = request.POST["csvClubData"] lines = csvText.split("\n") clubContext = ClubContextImport() for nextLine in lines: fields = nextLine.split(",") clubName = fields[0] clubNum = fields[1] clubArea = fields[2] clubDiv = fields[3] clubDistrict = fields[4] existingClub = Club.objects.filter(Number=clubNum) if len(existingClub) == 0: newClub = Club(Name=clubName, Number=clubNum, Area=clubArea, Division=clubDiv, District=clubDistrict) newClub.save() clubContext.Added = clubContext.Added + 1 else: clubContext.Existing = clubContext.Existing + 1 clubContext.ClubList = Club.objects.all().order_by("Number") return render_to_response("clubs/index.html", {"clubContext": clubContext})
def addClub(request): clubContext = ClubContextAdd() try: clubName = request.POST["clubName"] clubNumber = request.POST["clubNumber"] clubArea = request.POST["clubArea"] clubDivision = request.POST["clubDivision"] clubDistrict = request.POST["clubDistrict"] except (): # return render_to_response( "clubs/index.html", {"error_message": "Club create failed."}, context_instance=RequestContext(request) ) else: newClub = Club(Name=clubName, Number=clubNumber, Area=clubArea, Division=clubDivision, District=clubDistrict) newClub.save() # messages.success(request, 'Add') # Always return an HttpResponseRedirect after successfully dealing # with POST data. This prevents data from being posted twice if a # user hits the Back button. return HttpResponseRedirect(reverse("clubs.views.clubsHome"))
def test_rank(self): # create some clubs Club.objects.bulk_create( [ Club( code=f"club-{i}", name=f"Test Club #{i}", description="This is a sentence. " * i, active=True, subtitle="This is a subtitle.", ) for i in range(1, 101) ] ) # create some tags tag1 = Tag.objects.create(name="Undergraduate") tag2 = Tag.objects.create(name="Graduate") tag3 = Tag.objects.create(name="Professional") # add objects to club for club in Club.objects.all()[:10]: club.tags.add(tag1) club.tags.add(tag2) club.tags.add(tag3) now = timezone.now() # add an event Event.objects.create( code="test-event-1", name="Test Event 1", club=Club.objects.first(), start_time=now, end_time=now + datetime.timedelta(hours=2), description="This is a test event!", ) # run the rank command call_command("rank") for club in Club.objects.all(): self.assertGreater(club.rank, 0)
class TestInterestApi(APITestCase): def setUp(self): self.club = Club(name="NTNUI") self.club.save() self.group = Group(name="NTNUI Fotball", club=self.club) self.group.save() self.user = User.objects.create_superuser("testuser", email="*****@*****.**", password="******") self.user.save() self.interest = Interest(session_id="c00k13M0n5t3r", group=self.group) self.interest.save() self.factory = APIRequestFactory() self.interests = Interest.objects.all() def test_post_interests(self): request = self.factory.post( "/interest/", { "group": self.group.pk, "session_id": "c00k13" }, format="json", HTTP_ORIGIN="localhost:8000", ) response = get_response(request, create=True) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data.keys(), {"id", "session_id", "group", "created"}) def test_post_used_session_id(self): request = self.factory.post( "/interest/", { "group": self.group.pk, "session_id": "c00k13M0n5t3r" }, format="json", HTTP_ORIGIN="localhost:8000", ) response = get_response(request, create=True) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) def test_post_empty_session_id(self): request = self.factory.post( "/interest/", {"group": self.group.pk}, format="json", HTTP_ORIGIN="localhost:8000", ) response = get_response(request, create=True) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) def test_post_empty_club(self): request = self.factory.post( "/interest/", {"session_id": "no_group"}, format="json", HTTP_ORIGIN="localhost:8000", ) response = get_response(request, create=True) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) def test_get_interests_no_auth(self): request = self.factory.get("/interest/") response = get_response(request) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_get_interests_auth(self): request = self.factory.get("/interest/") response = get_response(request, user=self.user) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data.keys(), {"count", "next", "previous", "results"}) self.assertEqual(len(response.data.get("results")), len(self.interests)) self.assertEqual( response.data.get("results"), InterestSerializer(self.interests, many=True).data, ) def test_get_interest_detail_no_auth(self): request = self.factory.get("/interest/") response = get_response(request, interest_id=self.interest.pk) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_get_interest_detail_auth(self): request = self.factory.get("/interest/") response = get_response(request, user=self.user, interest_id=self.interest.pk) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, InterestSerializer(self.interest).data) def test_get_non_existing_interest(self): request = self.factory.get("/interest/") response = get_response(request, user=self.user, interest_id="99") self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def process_url(self, url): self.stdout.write("Processing Page {}".format(self.count)) self.count += 1 resp = self.session.get(url) resp.raise_for_status() soup = BeautifulSoup(resp.content, "html.parser") grps = soup.select(".grpl .grpl-grp") for grp in grps: name = grp.select_one("h3 a").text.strip() image_url = urljoin(url, grp.select_one("img")["src"]).strip() if image_url.endswith("/group_img.png"): image_url = None group_tag = grp.select_one(".grpl-type") if group_tag is not None: group_type = group_tag.text.strip() else: group_type = None description = grp.select_one(".grpl-purpose").text.replace( "\r\n", "\n").strip() if description == "This group has not written a purpose": description = "" else: description = clean(description) contact_tag = grp.select_one(".grpl-contact") if contact_tag is not None: contact_email = contact_tag.text.strip() else: contact_email = None if group_type is not None and not self.dry_run and not self.skip_tags: tag = Tag.objects.get_or_create(name=group_type)[0] else: tag = None clubs = Club.objects.filter(name__iexact=name) if clubs.exists(): if clubs.count() > 1: raise CommandError( "Club with name '{}' exists twice!".format(name)) club = clubs.first() flag = False else: code = slugify(name) if not self.dry_run: club, flag = Club.objects.get_or_create(code=code) elif Club.objects.filter(code=code).exists(): club = Club.objects.get(code=code) flag = False else: club = Club(code=code) flag = True # only overwrite blank fields if not club.name: club.name = name if not club.description: club.description = description use_image = False if image_url: if not self.dry_run: if club.image: resp = requests.head(image_url, allow_redirects=True) use_image = not resp.ok else: use_image = True if use_image: resp = requests.get(image_url, allow_redirects=True) resp.raise_for_status() club.image.save(os.path.basename(image_url), ContentFile(resp.content)) if not club.email: club.email = contact_email # mark newly created clubs as inactive (has no owner) if flag: club.active = False if not self.dry_run: club.save() if tag is not None and not club.tags.count(): club.tags.set([tag]) self.club_count += 1 self.stdout.write("{} '{}' (image: {})".format( "Created" if flag else "Updated", name, use_image)) next_tag = soup.find(text="Next >") if next_tag is not None: next_link = next_tag.find_parent("a")["href"] next_url = url.split("?", 1)[0] + next_link self.process_url(next_url)
def process_url(self, url): self.stdout.write(f"Processing Page {self.count}") self.count += 1 resp = self.session.get(url) resp.raise_for_status() soup = BeautifulSoup(resp.content, "html.parser") grps = soup.select(".grpl .grpl-grp") for grp in grps: # parse name name = grp.select_one("h3 a").text.strip() # parse image url image_url = urljoin(url, grp.select_one("img")["src"]).strip() if image_url.endswith("/group_img.png"): image_url = None # parse tag group_tag = grp.select_one(".grpl-type") if group_tag is not None: group_type = group_tag.text.strip() else: group_type = None # parse description description = grp.select_one(".grpl-purpose").text.replace( "\r\n", "\n").strip() if description == "This group has not written a purpose": description = "" else: description = clean(description) # parse email contact contact_tag = grp.select_one(".grpl-contact") if contact_tag is not None: contact_email = contact_tag.text.strip() else: contact_email = None # create or update tag if group_type is not None and not self.dry_run and not self.skip_tags: tag = Tag.objects.get_or_create(name=group_type)[0] else: tag = None # don't include parentheses content in code slug_name = re.sub(r"\(.+?\)$", "", name).strip() # create or update club code = slugify(slug_name) club = fuzzy_lookup_club(name) if club is not None: code = club.code flag = False else: club = Club(code=code) flag = True if not flag and self.create_only: self.ignore_count += 1 self.stdout.write(f"Ignoring {name}, club already exists") continue # only overwrite blank fields if not club.name: club.name = name if not club.description: club.description = description # only update image if existing image is nonexistent/broken link # if image is local and set, assume that it exists use_image = False if image_url: if not self.dry_run: if club.image: if club.image.url.startswith("http"): resp = requests.head(club.image.url, allow_redirects=True) use_image = not resp.ok else: use_image = False else: use_image = True if use_image: resp = requests.get(image_url, allow_redirects=True) resp.raise_for_status() club.image.save(os.path.basename(image_url), ContentFile(resp.content)) else: use_image = not bool(club.image) # update email if there is no email if not club.email: club.email = contact_email # mark newly created clubs as inactive (has no owner) if flag: club.active = False if not self.dry_run: with transaction.atomic(): club.save() if tag is not None and not club.tags.count(): club.tags.set([tag]) self.club_count += 1 action_verb = "Created" if flag else "Updated" out_string = f"{action_verb} '{name}' (image: {use_image})" if flag: self.stdout.write(self.style.SUCCESS(out_string)) self.create_count += 1 else: self.stdout.write(out_string) self.update_count += 1 next_tag = soup.find(text="Next >") if next_tag is not None: next_link = next_tag.find_parent("a")["href"] next_url = url.split("?", 1)[0] + next_link self.process_url(next_url)