Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
Upload and process a remote file called by an API REST call with a Django backend

The application is written in Django. I need to A) upload a file (an Excel spreadsheet) from a remote location (it could be via HTTP or SFTP), headless, i.e. without a form--could it be a call to an external process?, B) triggered by a signal/API call issued by a remote client. The file will then be processed (it could be synchronously or asynchronously) to update a database in the backend (this latter part is already working and is implemented with a file which is read locally). What is the best strategy/architecture, and tools (packages, commands, etc.) to use to solve both points A) and B) above?

/r/django
https://redd.it/6c9za7
Help with differentiating types of users

Hello everyone, I'm a bit confused as to how about I can solve this problem.

I have two types of users: parent and child.

Parent users can CRUD objects, and child users can only view.

What is the best way to create a login system so that creating a user can choose between one of these types and that upon login they are directed the their view of their respective user?


Should I just make a User class with a boolean attribute as Parent=true/false?




/r/flask
https://redd.it/6by6zv
Unique Form Urls to update Model Instances

I have a model with a project number field and notes field. I would like to generate a URL for each project number, and on that page have a form field that would allow me to update that note. Any ideas?

I'm getting a name error in my view that "request" is not defined.

Here's what I have so far...

Model:

class Projectsummary(models.Model):
number = models.CharField(db_column='Number', primary_key=True, max_length=50) # Field name made lowercase.
projectnote = models.TextField(db_column='ProjectNote', blank=True, null=True)
overrunnote = models.TextField(db_column='OverrunNote', blank=True, null=True)

View:

def inputs(requset, number):
formnumber = Projectsummary.objects.get(number=number)
form = ProjNoteField(request.POST or None, instance=formnumber)
if form.is_valid():
form.save()
return HttpResponseRedirect('app/pmhub/')
context = {'form': form}
return render(requset, 'projectinputfields.html', context)

Forms:

class ProjNoteField(forms.ModelForm):
class Meta:
model = Projectsummary
fields = ['projectnote', 'overrunnote']

/r/django
https://redd.it/6cc5vp
Passing variables within functions

Hello everyone,

I have few function which look something like this:

@login_required
def get_pages(request, startDate='2017-01-01', endDate='today'):
do_something..

@login_required
def get_speed(request, startDate='2017-01-01', endDate='today'):
do_something..

etc..

These functions get the data for the correct period and save it in my db. Now I want a new function which calls them changing the startDate & endDate parameter, so I can update metrics daily.

I tried something like this:

def update_last2days(requests):
get_speed('yesterday','today') # The API accepts yesterday & today as input format. Consider it as YYYY-MM-DD
get_pages('yesterday','today')
return redirect('/report?updated=true')

I also tried:

def update_last2days(requests):
get_speed(request, 'yesterday','today')
get_pages(request, 'yesterday','today')
return redirect('/report?updated=true')

But I keep getting `'str' object has no attribute 'user'` and I can't figure out why. How can I call a function with new parameters?

P.s. when I add the values `today` and `yesterday` to the main function all works just fine.

/r/django
https://redd.it/6ccwy3
Porting a program I wrote in python to be a web app. Have questions about how to structure data files.

Okay, so I recently learned python so that I could write a drop logging program for an MMO that I play. It might be sloppy (I don't know, this is my first coding project ever), but all of the features I initially envisioned function properly and I want other people to be able to use it now. Initially I was going to package it into an .exe and distribute that, but after some research and thinking on it for a bit, I've decided it makes more sense and will be better in the long run to make a web app with the same functionality for users to access.

My question is, my drop logger utilizes text files to pull drop table data for monsters from as well as save drop logs to. (I.e. a text file named "monster name droptable.txt" with a tab separated column/row structure for the items it can drop like item name, item quantity, etc...).

Drop logs that users create are also saved and loaded from txt files. I've gone through a few tutorials and it seems Flask would be perfect for making a simple web app that functions the same as my program. But, after looking into databases, I'm not sure how to store my data for retrieval. Is it still okay/efficient to just have static text files on the server and if I want to edit a drop table for a monster, or add a new monster, I can just edit/add a text file? Or should I store this information in a database. I feel like each monster would need it's own database, so is there a limit/problem with having a ton of databases?

As for user saved files. If a user wants to save a drop log to load at a later time, (or download to their computer) I knew I'd have to use a database to store this data (at least a filepath to their specific text drop log file), but should I store the drop logs themselves in databases?

Sorry if these are newbie questions, I'm just ready to jump into this and rebuild my program as a web app, but I don't even know which tutorials I need to be looking at because I don't know what's best. I don't want to do it one way only to find out a few days/weeks from now that it was a bad decision and backtrack to square one.

Thank you for any help!

/r/flask
https://redd.it/6ccncc
Anyone tried using Brython in Flask/Python apps? What were your thoughts?

[Brython](http://brython.info/) is a Python-to-JavaScript tool, for anyone unaware.

/r/flask
https://redd.it/6br4vb
Logging number of requests to a rest api

Hi Reddit, I am working on tracking the number of times our rest api endpoints are called over time. What are some best practices for doing this?

I'm thinking I could write some python to update a JSON file each time the API is called, however this API is called thousands of times a day and am afraid this might be way to expensive of a process to launch each time it's called.

Any help is appreciated!

/r/flask
https://redd.it/6bqf4e
Question: How to verify that a user is posting (POST) to a flask webapp only using my CLI?

I am building an application which send POST request to an endpoint. I want to block the user from manually posting the POST request using some tools. I want to make sure that the user is posting to the flask webapp from my python script. What is the best way to implement this?

/r/Python
https://redd.it/6cfi2i
Kalliope v0.4.4 is out !

Kalliope is a modular always-on voice controlled personal assistant designed for home automation.

This new release brings new features, fixes many bugs and new plugins (called neurons !).

You can check details, videos and the neuron market place on the github page : https://kalliope-project.github.io/

The target is a rasp3 on raspbian, but you can quickly test it on a Ubuntu. You just need a microphone and a speaker/heapphones.

Python 2.7, 3.4, 3.5 and 3.6 are supported.

The project is open source under the MIT licence.

Find sources on github : https://github.com/kalliope-project/kalliope



Here is the changelog for the v0.4.4:

- Fix: Uppercase in order/parameters/global variables are now handled correctly
- Fix: usage of integer in neuron parameters
- Fix: encoding with special character
- Refactor main controller. Use a LIFO to allow full usage of kalliope via API (even with neurotransmitter)
- Add a systemd script to start kalliope automatically
docker testing
- python 3 support 3.4, 3.5, 3.6
- Increase testing code coverage
- Fix: Raspberry performance. CPU usage from 120% to 15%
- Input value refactoring. "args" parameter replaced by jinja templating
- Review TTS overriding config in neuron declaration
- Fix: accapela TTS
- LED and mute button support for Raspberry Pi
- Player modularity


Enjoy !

/r/Python
https://redd.it/6cgwic
I'm having problems with Django/nginx/uwsgi and uploading files larger than ~4MB

So, I don't know if this is the correct location to ask these questions, but I'm, well, kinda hopeless now. I've read through halve the internet for weeks without any luck.

I'm *very new* to hosting Django applications (or actually, I'm new to hosting and Linux in general too) so maybe I keep doing something stupid. Anyway, the problem:

I'm working on a (personal) Django website. In the Django admin, there's a page where you can upload multiple images and a optional pdf. So far so good. It works flawlessly on my local machine. However, when I run this on the server, things get weird.

When the *total* request size is smaller than ~4MB (e.g. 1 pdf of 2MB, or 4 images of 700KB etc.) everything works fine. When the request size is larger, the files get saved on the server without a single problem. **But**, they then become inaccessible and I get nginx 403 errors on those files. The Linux permissions are actually different. A 'good' file gets 664 as permissions, a 'bad' file only has 600.

I'm having this problem for weeks now and I'm so frustrated now. To the point that I'm just getting demotivated to even work on it. I've tried so many things, even completely resetting the whole server multiple times, but this stupid ass problem just keeps re-appearing. If *anyone* has the solution or the golden tip, I'd be sooo grateful.

Thanks for reading! :)

-----------

Running on a Linode server with:
Ubuntu 17.04, Python 3, Django 1.11, Nginx, Uwsgi

/r/django
https://redd.it/6cfg3c
Render? Redirect? When validating form...

Hello, so I'm few week in with Flask, it's real fun, I enjoy it quite a bit. But there are still things I don't understand.

Now I'm fighting with this:

I've got a page `issue_status.html` which contains 2 tables with data acquired from a database. This is a route to which I link my links.

@app.route('/issue_status/')
def issue_status():
cur, db = get_db(cursor=True)
issues = cur.execute("SELECT * FROM issues").fetchall()
resolved, unresolved = [], []

for row in issues:
issue = row[1]
found = cur.execute("SELECT * FROM resolved_issues WHERE issue = ?", [issue]).fetchall()
if len(found) > 0:
resolved.append(row)
else:
unresolved.append(row)

all_issues = {'resolved': resolved, 'unresolved': unresolved}
today = datetime.datetime.today().strftime('%Y-%m-%d')
return render_template('issue_status.html', all_issues=all_issues, today=today)

The page contains a form too for adding new issues. It's in the same `issue_status.html` page. When user clicks on `submit`, it would go to this function:

@app.route('/add_issue/', methods=['GET', 'POST'])
def add_issue():
if not session.get('logged_in'):
abort(401)

cur, db = get_db(cursor=True)
issue = request.form.get('issue')
version = ""
date_resolved = ""

existing = cur.execute("SELECT * FROM issues WHERE issue = ?", [issue]).fetchall()
if len(existing) > 0:
flash("This issue already exists...", 'danger')
return redirect(url_for('issue_status'))
....
....
....
flash('New issue was successfully added into database.', 'success')
return redirect(url_for('issue_status'))

**The problem:** I wanted to add to this function **form validation** but don't know how/what to return if the validation fails.

Because when I render the page, I'm missing the data parsed to `issue_status.html` from `issue_status` function. And when I tried to redirect to `issue_status`, that failed somehow too.

I would like to somehow only show under each field (Issue number, username, Description) a form invalid error without refreshing the page. How to do this elegantly?

If you want to see details what I have till now, the WebApp is at [github.com](https://github.com/SonGokussj4/beta-issues)

Any help would be appreciated.

[Screenshot](http://i.imgur.com/zKXV17W.jpg) for visual aid.

/r/flask
https://redd.it/6cidqh
Just installed django, but get ModuleNotFoundError when I run "django-admin --version" (traceback indluded)

Traceback (most recent call last):
File "C:\Users\X\AppData\Local\Programs\Python\Python36-32\Scripts\django-admin-script.py", line 11, in <module>
load_entry_point('django==1.11.1', 'console_scripts', 'django-admin')()
File "c:\users\x\appdata\local\programs\python\python36-32\lib\site-packages\pkg_resources\__init__.py", line 560, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "c:\users\x\appdata\local\programs\python\python36-32\lib\site-packages\pkg_resources\__init__.py", line 2648, in load_entry_point
return ep.load()
File "c:\users\x\appdata\local\programs\python\python36-32\lib\site-packages\pkg_resources\__init__.py", line 2302, in load
return self.resolve()
File "c:\users\x\appdata\local\programs\python\python36-32\lib\site-packages\pkg_resources\__init__.py", line 2308, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "c:\users\x\appdata\local\programs\python\python36-32\lib\site-packages\django\__init__.py", line 3, in <module>
from django.utils.version import get_version
ModuleNotFoundError: No module named 'django.utils'

/r/django
https://redd.it/6chwky