def create(users, fspath, wspath, htpasswd, **args): verbose = args.get("verbose", False) term.banner("CREATE INDEXES") progress = term.Progress(0, title="Searching:", bar=(verbose is False)) galleries = search_galleries(fspath, load_access=True, load_albums=True, progress=progress.progress) progress.finish() # create default index file create_write(fspath, galleries, path=wspath) if users: users = collect_users(galleries) for user in users: user_path = os.path.join(fspath, "~{0}".format(safe(user.name))) if not os.path.exists(user_path): os.mkdir(user_path) if os.path.isdir(user_path): user_galleries = collect_galleries_for_user(galleries, user) # create user index file create_write(user_path, user_galleries, path=wspath, title=user.name) # create htaccess file for user directory access = Access(authname=TITLE, authuserfile=htpasswd) access.users.extend([user]) access.write(user_path) else: raise IOError("CREATE INDEXES: could not write index for user '{0}'".format(str(user))) term.banner("DONE", type="INFO")
def install(wspath, fspath, htpasswd, **args): """ Install indexes: Create htaccess file with rewrite rules to serve user-specific indexes. Indexes must be created by executing 'index create' command. NOTE: htaccess file will be overwritten! """ term.banner("INSTALL INDEXES") galleries = search_galleries(fspath, load_access=True) users = collect_users(galleries) access = Access(authname=TITLE, authuserfile=htpasswd) access.users.extend(users) access.conf.append(("RewriteEngine", "On")) access.conf.append(("RewriteBase", wspath)) access.conf.append(("RewriteCond", "%{REMOTE_user} ^.+$")) access.conf.append(("RewriteRule", "^$ {0}~%{{REMOTE_user}} [R,L]".format(wspath))) access.write(fspath) term.banner("DONE", type="INFO")
class Gallery: def __init__(self, path, name, label=None, year=None, month=None, day=None, countries=None, albums=None, access=None ): self.path = path self.name = name self.label = label self.year = year self.month = month self.day = day self.countries = countries self.albums = albums self.access = access def __str__(self): return " ".join([ "".join([self.year, self.month, self.day]), "".join(self.countries), self.label ]).strip() def date_str(self): if self.day == "00": return "{0}/{1}".format(self.month, self.year) else: return "{0}.{1}.{2}".format(self.day, self.month, self.year) def countries(self, sep=''): return sep.join(self.countries) def countries_str(self, format='long', sep=' '): c = [] if format == 'long': for alpha2 in self.countries: country = pycountry.countries.get(alpha2=alpha2) if country: c.append(_(country.name)) elif format == 'short': c = self.countries return sep.join(c) def albums_str(self, sep=', '): return sep.join(["'{0!s}'".format(a) for a in self.albums]) def get_album(self, label): for album in self.albums: if label == album.label: return album return None def access_init(self, authname, authuserfile): self.access = Access(authname=authname, authuserfile=authuserfile) def access_write(self): if self.access: self.access.write(self.path) def factory(path, name, load_access=False, load_albums=False): path = os.path.join(path, name) if not os.path.isdir(path): return None g = {} g['albums'] = None g['access'] = None # parse name m = GALLERY_NAME_PATTERN.match(name) if not m: return None g['year'] = m.group(1) g['month'] = m.group(2) g['day'] = m.group(3) # parse name: split country codes into iso codes of two characters t = m.group(4) g['countries'] = [t[x:x+2] for x in range(0, len(t), 2)] # parse name: gallery label t = m.group(6) or "" g['label'] = " ".join(word.title() for word in t.split("_")) # search albums g['albums'] = search_albums(path, load=load_albums) # read access if load_access: try: g['access'] = read_access(path) except ValueError as x: logging.warning("%s", x) return Gallery(path, name, **g) factory = staticmethod(factory)
def access_init(self, authname, authuserfile): self.access = Access(authname=authname, authuserfile=authuserfile)