Beispiel #1
0
    def post(self, relpath):

        if relpath == 'live':
            # Get first (and only) result.
            live_data = models.LiveData.all().get()
            if live_data is None:
                live_data = models.LiveData()

            live_data.gdl_page_url = self.request.get('gdl_page_url') or None

            #if live_data.gdl_page_url is not None:
            live_data.put()

            return self.redirect('/database/live')

        elif relpath == 'author':
            try:
                given_name = self.request.get('given_name')
                family_name = self.request.get('family_name')
                author = models.Author(
                    key_name=''.join([given_name, family_name]).lower(),
                    given_name=given_name,
                    family_name=family_name,
                    org=self.request.get('org'),
                    unit=self.request.get('unit'),
                    city=self.request.get('city'),
                    state=self.request.get('state'),
                    country=self.request.get('country'),
                    homepage=self.request.get('homepage') or None,
                    google_account=self.request.get('google_account') or None,
                    twitter_account=self.request.get('twitter_account')
                    or None,
                    email=self.request.get('email') or None,
                    lanyrd=self.request.get('lanyrd') == 'on')
                lat = self.request.get('lat')
                lon = self.request.get('lon')
                if lat and lon:
                    author.geo_location = db.GeoPt(float(lat), float(lon))

                author.put()

            except db.Error, e:
                # TODO: Doesn't repopulate lat/lng or return errors for it.
                form = models.AuthorForm(self.request.POST)
                if not form.is_valid():
                    sorted_profiles = models.get_sorted_profiles(
                        update_cache=True)
                    template_data = {
                        'sorted_profiles': sorted_profiles,
                        'profile_amount': len(sorted_profiles),
                        'author_form': form
                    }
                    return self.render(
                        data=template_data,
                        template_path='database/author_new.html',
                        relpath=relpath)
            else:
                self.redirect('/database/author')
    def post(self, relpath):

        if relpath == 'live':
            # Get first (and only) result.
            live_data = models.LiveData.all().get()
            if live_data is None:
                live_data = models.LiveData()

            live_data.gdl_page_url = self.request.get('gdl_page_url') or None

            live_data.put()

            return self.redirect('/database/live')

        elif relpath == 'author':
            try:
                given_name = self.request.get('given_name')
                family_name = self.request.get('family_name')
                author = models.Author(
                    key_name=''.join([given_name, family_name]).lower(),
                    given_name=given_name,
                    family_name=family_name,
                    org=self.request.get('org'),
                    unit=self.request.get('unit'),
                    city=self.request.get('city'),
                    state=self.request.get('state'),
                    country=self.request.get('country'),
                    homepage=self.request.get('homepage') or None,
                    google_account=self.request.get('google_account') or None,
                    twitter_account=self.request.get('twitter_account')
                    or None,
                    email=self.request.get('email') or None,
                    lanyrd=self.request.get('lanyrd') == 'on')
                lat = self.request.get('lat')
                lon = self.request.get('lon')
                if lat and lon:
                    author.geo_location = db.GeoPt(float(lat), float(lon))

                author.put()

            except db.Error:
                pass
            else:
                self.redirect('/database/author')

        elif relpath == 'resource':
            author_key = models.Author.get_by_key_name(
                self.request.get('author'))
            author_key2 = models.Author.get_by_key_name(
                self.request.get('second_author'))

            if author_key.key() == author_key2.key():
                author_key2 = None

            tags = (self.request.get('tags') or '').split(',')
            tags = [x.strip() for x in tags if x.strip()]

            browser_support = [
                x.lower()
                for x in (self.request.get_all('browser_support') or [])
            ]

            pub = datetime.datetime.strptime(
                self.request.get('publication_date'), '%Y-%m-%d')

            update_date = self.request.get('update_date') or None

            tutorial = None
            if self.request.get('post_id'):
                tutorial = models.Resource.get_by_id(
                    int(self.request.get('post_id')))

            # Updating existing resource.
            if tutorial:
                try:
                    #TODO: This is also hacky.
                    tutorial.title = self.request.get('title')
                    tutorial.description = self.request.get('description')
                    tutorial.author = author_key
                    tutorial.second_author = author_key2
                    tutorial.url = self.request.get('url') or None
                    tutorial.browser_support = browser_support
                    tutorial.update_date = datetime.date.today()
                    tutorial.publication_date = datetime.date(
                        pub.year, pub.month, pub.day)
                    tutorial.tags = tags
                    tutorial.draft = self.request.get('draft') == 'on'
                    tutorial.social_url = unicode(
                        self.request.get('social_url') or '')
                except TypeError:
                    pass
            else:
                # Create new resource.
                try:
                    tutorial = models.Resource(
                        title=self.request.get('title'),
                        description=self.request.get('description'),
                        author=author_key,
                        second_author=author_key2,
                        url=self.request.get('url') or None,
                        browser_support=browser_support,
                        update_date=datetime.date.today(),
                        publication_date=datetime.date(pub.year, pub.month,
                                                       pub.day),
                        tags=tags,
                        draft=self.request.get('draft') == 'on',
                        social_url=self.request.get('social_url') or None)
                except TypeError:
                    pass

            tutorial.put()

        # TODO: Don't use flush_all. Use flush_all_async() or only purge tutorials.
        # Once new entry is saved, flush memcache.
        memcache.flush_all()

        return self.redirect('/database/')