Esempio n. 1
0
def admin_sections_name(name):
    if not User.is_logged_in():
        return redirect('/admin/login?e=You%20need%20to%20log%20in%20first')
    args = {
        'section': Section.get(name)
    }
    return render('/admin/section_single.html', '{} section'.format(args['section'].title), args)
Esempio n. 2
0
    def post(self):
        item = None
        vals = {}
        try:
            # get all the incoming values
            section = Section.get( self.request.get('section') )
            name = self.request.get('name').strip()
            title = self.request.get('title').strip()
            content = self.request.get('content')
            type = self.request.get('type')
            label_raw = self.request.get('label_raw').strip()
            attribute_raw = util.make_attr_raw_string(
                {
                    'index-entry'   : self.request.get('index_entry'),
                    'has-comments'  : self.request.get('has_comments'),
                    'comments-open' : self.request.get('comments_open'),
                    }
                ).strip()

            # some pre-processing of the input params
            if name == '':
                name = util.urlify(self.request.get('title'))

            if self.request.get('key'):
                item = Page.get( self.request.get('key') )
                item.section = section
                item.name = name
                item.title = title
                item.content = content
                item.type = type
                item.label_raw = label_raw
                item.attribute_raw = attribute_raw
            else:
                item = Page(
                    section = section,
                    name = name,
                    title = title,
                    content = content,
                    type = type,
                    label_raw = label_raw,
                    attribute_raw = attribute_raw,
                    )

            # update and save this page
            item.set_derivatives()
            item.put()
            # once saved, regenerate certain section properties
            section.regenerate()
            # also, check that this section doesn't have duplicate content
            Task( params={ 'section_key': str(section.key()), 'name': item.name }, countdown=30, ).add( queue_name='section-check-duplicate-nodes' )
            self.redirect('.')
        except Exception, err:
            vals['item'] = self.request.POST
            vals['err'] = err
            vals['sections'] = Section.all()
            vals['types'] = models.type_choices
            self.template( 'page-form.html', vals, 'admin' );
Esempio n. 3
0
    def post(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.write('Started section regeneration task:')

        key = self.request.get('key')
        self.write('- key = ' + key)

        # ok, so get the section first
        section = Section.get( self.request.get('key') )
        if section is None:
            self.write('No section found')
            logging.warn( 'No section found for key: ' +  key )
            return

        # keep a count of all these things
        labels = {}
        archives = {}

        # get all the nodes
        # FixMe: why doesn't "nodes = section.nodes()" work ???
        nodes = Node.all().filter('section =', section.key())
        self.write('Nodes:')
        for node in nodes:
            self.write('- node=' + node.kind()) # FixMe: why doesn't "node.name()" work :(
            # count all of the labels
            for label in node.label:
                self.write('  - ' + label)
                if label in labels:
                    labels[label] += 1
                else:
                    labels[label] = 1

            # the archive counts
            for archive in node.archive:
                self.write('  - ' + archive)
                if archive in archives:
                    archives[archive] += 1
                else:
                    archives[archive] = 1

        # after all that, make them into a list (for ease of use in Django Templates)
        archives = [
            { 'archive' : x, 'count' : archives[x] }
            for x in sorted(archives)
            if re.search(r'^\d\d\d\d(-\d\d)?$', x, re.DOTALL | re.VERBOSE) # just get years and months
            ]
        labels = [
            { 'label' : x, 'count' : labels[x] }
            for x in sorted(labels)
            ]

        # now that we have our counts, save it as JSON
        section.archive_json = archives
        section.label_json = labels
        section.put()

        self.write('Finished')
Esempio n. 4
0
    def get_list(self, *args, **kwargs):
        count, threads = super().get_list(*args, **kwargs)

        for thread in threads:
            thread.forum = Forum.get(thread.forum).title
            thread.section = Section.get(thread.section).title
            thread.author = User.get(thread.author).username

        return count, threads
Esempio n. 5
0
    def get(self):
        item = None
        if self.request.get('key'):
            item = Section.get( self.request.get('key') )

        vals = {
            'item' : item,
            'types' : models.type_choices,
            'layouts' : models.layout_choices,
            }
        self.template( 'section-form.html', vals, 'admin' );
Esempio n. 6
0
    def get_list(self, *args, **kwargs):
        count, sections = super().get_list(*args, **kwargs)

        for section in sections:
            section.forum = Forum.get(section.forum).title
            parent = Section.get(section.parent)
            if parent:
                section.parent = parent.title
            section.created_by = User.get(section.created_by).username

        return count, sections
Esempio n. 7
0
    def post(self):
        item = None
        vals = {}
        try:
            # get all the incoming values
            path = self.request.get('path').strip()
            title = self.request.get('title').strip()
            description = self.request.get('description')
            type = self.request.get('type')
            layout = self.request.get('layout')
            attribute_raw = util.make_attr_raw_string(
                {
                    'sitemap-entry' : self.request.get('sitemap_entry'),
                    'contact-form'  : self.request.get('contact_form'),
                    'sitefeed'      : self.request.get('sitefeed'),
                    }
                ).strip()

            # some pre-processing of the input params
            description_html = util.render(description, type)

            if self.request.get('key'):
                item = Section.get( self.request.get('key') )
                item.path = path
                item.title = title
                item.description = description
                item.description_html = description_html
                item.type = type
                item.layout = layout
                item.attribute_raw = attribute_raw
            else:
                item = Section(
                    path = path,
                    title = title,
                    description = description,
                    description_html = description_html,
                    type = type,
                    layout = layout,
                    attribute_raw = attribute_raw,
                    )

            # update and save this section
            item.set_derivatives()
            item.put()
            # once saved, add the section to the two task queues
            item.regenerate()
            self.redirect('.')
        except Exception, err:
            vals['item'] = self.request.POST
            vals['err'] = err
            vals['types'] = models.type_choices
            vals['layouts'] = models.layout_choices
            self.template( 'section-form.html', vals, 'admin' );
Esempio n. 8
0
    def get(self):
        try:
            if self.request.get('key'):
                item = Section.get( self.request.get('key') )

                vals = {
                    'item' : item,
                    }
                self.template( 'section-del.html', vals, 'admin' );
            else:
                self.redirect('.')
        except:
            self.redirect('.')
Esempio n. 9
0
 def post(self):
     try:
         item = Section.get( self.request.get('key') ) if self.request.get('key') else None
         if item is not None:
             try:
                 item.delete()
                 self.redirect('.')
             except:
                 vals = {
                     'item' : item,
                     'err' : 'There was an error when deleting this section, please try again'
                     }
                 self.template( 'section-del.html', vals, 'admin' );
     except:
         self.redirect('.')
Esempio n. 10
0
    def post(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.write('Started section duplicate node check:')

        section_key = self.request.get('section_key')
        name = self.request.get('name')
        self.write('- key  = ' + section_key)
        self.write('- name = ' + name)

        # get the section first (and if the section_key is crap just finish)
        section = None
        try:
            section = Section.get( self.request.get('section_key') )
        except:
            # by just returning here, it ends up a 200, so all is good
            return

        if section is None:
            self.write('No section found')
            logging.warn( 'No section found for key: ' +  section_key )
            return

        nodes = Node.all().filter('section =', section).filter('name =', name)
        if nodes.count() <= 1:
            msg = 'Only [%d] nodes of this name in this section' % nodes.count()
            self.write(msg)
            return

        msg = 'More than one node named [%s] in section [%s]' % (name, section.path)
        self.write(msg)
        logging.warn(msg)
        admin_email = util.config_value('Admin Email')
        if not mail.is_email_valid(admin_email):
            return

        url_edit = util.construct_url() + '/admin/node/'
        body = 'Section %s has two nodes named %s ' % (section.path, name)
        mail.send_mail(admin_email, admin_email, 'Duplicate node name in section ' + section.path, body)
Esempio n. 11
0
    def get(self):
        section = None
        if self.request.get('section'):
            try:
                section = Section.get( self.request.get('section') )
            except BadKeyError:
                # invalid key
                self.redirect('.')
            pages = Page.all().filter('section =', section).order('-inserted').fetch(page_count+1)
        else:
            pages = Page.all().order('-inserted').fetch(page_count+1)

        more = True if len(pages) > page_count else False

        vals = {
            'title'      : 'Page List',
            'sections'   : Section.all(),
            'section'    : section,
            'pages'      : pages,
            'page_count' : page_count if more else len(pages),
            'more'       : more,
        }
        self.template( 'page-list.html', vals, 'admin' );
Esempio n. 12
0
def section_view(section_id):
    section = Section.get(section_id)
    forum = Forum.get(section.forum)
    parent_section = Section.get(section.parent)
    subsections = Section.filter(parent=section.id)

    # Threads
    try:
        page = int(request.args.get('page'))
    except (TypeError, ValueError):
        page = 1

    if not page or page < 1:
        page = 1

    threads_count = len(Thread.filter(section=section.id))
    # FIXME(a.telishev): Threads per page by search
    pages_count = threads_count // THREADS_PER_PAGE

    if page > pages_count:
        page = pages_count

    prev_page = page - 1
    next_page = page + 1
    if page == 1:
        prev_page = None
    if page == pages_count:
        next_page = None

    offset = (page - 1) * THREADS_PER_PAGE

    search = request.args.get('search', '')
    search_condition = f'AND thread.title LIKE "%{search}%"' if search else ''
    query = f"""
        SELECT
            thread.id thread_id,
            thread.title thread_title,
            thread.created_at thread_created_at,
            thread.last_answer_time thread_last_answer_time,
            user.id user_id,
            user.username username
        FROM thread
        INNER JOIN user ON thread.author = user.id
        WHERE section = %(section_id)s {search_condition}
        ORDER BY thread.last_answer_time DESC
        LIMIT %(limit)s
        OFFSET %(offset)s;
    """
    cursor = get_connector().cursor()
    cursor.execute(query, {
        'section_id': section.id,
        'limit': THREADS_PER_PAGE,
        'offset': offset,
    })
    threads = {
        thread_id: {
            'id':
            thread_id,
            'title':
            thread_title,
            'created_at':
            thread_created_at.strftime('%d %b %Y'),
            'created_at_h':
            thread_created_at.strftime('%d %b %Y\n%H:%M:%S'),
            'last_answer_time':
            (thread_last_answer_time.strftime('%d %b %Y\n%H:%M:%S')
             if thread_last_answer_time else None),
            'user_id':
            user_id,
            'username':
            username,
        }
        for (
            thread_id,
            thread_title,
            thread_created_at,
            thread_last_answer_time,
            user_id,
            username,
        ) in cursor
    }

    if threads:
        answers_count_query = f"""
            SELECT
                thread.id,
                COUNT(*)
            FROM thread INNER JOIN answer on thread.id = answer.thread
            WHERE thread.id IN ({
                ', '.join(str(thread_id) for thread_id in threads)
            })
            GROUP BY thread.id;
        """
        cursor.execute(answers_count_query)
        for thread_id, answers_count in cursor:
            threads[thread_id]['answers_count'] = answers_count

    cursor.close()

    return render_template(
        'section.html',
        section=section,
        forum=forum,
        parent_section=parent_section,
        subsections=subsections,
        threads=threads.values(),
        search=search,
        next_page=next_page,
        curr_page=page,
        prev_page=prev_page,
    )
Esempio n. 13
0
    def post(self):
        msgs = []
        section = None
        try:
            section = Section.get( self.request.get('section_key') )
        except BadKeyError:
            # invalid key, try again
            self.redirect('.')

        node_type = self.request.get('node_type')
        data_input = self.request.POST.get('data_input').file.read()
        data_type = self.request.get('data_type')

        data = None
        if data_type == 'json':
            data = json.loads( data_input )
        elif data_type == 'yaml':
            data = yaml.load( data_input )
        else:
            # someone is messing with the input params
            self.redirect('.')

        # logging.info( 'data=' + json.dumps( data ) )

        # figure out what this node_type is
        item = None
        if node_type == 'page':
            item = Page(
                section = section,
                name = data['name'],
                title = data['title'],
                content = data['content'],
                type = data['type'],
                label_raw = ' '.join( data['label'] ),
                attribute_raw = ' '.join( data['attribute'] ),
                inserted = util.str_to_datetime( data['inserted'] ),
                updated = util.str_to_datetime( data['updated'] ),
                )

        elif node_type == 'recipe':
            item = Recipe(
                section = section,
                name = data['name'],
                title = data['title'],
                intro = data['intro'],
                serves = data['serves'],
                ingredients = data['ingredients'],
                method = data['method'],
                type = data['type'],
                label_raw = ' '.join( data['label'] ),
                attribute_raw = ' '.join( data['attribute'] ),
                inserted = util.str_to_datetime( data['inserted'] ),
                updated = util.str_to_datetime( data['updated'] ),
                )

        else:
            # again, someone is messing with the input params
            self.redirect('.')

        # regenerate this item
        item.set_derivatives()
        item.put()
        item.section.regenerate()

        # output messages
        msgs.append( 'Added node [%s, %s]' % (item.name, item.title) )

        if 'comments' in data:
            for comment in data['comments']:
                # load each comment in against this node
                comment = Comment(
                    node = item,
                    name = comment['name'],
                    email = comment['email'],
                    website = comment['website'],
                    comment = comment['comment'],
                    comment_html = util.render(comment['comment'], 'text'),
                    status = comment['status'],
                    inserted = util.str_to_datetime( comment['inserted'] ),
                    updated = util.str_to_datetime( comment['updated'] ),
                )
                comment.put()
                msgs.append( 'Added comment [%s, %s]' % (comment.name, comment.email) )

        # now that we've added a node, regenerate it
        item.regenerate()

        # also, check that this section doesn't have duplicate content
        Task( params={ 'section_key': str(section.key()), 'name': item.name }, countdown=30, ).add( queue_name='section-check-duplicate-nodes' )

        vals = {
            'msgs'        : msgs,
            'node_type'   : node_type,
            'section_key' : section.key(),
            'data_type'   : data_type,
            }
        self.template( 'load-import.html', vals, 'admin' );
Esempio n. 14
0
def thread_view(thread_id):
    thread = Thread.get(thread_id)

    forum = Forum.get(thread.forum)
    section = Section.get(thread.section)
    parent_section = Section.get(section.parent)
    author = User.get(thread.author)

    answers_query = """
        SELECT
            answer.id answer_id,
            answer.text answer_text,
            answer.rating answer_rating,
            answer.created_at answer_created_at,
            answer.is_off_topic answer_is_off_topic,

            user.id user_id,
            user.username username,
            user.msg_count user_msg_count,
            user.msg_signature user_signature,
            user.registered_at user_registered_at
        FROM answer
        INNER JOIN user ON answer.author = user.id
        WHERE answer.thread = %(thread_id)s
        ORDER BY answer.created_at;
    """
    cursor = get_connector().cursor()
    cursor.execute(answers_query, {'thread_id': thread.id})

    answers = [
        Answer(
            id=answer_id,
            text=answer_text,
            rating=answer_rating,
            created_at=answer_created_at,
            is_off_topic=answer_is_off_topic,
            author=User(
                id=user_id,
                username=username,
                msg_count=user_msg_count,
                msg_signature=user_signature,
                registered_at=user_registered_at,
            )
        )
        for (
            answer_id,
            answer_text,
            answer_rating,
            answer_created_at,
            answer_is_off_topic,

            user_id,
            username,
            user_msg_count,
            user_signature,
            user_registered_at,
        ) in cursor
    ]

    labels_query = """
        SELECT
            thread_label.id label_id,
            thread_label.text label_text
        FROM threads_labels
        INNER JOIN thread_label ON threads_labels.label = thread_label.id
        WHERE threads_labels.thread = %(thread_id)s;
    """
    cursor = get_connector().cursor()
    cursor.execute(labels_query, {'thread_id': thread.id})

    labels = [
        ThreadLabel(id=label_id, text=label_text)
        for label_id, label_text in cursor
    ]

    cursor.close()

    return render_template(
        'thread.html',
        forum=forum,
        parent_section=parent_section,
        section=section,

        author=author,
        thread=thread,
        labels=labels,
        answers=answers,
    )
Esempio n. 15
0
async def put(id:int, section:PydanticSectionIn):
	await Section.filter(id=id).update(**section.dict(exclude_unset=True))
	return await PydanticSection.from_queryset_single(Section.get(id=id))
Esempio n. 16
0
async def get(id:int):
	return await PydanticSection.from_queryset_single(Section.get(id=id))