diff --git a/.gitignore b/.gitignore index 06b29ab4..c9d8da8f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /allianceauth_afat.egg-info/ /afat/__pycache__/ .idea/ +.github/ diff --git a/afat/__init__.py b/afat/__init__.py index 89aa99c7..772d6bd5 100644 --- a/afat/__init__.py +++ b/afat/__init__.py @@ -2,4 +2,4 @@ default_app_config = "afat.apps.AfatConfig" __title__ = "Fleet Activity Tracking" -__version__ = "0.0.2" +__version__ = "0.3.4" diff --git a/afat/models.py b/afat/models.py index d386650a..d1dbeefd 100644 --- a/afat/models.py +++ b/afat/models.py @@ -65,7 +65,7 @@ def dead(self): return self.exclude(deleted_at=None) -# Fat Link type (StratOp, ADM, HD etc) +# AFatLinkType Model (StratOp, ADM, HD etc) class AFatLinkType(SoftDeletionModel): id = models.AutoField(primary_key=True) name = models.CharField(max_length=254) @@ -75,7 +75,7 @@ def __str__(self): return "{} - {}".format(self.id, self.name) -# FatLink Model +# AFatLink Model class AFatLink(SoftDeletionModel): afattime = models.DateTimeField(default=timezone.now) fleet = models.CharField(max_length=254, null=True) @@ -100,13 +100,13 @@ class Meta: ordering = ("-afattime",) -# Clickable Link Duration Model +# ClickAFatDuration Model class ClickAFatDuration(models.Model): duration = models.PositiveIntegerField() fleet = models.ForeignKey(AFatLink, on_delete=models.CASCADE) -# PAP/FAT Model +# AFat Model class AFat(SoftDeletionModel): character = models.ForeignKey(EveCharacter, on_delete=models.CASCADE) afatlink = models.ForeignKey(AFatLink, on_delete=models.CASCADE) @@ -121,7 +121,7 @@ def __str__(self): return "{} - {}".format(self.afatlink, self.character) -# Log Model for Manual FAT creation +# ManualAFat Model class ManualAFat(models.Model): creator = models.ForeignKey(User, on_delete=models.SET(get_sentinel_user)) afatlink = models.ForeignKey(AFatLink, on_delete=models.CASCADE) @@ -133,7 +133,7 @@ def __str__(self): return "{} - {} ({})".format(self.afatlink, self.character, self.creator) -# Log Model for Deletion of Fats and FatLinks +# AFatDelLog Model class AFatDelLog(models.Model): # 0 for FatLink, 1 for Fat deltype = models.BooleanField(default=0) diff --git a/afat/tasks.py b/afat/tasks.py index cbe1743b..8a40903f 100644 --- a/afat/tasks.py +++ b/afat/tasks.py @@ -35,8 +35,10 @@ def get_or_create_char(name: str = None, id: int = None): result = esi.client.Search.get_search( categories=["character"], search=name, strict=True ).result() + if "character" not in result: return None + id = result["character"][0] qs = EveCharacter.objects.filter(character_id=id) elif id: @@ -53,12 +55,14 @@ def get_or_create_char(name: str = None, id: int = None): # Make corp and alliance info objects for future sane if character.alliance_id is not None: test = EveAllianceInfo.objects.filter(alliance_id=character.alliance_id) + if len(test) == 0: EveAllianceInfo.objects.create_alliance(character.alliance_id) else: test = EveCorporationInfo.objects.filter( corporation_id=character.corporation_id ) + if len(test) == 0: EveCorporationInfo.objects.create_corporation(character.corporation_id) @@ -106,7 +110,7 @@ def process_line(line, type_, hash): shiptype = line[2] if character is not None: - afat = AFat( + AFat( afatlink_id=link.pk, character=character, system=system, @@ -116,7 +120,7 @@ def process_line(line, type_, hash): character = get_or_create_char(name=line.strip(" ")) if character is not None: - afat = AFat(afatlink_id=link.pk, character=character).save() + AFat(afatlink_id=link.pk, character=character).save() @shared_task @@ -135,8 +139,7 @@ def process_character(char, hash): sol_name = solar_system["name"] ship_name = ship["name"] character = get_or_create_char(id=char_id) - link = AFatLink.objects.get(hash=hash) - fat = AFat( + AFat( afatlink_id=link.pk, character=character, system=sol_name, shiptype=ship_name ).save() diff --git a/afat/views.py b/afat/views.py index 6ff4d8b7..d0f745bc 100644 --- a/afat/views.py +++ b/afat/views.py @@ -59,16 +59,18 @@ def afat_view(request): .order_by("afatlink__afattime") .reverse()[:30] ) + char_1 = [char.character.character_name] for f in fat: char_1.append(f) + char_1.append(char.character.character_id) fats.append(char_1) - links = AFatLink.objects.order_by("afattime").reverse()[:10] + fatlinks = AFatLink.objects.order_by("afattime").reverse()[:10] - context = {"fats": fats, "links": links, "msg": msg} + context = {"fats": fats, "links": fatlinks, "msg": msg} logger.info("Module called by %s", request.user) @@ -92,7 +94,6 @@ def stats(request): data[corp.alliance.alliance_name].append( (corp.corporation_id, corp.corporation_name) ) - elif request.user.has_perm("afat.stats_corp_own"): data = [ ( @@ -178,9 +179,8 @@ def stats_char(request, charid, month=None, year=None): colors.append(bg_color_str) data_ship_type = [ - list( - str(key) for key in data_ship_type.keys() - ), # ship type can be None, so we need to convert to string here + # ship type can be None, so we need to convert to string here + list(str(key) for key in data_ship_type.keys()), list(data_ship_type.values()), colors, ] @@ -260,6 +260,7 @@ def stats_corp(request, corpid, month=None, year=None): afatlink__afattime__year=year, character__corporation_id=corpid, ) + characters = EveCharacter.objects.filter(corporation_id=corpid) # Data for Stacked Bar Graph @@ -298,6 +299,7 @@ def stats_corp(request, corpid, month=None, year=None): ) ) stack.append([]) + data_ = stack[2] for char in chars: @@ -429,9 +431,8 @@ def stats_alliance(request, allianceid, month=None, year=None): colors.append(bg_color_str) data_ship_type = [ - list( - str(key) for key in data_ship_type.keys() - ), # ship type can be None, so we need to convert to string here + # ship type can be None, so we need to convert to string here + list(str(key) for key in data_ship_type.keys()), list(data_ship_type.values()), colors, ] @@ -475,6 +476,7 @@ def stats_alliance(request, allianceid, month=None, year=None): ) ) stack.append([]) + data_ = stack[2] for corp in corps: @@ -568,13 +570,13 @@ def links(request): if "msg" in request.session: msg = request.session.pop("msg") - links = ( + fatlinks = ( AFatLink.objects.all() .order_by("-afattime") .annotate(number_of_fats=Count("afat", filter=Q(afat__deleted_at__isnull=True))) ) - context = {"links": links, "msg": msg} + context = {"links": fatlinks, "msg": msg} logger.info("FAT link list called by %s", request.user) @@ -600,9 +602,11 @@ def link_create_click(request): form = AFatClickFatForm(request.POST) if form.is_valid(): - hash = get_random_string(length=30) + fatlinkhash = get_random_string(length=30) + link = AFatLink() link.fleet = form.cleaned_data["name"] + if ( form.cleaned_data["type"] is not None and form.cleaned_data["type"] != -1 @@ -610,24 +614,25 @@ def link_create_click(request): link.link_type = AFatLinkType.objects.get(id=form.cleaned_data["type"]) link.creator = request.user - link.hash = hash + link.hash = fatlinkhash link.save() + dur = ClickAFatDuration() - dur.fleet = AFatLink.objects.get(hash=hash) + dur.fleet = AFatLink.objects.get(hash=fatlinkhash) dur.duration = form.cleaned_data["duration"] dur.save() - request.session["{}-creation-code".format(hash)] = 202 + request.session["{}-creation-code".format(fatlinkhash)] = 202 logger.info( "FAT link %s with name %s and a duration of %s minutes was created by %s", - hash, + fatlinkhash, form.cleaned_data["name"], form.cleaned_data["duration"], request.user, ) - return redirect("afat:link_edit", hash=hash) + return redirect("afat:link_edit", hash=fatlinkhash) else: request.session["msg"] = [ "danger", @@ -655,23 +660,20 @@ def link_create_click(request): def link_create_esi(request, token, hash): # Check if there is a fleet try: - requiredScopes = ["esi-fleets.read_fleet.v1"] - esiToken = Token.get_token(token.character_id, requiredScopes) + required_scopes = ["esi-fleets.read_fleet.v1"] + esi_token = Token.get_token(token.character_id, required_scopes) - f = esi.client.Fleets.get_characters_character_id_fleet( - character_id=token.character_id, token=esiToken.valid_access_token() + fleet_from_esi = esi.client.Fleets.get_characters_character_id_fleet( + character_id=token.character_id, token=esi_token.valid_access_token() ).result() try: - fleet = esi.client.Fleets.get_fleets_fleet_id( - fleet_id=f["fleet_id"], token=esiToken.valid_access_token() + esi_fleet_member = esi.client.Fleets.get_fleets_fleet_id_members( + fleet_id=fleet_from_esi["fleet_id"], + token=esi_token.valid_access_token(), ).result() - m = esi.client.Fleets.get_fleets_fleet_id_members( - fleet_id=f["fleet_id"], token=esiToken.valid_access_token() - ).result() - - process_fats.delay(m, "eve", hash) + process_fats.delay(esi_fleet_member, "eve", hash) request.session["{}-creation-code".format(hash)] = 200 @@ -690,23 +692,26 @@ def link_create_esi(request, token, hash): @login_required() def create_esi_fat(request): - # "error": "The fleet does not exist or you don't have access to it!" form = AFatLinkForm(request.POST) - hash = get_random_string(length=30) + fat_link_hash = get_random_string(length=30) if form.is_valid(): link = AFatLink( - fleet=form.cleaned_data["name"], creator=request.user, hash=hash + fleet=form.cleaned_data["name"], creator=request.user, hash=fat_link_hash ) + if form.cleaned_data["type"] is not None and form.cleaned_data["type"] != -1: link.link_type = AFatLinkType.objects.get(id=form.cleaned_data["type"]) + link.save() - return redirect("afat:link_create_esi", hash=hash) + + return redirect("afat:link_create_esi", hash=fat_link_hash) else: request.session["msg"] = [ "danger", ("Something went wrong when attempting to submit your" " ESI FAT Link."), ] + return redirect("afat:afat_view") @@ -739,34 +744,41 @@ def click_link(request, token, hash=None): "contact your FC about having your FAT manually added." ), ] + return redirect("afat:afat_view") character = EveCharacter.objects.get(character_id=token.character_id) try: - requiredScopes = [ + required_scopes = [ "esi-location.read_location.v1", "esi-location.read_ship_type.v1", ] - esiToken = Token.get_token(token.character_id, requiredScopes) + esi_token = Token.get_token(token.character_id, required_scopes) + # character location location = esi.client.Location.get_characters_character_id_location( - character_id=token.character_id, token=esiToken.valid_access_token() + character_id=token.character_id, token=esi_token.valid_access_token() ).result() + # current ship ship = esi.client.Location.get_characters_character_id_ship( - character_id=token.character_id, token=esiToken.valid_access_token() + character_id=token.character_id, token=esi_token.valid_access_token() ).result() - location = esi.client.Universe.get_universe_systems_system_id( + # system information + system = esi.client.Universe.get_universe_systems_system_id( system_id=location["solar_system_id"] ).result()["name"] - ship = provider.get_itemtype(ship["ship_type_id"]).name + ship_name = provider.get_itemtype(ship["ship_type_id"]).name try: fat = AFat( - afatlink=fleet, character=character, system=location, shiptype=ship + afatlink=fleet, + character=character, + system=system, + shiptype=ship_name, ) fat.save() @@ -864,15 +876,17 @@ def edit_link(request, hash=None): character = get_or_create_char(name=character_name) if character is not None: - fat = AFat( + AFat( afatlink_id=link.pk, character=character, system=system, shiptype=shiptype, ).save() + ManualAFat( afatlink_id=link.pk, creator=creator, character=character ).save() + request.session["{}-task-code".format(hash)] = 3 else: request.session["{}-task-code".format(hash)] = 4 @@ -922,6 +936,7 @@ def edit_link(request, hash=None): def del_link(request, hash=None): if hash is None: request.session["msg"] = ["warning", "No FAT Link hash provided."] + return redirect("afat:afat_view") try: @@ -934,10 +949,12 @@ def del_link(request, hash=None): return redirect("afat:afat_view") - fats = AFat.objects.filter(afatlink_id=link.pk).delete() + AFat.objects.filter(afatlink_id=link.pk).delete() link.delete() + AFatDelLog(remover=request.user, deltype=0, string=link.__str__()).save() + request.session["msg"] = [ "success", "The FAT Link ({0}) and all associated FATs have been successfully deleted.".format(