def send_esi_mail(): """ Sends mails to characters. mailRecipients => JSON String recipients=[{"recipient_id": 0, "recipient_type": "character|alliance"}] mailBody => String mailSubject => String """ token: SSOToken = current_user.get_a_sso_token_with_scopes( esi_scopes.mail_scopes) if token is None: return flask.abort(412, 'Not Authenticated for esi-mail.send_mail.v1') body = request.form.get('mailBody') subject = request.form.get('mailSubject') recipients = json.loads(request.form.get('mailRecipients')) target_chars = [] for rec in recipients: if rec['recipient_type'] == 'character': target_chars.append(rec['recipient_id']) target_accs = db.session.query(Account).filter( or_(Account.current_char == charid for charid in target_chars)).all() resp = send_mail(token, recipients, body, subject) if resp.status == 201: history_entry: AccountNote = AccountNote( accountID=current_user.id, byAccountID=current_user.id, type=account_notes.TYPE_SENT_ACCOUNT_MAIL) history_entry.jsonPayload = { 'sender_character_id': current_user.get_eve_id(), 'recipients': recipients, 'body': body, 'subject': subject } db.session.add(history_entry) for acc in target_accs: acc.had_welcome_mail = True history_entry = AccountNote( accountID=acc.id, byAccountID=current_user.id, type=account_notes.TYPE_GOT_ACCOUNT_MAIL) history_entry.jsonPayload = { 'sender_character_id': current_user.get_eve_id(), 'target_character_id': acc.current_char, 'mail_body': body, 'subject': subject } db.session.add(history_entry) db.session.commit() else: esi_resp: ESIResponse = make_error_response(resp) if esi_resp.is_monolith_error(): return make_response(esi_resp.get_monolith_error()['error_label'], resp.status) return make_response( str(resp.data) if resp.data is not None else '', resp.status)
def create_wing(self) -> WingCreated: response = self.__client.request( self.__api.op['post_fleets_fleet_id_wings']( fleet_id=self.__fleetID)) if response.status == 201: return WingCreated(get_expire_time(response), response.status, None, response.data['wing_id']) return make_error_response(response)
def open_information(target_id: int) -> ESIResponse: api_v1: App = get_api() client: EsiClient = get_esi_client() resp = client.request(api_v1.op['post_ui_openwindow_information'](target_id=target_id)) if resp.status == 204: return ESIResponse(get_expire_time(resp), resp.status, None) return make_error_response(resp)
def get_member(self) -> ESIResponse: response = self.__client.request( self.__api.op['get_fleets_fleet_id_members']( fleet_id=self.__fleetID)) logger.debug("Got ESI Response with status[%d]", response.status) if response.status == 200: return EveFleetMembers(get_expire_time(response), response.status, None, response.data) return make_error_response(response)
def set_wing_name(self, wing_id: int, name: str) -> ESIResponse: # type: (int, str) -> ESIResponse data = {'name': name} response = self.__client.request( self.__api.op['put_fleets_fleet_id_wings_wing_id']( fleet_id=self.__fleetID, wing_id=wing_id, naming=data)) if response.status == 204: return ESIResponse(get_expire_time(response), response.status, None) return make_error_response(response)
def get_fleet_info(self, char_id: int) -> Union[FleetInfo, ESIResponse]: authed_esi_client = get_esi_client() try: resp = authed_esi_client.request(self._api().op['get_characters_character_id_fleet'](character_id=char_id)) if resp.status == 200: return FleetInfo(get_expire_time(resp), resp.status, None, resp.data) else: logger.error(f'Got error requesting fleet info for character={char_id}') return make_error_response(resp) except ReadTimeoutError as e: logger.error(f'ESI Read Timeout on get_characters_character_id {e}') raise e
def set_squad_name(self, squad_id: int, name: str) -> ESIResponse: response = self.__client.request( self.__api.op['put_fleets_fleet_id_squads_squad_id']( fleet_id=self.__fleetID, squad_id=squad_id, naming={ 'name': name })) if response.status == 204: return ESIResponse(get_expire_time(response), response.status, None) return make_error_response(response)
def get_corporation_info(self, corp_id: int) -> Union[CorporationInfo, ESIResponse]: try: resp = self.esi_client.request(self._api() .op['get_corporations_corporation_id'](corporation_id=corp_id)) if resp.status == 200: return CorporationInfo(get_expire_time(resp), resp.status, None, resp.data) else: logger.error(f'Failed to get corp info for id={corp_id}') return make_error_response(resp) except ReadTimeoutError as e: logger.error(f'ESI Read Timeout on get_corporation_info {e}') raise e
def set_fleet_settings(self, is_free_move: bool, motd: str) -> ESIResponse: settings = FleetSettings(is_free_move, motd) logger.info( f"Changing Fleetsetting to for fleet={self.__fleetID} to {settings.get_esi_data()}" ) request = self.__api.op['put_fleets_fleet_id']( fleet_id=self.__fleetID, new_settings=settings.get_esi_data()) response = self.__client.request(request) if response.status == 204: logger.info("Got 204 back") return ESIResponse(get_expire_time(response), response.status, None) return make_error_response(response)
def open_mail(token: SSOToken, recipients: Sequence[int], body: str, subject: str, to_corp_or_alliance_id: int = None, to_mailing_list_id: int = None) -> ESIResponse: """ { "body": "string", "recipients": [ 0 # min 1 item ], "subject": "string", "to_corp_or_alliance_id": 0, # optional "to_mailing_list_id": 0 # optional # max 1 of the 2 optimal values } """ payload: Dict[str, Any] = {} if to_corp_or_alliance_id is not None and to_mailing_list_id is not None: raise ValueError( "Only to_mailing_list_id or to_corp_or_alliance_id can have a value, not both!" ) payload['body'] = body payload['subject'] = subject if to_mailing_list_id is not None: payload['to_mailing_list_id'] = to_mailing_list_id if to_corp_or_alliance_id is not None: payload['to_corp_or_alliance_id'] = to_corp_or_alliance_id payload['recipients'] = [] for charID in recipients: payload['recipients'].append(charID) if len(payload['recipients']) <= 0: payload['recipients'] = [0] client = get_esi_client(token, False) api: App = get_api() response = client.request( api.op['post_ui_openwindow_newmail'](new_mail=payload)) if response.status == 204: return ESIResponse(get_expire_time(response), response.status, None) return make_error_response(response)
def public_search(self, search: str, type_names: Sequence[str], strict: bool = True)\ -> Union[SearchResponse, ESIResponse]: try: resp = self.esi_client.request(self._api().op['get_search']( categories=type_names, search=search, strict=strict)) if resp.status == 200: return SearchResponse(get_expire_time(resp), resp.status, None, resp.data) else: return make_error_response(resp) except ReadTimeoutError as e: logger.error(f'ESI Read Timeout on public_search {e}') raise e
def get_alliance_info(self, all_id: int) -> Union[AllianceInfo, ESIResponse]: try: resp = self.esi_client.request( self._api().op['get_alliances_alliance_id']( alliance_id=all_id)) if resp.status == 200: return AllianceInfo(get_expire_time(resp), resp.status, None, resp.data) else: logger.error(f'Failed to get alliance info for id={all_id}') return make_error_response(resp) except ReadTimeoutError as e: logger.error(f'ESI Read Timeout on get_alliance_info {e}') raise e
def invite(self, character_id: int, role: str, squad_id: int, wing_id: int) -> ESIResponse: """ 'fleet_commander', 'wing_commander', 'squad_commander', 'squad_member' """ invite: Dict[str, Any] = {'character_id': character_id, 'role': role} if squad_id is not None: invite['squad_id'] = squad_id if wing_id is not None: invite['wing_id'] = wing_id response = self.__client.request( self.__api.op['post_fleets_fleet_id_members']( fleet_id=self.__fleetID, invitation=invite)) if response.status == 204: return ESIResponse(get_expire_time(response), response.status, None) return make_error_response(response)
def open_information(target_id: int) -> ESIResponse: """ Tries to open an ingame information window for the given id. :param target_id: id to open ingame information window for :return: ESIResponse :raises APIException if there is something wrong with tokens """ token: Optional[SSOToken] = current_user.get_a_sso_token_with_scopes(esi_scopes.open_ui_window) if token is None: return ESIResponse(datetime.datetime.utcnow(), 403, f"No token with the required scopes:" f" {''.join(esi_scopes.open_ui_window)} exists.") api_v1: App = get_api() client: EsiClient = get_esi_client(token) resp = client.request(api_v1.op['post_ui_openwindow_information'](target_id=target_id)) if resp.status == 204: return ESIResponse(get_expire_time(resp), resp.status, None) return make_error_response(resp)
def get_wings(self) -> EveFleetWings: response = self.__client.request( self.__api.op['get_fleets_fleet_id_wings']( fleet_id=self.__fleetID)) if response.status == 200: wings = [] for wing in response.data: wing_id = wing['id'] wing_name = wing['name'] squads = [] for squad in wing['squads']: squad = EveFleetSquad(squad['id'], squad['name']) squads.append(squad) fleet_wing = EveFleetWing(wing_id, wing_name, squads) wings.append(fleet_wing) return EveFleetWings(get_expire_time(response), response.status, None, wings) return make_error_response(response)
def send_esi_mail(): """ mailRecipients => JSON String recipients=[{"recipient_id": 0, "recipient_type": "character|alliance"}] mailBody => String mailSubject => String """ needs_refresh = True if current_user.ssoToken is not None: for scope in current_user.ssoToken.scopes: if scope.scopeName == 'esi-mail.send_mail.v1': needs_refresh = False if needs_refresh: return flask.abort(412, 'Not Authenticated for esi-mail.send_mail.v1') body = request.form.get('mailBody') subject = request.form.get('mailSubject') recipients = json.loads(request.form.get('mailRecipients')) target_chars = [] for rec in recipients: if rec['recipient_type'] == 'character': target_chars.append(rec['recipient_id']) resp = send_mail(recipients, body, subject) if resp.status == 201: target_accs = db.session.query(Account).filter( or_(Account.current_char == charid for charid in target_chars)).all() for acc in target_accs: acc.had_welcome_mail = True history_entry = AccountNote(accountID=acc.id, byAccountID=current_user.id, note="Send mail to main character linked to this account with id=" + str(acc.current_char) + " and name=" + acc.current_char_obj.eve_name) db.session.add(history_entry) db.session.commit() else: esi_resp: ESIResponse = make_error_response(resp) if esi_resp.is_monolith_error(): return make_response(esi_resp.get_monolith_error()['error_label'], resp.status) return make_response(str(resp.data) if resp.data is not None else '', resp.status)
def get_fleet_settings(self) -> EveFleet: # type: () -> EveFleet """ Get fleet information get_fleets_fleet_id_ok { is_free_move (boolean): Is free-move enabled , is_registered (boolean): Does the fleet have an active fleet advertisement , is_voice_enabled (boolean): Is EVE Voice enabled , motd (string): Fleet MOTD in CCP flavoured HTML } """ response = self.__client.request( self.__api.op['get_fleets_fleet_id'](fleet_id=self.__fleetID)) if response.status == 200: return EveFleet(get_expire_time(response), response.status, None, response.data['is_free_move'], response.data['is_registered'], response.data['is_voice_enabled'], response.data['motd']) return make_error_response(response)