def image(self, id): label = toolis.Label.get(id=id, fields=("image", ), rules=False) image = label.image if not image: raise appier.NotFoundError( message="File not found for label '%d'" % id) return self.send_file(image.data, content_type=image.mime, etag=image.etag, cache=True)
def ensure_twitter_account(self, create = True, safe = True): api = self.get_twitter_api() user = api.verify_account() email = "*****@*****.**" % user["screen_name"] twitter_username = user["screen_name"] account = self.owner.admin_account.get( twitter_username = twitter_username, rules = False, raise_e = False ) account = account or self.owner.admin_account.from_session() account = account or self.owner.admin_account.get( email = email, rules = False, raise_e = False ) if safe and "tw.oauth_token" in self.session: del self.session["tw.oauth_token"] if safe and "tw.oauth_token_secret" in self.session: del self.session["tw.oauth_token_secret"] if safe and "tw.oauth_temporary" in self.session: del self.session["tw.oauth_temporary"] if not account: if not create: raise appier.NotFoundError( message = "No account found for Twitter account" ) account = self.owner.admin_account( username = twitter_username, email = email, password = api.oauth_token, password_confirm = api.oauth_token, twitter_username = twitter_username, twitter_token = api.oauth_token, type = self.owner.admin_account.USER_TYPE ) account.save() account = account.reload(rules = False) if not account.twitter_username: account.twitter_username = twitter_username account.twitter_token = api.oauth_token account.save() if not account.twitter_token == api.oauth_token: account.twitter_token = api.oauth_token account.save() account.touch_login_s() account._set_account() return account
def _send_avatar(self, image="avatar.png", strict=False, cache=False): admin_part = self.owner.admin_part avatar = self.avatar if hasattr(self, "avatar") else None if not avatar: if strict: raise appier.NotFoundError( message="Avatar not found for user '%s'" % self.username) return self.owner.send_static("images/" + image, static_path=admin_part.static_path) return self.owner.send_file(avatar.data, content_type=avatar.mime, etag=avatar.etag, cache=cache)
def challenge_webroot(self, token): webroot_path = appier.conf("WEBROOT_PATH", "/var/lib/letsencrypt/webroot") token_path = os.path.join(webroot_path, ".well-known", "acme-challenge", token) if not os.path.exists(token_path): raise appier.NotFoundError(message="Token file not found") token_file = open(token_path, "rb") try: token_data = token_file.read() finally: token_file.close() return token_data
def ensure_github_account(self, create = True, safe = True): api = self.get_github_api() user = api.self_user() email = user["email"] github_login = user["login"] account = self.owner.admin_account.get( github_login = github_login, rules = False, raise_e = False ) account = account or self.owner.admin_account.from_session() account = account or self.owner.admin_account.get( email = email, rules = False, raise_e = False ) if safe and "gh.access_token" in self.session: del self.session["gh.access_token"] if not account: if not create: raise appier.NotFoundError( message = "No account found for GitHub account" ) account = self.owner.admin_account( username = email, email = email, password = api.access_token, password_confirm = api.access_token, github_login = github_login, github_token = api.access_token, type = self.owner.admin_account.USER_TYPE ) account.save() account = account.reload(rules = False) if not account.github_login: account.github_login = github_login account.github_token = api.access_token account.save() if not account.github_token == api.access_token: account.github_token = api.access_token account.save() account.touch_login_s() account._set_account() return account
def ensure_google_account(self, create=True, safe=True): api = self.get_google_api() user = api.self_user() email = user["emails"][0]["value"] google_id = user["id"] account = self.owner.admin_account.get(google_id=google_id, rules=False, raise_e=False) account = account or self.owner.admin_account.from_session() account = account or self.owner.admin_account.get( email=email, rules=False, raise_e=False) if safe and "gg.access_token" in self.session: del self.session["gg.access_token"] if not account: if not create: raise appier.NotFoundError( message="No account found for Google account") account = self.owner.admin_account( username=email, email=email, password=api.access_token, password_confirm=api.access_token, google_id=google_id, google_token=api.access_token, type=self.owner.admin_account.USER_TYPE) account.save() account = account.reload(rules=False) if not account.google_id: account.google_id = google_id account.google_token = api.access_token account.save() if not account.google_token == api.access_token: account.google_token = api.access_token account.save() account.touch_login_s() account._set_account() return account
def ensure_facebook_account(self, create=True, safe=True): api = self.get_facebook_api() user = api.self_user(fields=("id", "email")) email = user.get("email", None) facebook_id = user["id"] account = self.owner.admin_account.get(facebook_id=facebook_id, rules=False, raise_e=False) account = account or self.owner.admin_account.from_session() account = account or email and self.owner.admin_account.get( email=email, rules=False, raise_e=False) if safe and "fb.access_token" in self.session: del self.session["fb.access_token"] if not account: if not create: raise appier.NotFoundError( message="No account found for Facebook account") account = self.owner.admin_account( username=email, email=email, password=api.access_token, password_confirm=api.access_token, facebook_id=facebook_id, facebook_token=api.access_token, type=self.owner.admin_account.USER_TYPE) account.save() account = account.reload(rules=False) if not account.facebook_id: account.facebook_id = facebook_id account.facebook_token = api.access_token account.save() if not account.facebook_token == api.access_token: account.facebook_token = api.access_token account.save() account.touch_login_s() account._set_account() return account