Пример #1
0
    def get(self):
        user = users.get_current_user()
        prefs = InternalUser.from_user(user)
        invalid_url = decode(self.request.get('u'))

        recent = mc.get_recent_pivots()[:10]
        heavy = mc.get_heavy_pivots()[:10]
        topdomains = mc.get_topdomains()[:20]
        recent_projects = mc.get_recent_projects()

        webapp.template.register_template_library('common.templateaddons')
        self.response.out.write(template.render(tdir + "index.html", \
                {"prefs": prefs, 'invalid_url': invalid_url, \
                'recent': recent, 'heavy': heavy, \
                "topdomains": topdomains[:20], \
                'pivot_count': mc.get_pivot_count(), \
                'recent_projects': recent_projects,
                'recent_projects_5': recent_projects[:5]}))
Пример #2
0
    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)