コード例 #1
0
ファイル: main.py プロジェクト: metachris/csspivot
    def get(self, domain):
        # feedback form submit
        user = users.get_current_user()
        prefs = InternalUser.from_user(user)

        domain_base, domain_full = get_domains(domain)
        logging.info("base: %s, full: %s" % (domain_base, domain_full))
        if not domain_base or len(domain_base) < 5:
            self.error(404)
            return

        #projects = mc.projects_for_domain(domain_base)
        pivots = mc.pivots_for_domain(domain_base)
        logging.info("--> %s pivots" % len(pivots))

        webapp.template.register_template_library('common.templateaddons')
        self.response.out.write(template.render(tdir + "domain.html", \
                {"prefs": prefs, 'pivots': pivots, 'count': len(pivots),\
                'domain_base': domain_base}))
コード例 #2
0
ファイル: main.py プロジェクト: metachris/csspivot
    def post(self):
        user = users.get_current_user()
        prefs = InternalUser.from_user(user)
        #logging.info("new pivot")

        # project
        title = decode(self.request.get('csspivot_title'))
        url = decode(self.request.get('csspivot_url'))
        # first css pivot
        css = decode(self.request.get('csspivot_css'))
        comment = decode(self.request.get('csspivot_comment'))
        new = decode(self.request.get('csspivot_new'))
        orig = decode(self.request.get('csspivot_orig'))
        parent_project = decode(self.request.get('csspivot_project'))
        #logging.info("new: %s" % new)

        if not url:
            logging.info("- no css or url")
            self.response.out.write("no css or url")
            return

        url = url.strip("/")

        if not css:
            css = ""

        if new and len(new) > 4:
            # Update an existing pivot
            p = Pivot.all().filter("id =", new).get()
            if not p:
                self.error(404)
                return
            if p.userprefs.key() != prefs.key():
                self.error(403)
                return
            p.css = css

        else:
            # Create a new pivot. Reference parent pivot+project if exits
            parent_pivot = None
            if orig:
                # pivot has a parent. find that and reference it
                parent_pivot = Pivot.all().filter("id =", orig).get()
                if parent_pivot:
                    project = parent_pivot.project

            d_base, d_full = get_domains(url)

            # Create domain if needed
            domain_created = False
            domain = Domain.all().filter("url_domain_base =", d_base).get()
            if not domain:
                domain = Domain(url_domain_base=d_base, \
                        url_domain_full=d_full)
                domain.put()
                domain_created = True

            # Create project if needed, and increment counts
            if parent_pivot:
                # project already exists, just increment pivot count
                parent_pivot.project.pivot_count += 1
                parent_pivot.project.put()

                parent_pivot.domain.pivot_count += 1
                parent_pivot.domain.put()

            else:
                # Create a new pivot
                project = None
                if parent_project:
                    # new pivot for a specific project (/a/<project-id>/new)
                    project = Project.all().filter("id =", \
                            parent_project).get()

                    if project:
                        project.pivot_count += 1
                        project.put()

                if not project:
                    # create a new project. first check if domain exists
                    project = Project.all().filter("url =", \
                            url).get()
                    if project:
                        logging.info("project found by url")
                        project.pivot_count += 1
                        project.put()
                    else:
                        logging.info("project not found")
                        project = Project(userprefs=prefs, \
                                id=gen_modelhash(Project), \
                                title=title, \
                                url=url, \
                                rand=random.random())

                        domain.project_count += 1
                        project.domain = domain
                        project.url_domain_base = d_base
                        project.url_domain_full = d_full
                        project.put()

            p = Pivot(userprefs=prefs, project=project, css=css, \
                    id=gen_modelhash(Pivot), comment=comment, \
                    rand=random.random())

            p.domain = domain
            p.url = url
            p.url_domain_base = project.url_domain_base
            p.url_domain_full = project.url_domain_full

            if parent_pivot:
                p.parent_pivot = parent_pivot
                logging.info("parent pivot set")

            if not domain_created:
                # to not save domain twice within very short time when created
                domain.pivot_count += 1
                domain.put()

        if p.css:
            p.styles_count = p.css.count(":")

        p.put()

        if prefs:
            # auto star
            star = Star(userprefs=prefs, pivot=p)
            star.put()

        # Clear Cache
        mc.get_recent_pivots(clear=True)
        mc.get_heavy_pivots(clear=True)
        mc.get_pivot_count(clear=True)
        mc.pivots_for_domain(p.domain, clear=True)
        mc.get_topdomains(clear=True)
        mc.get_recent_projects(clear=True)

        self.redirect("/%s" % p.id)