def create_membership(self, team, char_name, active=True): #print(team,char_name) try: return TeamMembership.objects.get(team=team, char_name__iexact=char_name), False except TeamMembership.DoesNotExist: if active: profile, created = Profile(name=char_name, user=self.master_user), True # have to use fake name since admin doesn't have names profile.save() membership = TeamMembership(team=team, profile=profile, char_name=char_name) print("Creating player {0}".format(membership.char_name), file=self.stdout) membership.save() return membership, created return None, False
def get_queryset(self): related_members = [ 'map', 'home_player__profile', 'away_player__profile', 'home_player__team', 'away_player__team', 'match__home_team', 'match__away_team' ] used_fields = Game.fields_for_game_detail + [ 'match__home_team__name', 'match__home_team__photo', 'match__away_team__name', 'match__away_team__photo', 'match__creation_date', 'match__publish_date', 'match__home_submitted', 'match__away_submitted', ] queryset = (Game.objects.exclude(winner_team__isnull=True).order_by( 'match__publish_date', 'match', 'order')) if (not self.request.user.is_authenticated() or not self.request.user.get_profile().is_active()): queryset = queryset.filter(match__published=True) if self.kwargs.get('tournament'): queryset = queryset.filter( match__tournament=self.kwargs.get('tournament')) if self.kwargs.get('team') and self.kwargs.get('profile'): self.member = get_object_or_404( TeamMembership.get(**self.kwargs).only('profile__slug', 'team__slug', 'team__tournament', 'char_name')) queryset = queryset.filter( Q(home_player=self.member) | Q(away_player=self.member)) else: if self.request.GET.get('player'): queryset = queryset.filter( Q(home_player__profile__slug=self.request.GET.get( 'player')) | Q(away_player__profile__slug=self.request.GET.get( 'player'))) related_members.append('winner') try: self.player = Profile.objects.get( slug=self.request.GET.get('player')) except Profile.DoesNotExist: pass elif self.request.GET.get('s'): # using the search field queryset = queryset.filter( Q(home_player__char_name__icontains=self.request.GET.get( 's')) | Q(home_player__profile__name__icontains=self.request.GET. get('s')) | Q(away_player__char_name__icontains=self.request.GET.get( 's')) | Q(away_player__profile__name__icontains=self.request.GET. get('s'))) if self.vod_only or self.request.GET.get('vod_only'): queryset = queryset.exclude(vod="").order_by( '-match__publish_date', 'match', 'order') return queryset.select_related(*related_members).only(*used_fields)
def create_membership(self, team, char_name, active=True): #print(team,char_name) try: return TeamMembership.objects.get( team=team, char_name__iexact=char_name), False except TeamMembership.DoesNotExist: if active: profile, created = Profile( name=char_name, user=self.master_user ), True # have to use fake name since admin doesn't have names profile.save() membership = TeamMembership(team=team, profile=profile, char_name=char_name) print("Creating player {0}".format(membership.char_name), file=self.stdout) membership.save() return membership, created return None, False
def get_queryset(self): related_members = ['map', 'home_player__profile', 'away_player__profile', 'home_player__team', 'away_player__team', 'match__home_team', 'match__away_team'] used_fields = Game.fields_for_game_detail + [ 'match__home_team__name', 'match__home_team__photo', 'match__away_team__name', 'match__away_team__photo', 'match__creation_date', 'match__publish_date', 'match__home_submitted', 'match__away_submitted', ] queryset = (Game.objects.exclude(winner_team__isnull=True) .order_by('match__publish_date', 'match', 'order')) if (not self.request.user.is_authenticated() or not self.request.user.get_profile().is_active()): queryset = queryset.filter(match__published=True) if self.kwargs.get('tournament'): queryset = queryset.filter(match__tournament=self.kwargs.get('tournament')) if self.kwargs.get('team') and self.kwargs.get('profile'): self.member = get_object_or_404(TeamMembership.get(**self.kwargs).only('profile__slug', 'team__slug', 'team__tournament', 'char_name')) queryset = queryset.filter(Q(home_player=self.member) | Q(away_player=self.member)) else: if self.request.GET.get('player'): queryset = queryset.filter(Q(home_player__profile__slug=self.request.GET.get('player')) | Q(away_player__profile__slug=self.request.GET.get('player'))) related_members.append('winner') try: self.player = Profile.objects.get(slug=self.request.GET.get('player')) except Profile.DoesNotExist: pass elif self.request.GET.get('s'): # using the search field queryset = queryset.filter(Q(home_player__char_name__icontains=self.request.GET.get('s')) | Q(home_player__profile__name__icontains=self.request.GET.get('s')) | Q(away_player__char_name__icontains=self.request.GET.get('s')) | Q(away_player__profile__name__icontains=self.request.GET.get('s')) ) if self.vod_only or self.request.GET.get('vod_only'): queryset = queryset.exclude(vod="").order_by('-match__publish_date', 'match', 'order') return queryset.select_related(*related_members).only(*used_fields)
def load_player(self, member_url, team, char_name=None): """ Loads player and team membership data, and adds as member to team. Return profile, membership """ try: member_d = self.visit_url(member_url) except IOError: profile_name = " ".join( (word.capitalize() for word in member_url.strip("/").split("/")[-1].split("-"))) print( "Page not found, constructing from {0} name and {1} charname". format(profile_name, char_name)) # create profile and membership profile, created = Profile(name=profile_name, user=self.master_user), True profile.save() membership = TeamMembership(team=team, profile=profile, char_name=char_name, active=False) membership.save() return profile, membership if "Player not found in database" in tostring(member_d): print("Player not found...skipping", file=self.stdout) return info_ps = member_d.cssselect('.content-section-1 p') info_h3s = member_d.cssselect('.content-section-1 h3') profile_name = info_ps[1].text if char_name is None: char_name = info_ps[4].text if "." in char_name: char_name = char_name.split(".", 1)[0] if Profile.objects.filter(name=profile_name).count(): profile, created = Profile.objects.get(name=profile_name), False membership, membership_created = TeamMembership.objects.get_or_create( team=team, profile=profile, defaults={'char_name': char_name}) membership.char_name = char_name else: try: membership = TeamMembership.objects.get(team=team, char_name=char_name) profile, created = membership.profile, False except TeamMembership.DoesNotExist: # create profile and membership profile, created = Profile(name=profile_name, user=self.master_user), True profile.save() membership = TeamMembership(team=team, profile=profile, char_name=char_name) print(created, file=self.stdout) profile.name = profile_name member_photo_url = info_ps[0].cssselect('img')[0].get('src') if member_photo_url != self.unknown_photo: filename = slugify( profile.name) + posixpath.splitext(member_photo_url)[1] profile.photo.save( filename, ContentFile(urllib2.urlopen(member_photo_url).read())) if info_ps[3].text: profile.title = info_ps[3].text if info_ps[-1].text: # deal with blank race if info_h3s[-1].text.strip().lower() == "champion": membership.champion = info_ps[-1].text.strip() else: try: membership.race = info_ps[-1].text[0].upper() membership.clean_fields() except ValidationError: membership.race = None try: membership.questions_answers = tostring( member_d.cssselect('div.content-section-2 ol')[0]) except: pass try: profile.full_clean() except ValidationError as e: print("Profile did not validate! {profile_name} {e}".format( profile_name=profile_name.encode('ascii', 'ignore'), e=e), file=self.stderr) else: profile.save() membership.save() return profile, membership
def load_player(self, member_url, team, char_name=None): """ Loads player and team membership data, and adds as member to team. Return profile, membership """ try: member_d = self.visit_url(member_url) except IOError: profile_name = " ".join((word.capitalize() for word in member_url.strip("/").split("/")[-1].split("-"))) print("Page not found, constructing from {0} name and {1} charname".format(profile_name, char_name)) # create profile and membership profile, created = Profile(name=profile_name, user=self.master_user), True profile.save() membership = TeamMembership(team=team, profile=profile, char_name=char_name, active=False) membership.save() return profile, membership if "Player not found in database" in tostring(member_d): print("Player not found...skipping", file=self.stdout) return info_ps = member_d.cssselect('.content-section-1 p') info_h3s = member_d.cssselect('.content-section-1 h3') profile_name = info_ps[1].text if char_name is None: char_name = info_ps[4].text if "." in char_name: char_name = char_name.split(".", 1)[0] if Profile.objects.filter(name=profile_name).count(): profile, created = Profile.objects.get(name=profile_name), False membership, membership_created = TeamMembership.objects.get_or_create(team=team, profile=profile, defaults={'char_name': char_name}) membership.char_name = char_name else: try: membership = TeamMembership.objects.get(team=team, char_name=char_name) profile, created = membership.profile, False except TeamMembership.DoesNotExist: # create profile and membership profile, created = Profile(name=profile_name, user=self.master_user), True profile.save() membership = TeamMembership(team=team, profile=profile, char_name=char_name) print(created, file=self.stdout) profile.name = profile_name member_photo_url = info_ps[0].cssselect('img')[0].get('src') if member_photo_url != self.unknown_photo: filename = slugify(profile.name) + posixpath.splitext(member_photo_url)[1] profile.photo.save(filename, ContentFile(urllib2.urlopen(member_photo_url).read())) if info_ps[3].text: profile.title = info_ps[3].text if info_ps[-1].text: # deal with blank race if info_h3s[-1].text.strip().lower() == "champion": membership.champion = info_ps[-1].text.strip() else: try: membership.race = info_ps[-1].text[0].upper() membership.clean_fields() except ValidationError: membership.race = None try: membership.questions_answers = tostring(member_d.cssselect('div.content-section-2 ol')[0]) except: pass try: profile.full_clean() except ValidationError as e: print("Profile did not validate! {profile_name} {e}".format(profile_name=profile_name.encode('ascii', 'ignore'), e=e), file=self.stderr) else: profile.save() membership.save() return profile, membership