Forum Archive

post to pythonista forum from pythonista app

cook

It works!

Here's a user interface that you can set up to run from the action extension (sized for iPad). It automatically copies the code from the current file into the post draft. Once you are ready to post you can go through a markdown preview and then submit it. Perhaps this makes it easier to create posts. I don't like using web-interfaces too much, so, why not!

If you want to use it you need to set your username and password in the top.

I'm just starting to learn python and this is kind of an interesting project for me. I've been wanting to tackle the UI so this helped me more. I know we can use the UI Editor but I wanted to try and code it.

I'm still not sure how to deal with the delegates. I kind of wanted to have a textview and a live preview of the markdown - which I believe requires delegates for the textview but it just doesn't make a lot of sense to me yet. The way it is now works nice though!

I also couldn't get the @ui.in_background to work. I wanted to just give console alerts for some parts but I ended up just creating a UI popover alert which is sufficient - but not as nice I suppose.

I really hope this doesn't help contribute to any spam. My inbox has been flooded recently. If you modify this please don't loop the post bit!!!!!!!!
@omz, I really hope this doesn't cause any trouble on the forum. My intent is just to create something to interact with the forum from the app.

import urllib
import ui
import editor
import markdown
import requests

omz_forum_username = ''
omz_forum_password = ''

def submit_post(sender):
    global omz_forum_password, omz_forum_username
    login_info = {'username': omz_forum_username, 'password': omz_forum_password,}
    login_data = urllib.urlencode(login_info)

    if not title_field.text:
        alert_text.text = 'You need a Title!'
        alert.present('popover')
        return None

    if sc.selected_index == 0:
        sc_option = 'General'
    elif sc.selected_index == 1:
        sc_option = 'Share Code'
    elif sc.selected_index == 2:
        sc_option = 'Questions'

    post_info = {'title' : str(title_field.text), 'category' : sc_option, 'text' : post_edit_box.text, 'forum' : 'pythonista'}
    post_data = urllib.urlencode(post_info)
    clen = '\'%s\'' % len(post_edit_box.text)
    print clen

    with requests.session() as c:
        login = c.post('https://omz-forums.appspot.com/login?%s' % login_data)
        post_headers = {'content-length': clen, 'content-type': 'text/html; charset=UTF-8'}
        post_url = 'http://omz-forums.appspot.com/pythonista/new?%s' % post_data
        if login.cookies:
            debug = c.post(post_url, headers=post_headers)
            alert_text.text = 'Submitted'
            alert.present('popover')
            view.close()
        elif not login.cookies:
            alert_text.text = 'Login Unsuccessful'
            alert.present('popover')

def cancel_alert(sender):
    alert.close()

def submit_test(sender):
    testtext = post_edit_box.text
    post_edit_box.text = 'testing' + testtext

def insert_code():
    return 'Write Something!\n\n<pre><code>\n\n%s\n</code></pre>' % editor.get_text()

def markdown_prev(sender):
    mkd_w.load_html(markdown.markdown(post_edit_box.text))
    mkd.present('fullscreen')

#UI Code
view = ui.View()
view.background_color = 'white'
markdown_prev = ui.ButtonItem(title='Markdown Preview', action=markdown_prev, enabled=True)
view.right_button_items = [markdown_prev]

#Title Field
title_field = ui.TextField()
title_field.frame = 5,8,600,30
title_field.corner_radius = 5
title_field.bordered = True
title_field.border_color = '#999999'
title_field.border_width = 1
title_field.placeholder = 'Title of Post (Required)'
view.add_subview(title_field)

#Post Type
sc = ui.SegmentedControl()
sc.segments = ['General','Share Code','Question']
sc.enabled = True
sc.selected_index = 0
sc.frame = 615,8,400,30
view.add_subview(sc)

#Label
post_label = ui.Label()
post_label.frame = 5,45,250,30
post_label.text = "Edit your post:"
view.add_subview(post_label)

#Post Draft Box
post_edit_box = ui.TextView()
post_edit_box.frame = 5,80,1014,620
post_edit_box.font = ('Arial',14)
post_edit_box.border_width = 1
post_edit_box.border_color = '#999999'
post_edit_box.corner_radius = 5
post_edit_box.text = insert_code()
view.add_subview(post_edit_box)

#Markdown Preview View
mkd = ui.View()
submit = ui.ButtonItem(title='Submit', action=submit_post, enabled=True)
mkd.right_button_items = [submit]
mkd_w = ui.WebView()
mkd_w.scales_page_to_fit = False
mkd_w.flex = 'WH'
mkd.add_subview(mkd_w)

#Alert Popup
alert = ui.View()
alert.name = 'Notice:'
alert.width, alert.height = 300,300
alert_text = ui.TextView()
alert_text.frame = 0,0,300,260
alert_button = ui.Button()
alert_button.title = 'Okay'
alert_button.frame = 0,260,300,40
alert_button.action = cancel_alert
alert.add_subview(alert_text)
alert.add_subview(alert_button)

view.present('fullscreen')

techteej

Will try soon, but you should post it to Github. Then we could add it to Pythonista Tools ;)

cook

@techteej please feel free to put it there. I don't have a Github account!!

Gerzer

Pythonista Tools just links to code hosted on the other people's personal GitHub accounts. You should really get a GitHub account. It's free and you'll find yourself using it a lot. :)

ccc

Agreed. GitHub is free and is really worth learning. The approach that we take with Pythonista-Tools is that it is NOT a repository of code but instead it contains pointers to other repositories. This way each person contributing can update their own code, readme, and license without needing central coordination.

cook

Okay I got an account.
I suppose someone needs to add it to Pythonista tools. Here's the URL:

https://gist.github.com/danrcook/f200e67fac3f46fc9bf4

cook

Wow sorry that's wrong.
I uploaded straight from Pythonista but it created a gist.
I've now got a repository:

https://github.com/danrcook/Post-to-Pythonista-Forum

techteej

@cook Done!