Python Daily
2.56K subscribers
1.49K photos
53 videos
2 files
39.1K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
2D CAD/vectors drawing library

Hey, I'm looking to draw some patterns in Python based on a set of inputs. Need to do things like lines, tangents, and arcs. Does anyone know of library I could use to draw on a canvas?

/r/Python
https://redd.it/8j0vr2
help to create a custom user form

Hi I would like to know how I can customize my user model, I haves this model:

from django.db import models

from django.contrib.auth.models import AbstractUser, UserManager

class CustomUserManager\(UserManager\):

pass

class CustomUser\(AbstractUser\):

objects = CustomUserManager\(\)

and I have this form

from django import forms

from django.contrib.auth.forms import UserCreationForm, UserChangeForm

from .models import CustomUser

class CustomUserCreationForm\(UserCreationForm\):

class Meta\(UserCreationForm.Meta\):

model = CustomUser

fields = \('username', 'email'\)

class CustomUserChangeForm\(UserChangeForm\):

class Meta:

model = CustomUser

fields = UserChangeForm.Meta.fields

I need to know how I can add more fields to the model and how implement it in the form for example I want to add ID, adrees and Bday fields

/r/djangolearning
https://redd.it/8j1htl
[AF] Updated website is causing problem for old users, possible cookie issues

To remove "server/main.cgi/" from example.com/server/main.cgi/mypage I changed

@app.route('/mypage/')

to

@app.route('/mypage')

But now, when my old users click the button to open /mypage, the url "example.com/server/main.cgi/mypage/" is requested, which is no longer used. Altough the button url says example.com/mypage.

I am not sure if this is caused by old cookies or cache, but there are no issues when i open the website in a private window.

Also, I am sure there must be some better way to remove that part of the url, because now, /mypage/ wont work.

My .htaccess file looks like this.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /home/www/server/main.cgi/$1 [L]

main.cgi

#!/usr/bin/python3
import sys
sys.path.insert(0, '/home/.local/lib/python3.4/site-packages')

from wsgiref.handlers import CGIHandler
from myapp import app

CGIHandler().run(app)

myapp.py

from flask import Flask, render_template

app = Flask(__name__, static_folder="../static",
template_folder="../templates")

@app.route('/mypage')
def mypage():
return render_template('mypage.html')

if __name__ == '__main__':
app.run()

I appreciate any help I can get on this issue.

Edit: All my imports are listed with for example "/server/main.cgi/static/js/bootstrap.bundle.js". Is it possible to change it to "/static/js/bootstrap.bundle.js"?

/r/flask
https://redd.it/8j349j
Should I use cron or celery for a task to be performed at the exact same time twice a month?

I need to perform a specific task at exactly 12:00 AM on the 1st and 16th day of the month. The task is pretty simple; it is setting a model IntegerField to 0.

I know the 2 major apps for this are cron and celery. I'm not experienced at all with either of them. Which one of them is optimal for my situation?

/r/django
https://redd.it/8j903y
Django formsets and additional custom fields

Here is my setup:

class MyParentForm(forms.ModelForm):
class Meta:
model = MyParentModel
fields = '__all__'


# my_extra is not a field on MyChildModel
# but it's data is stored in the database in MyExtraModel
# which has a OneToOne rel with MyChildModel
class MyChildForm(forms.ModelForm):
my_extra = forms.CharField(label='Extra', max_length=20)

def save(self, commit=True):
# this function includes (unshown) logic that facilitates
# the save of MyExtraModel with the data from my_extra
super(MyChildForm, self).save(commit)

class Meta:
model = MyChildModel
fields = ['name', 'age']

MyChildFormSet = forms.inlineformset_factory(parent_model=MyParentModel,
model=MyChildModel,
form=MyChildForm)

The formset and general form setup works perfectly fine when I use it in a CreateView.

My issue is that when I attempt to utilize an UpdateView, I don't know how to populate each formset's my_extra field with the data from the one-to-one relation.

class MyParentModelChange(UpdateView):
model = MyParentModel
form = MyParentForm

def get_context_data(self, **kwargs):
data = super(MyParentModelChange, self).get_context_data(**kwargs)
initial = self.request.POST if self.request.POST else None
data['child_formset'] = MyChildFormSet(initial, instance=self.object, prefix='children')
return data

You can see that I am passing the ParentModel instance object, and so it's children set are also being passed in to the formset. The formset populates `child.name` and `child.age` fine, but it does not know about the additional my_extra field.

Child also has a `child.extra` (from the `OneToOne` relationship with `MyExtraModel`) that the formset doesn't know to pull from to populate for the my_extra form field.

How can I go about fixing this?

Here is what I have thought of and tried:

* I thought perhaps the instance data is available on FormSet `__init__`, so I made a custom `BaseInlineFormSet` descendant. In the `__init__()` of my custom descendent, after the `super().__init__()` call, I tried to print `self.data` and `self.initial` and got empty values from both. This indicates that the formsets aren't populated with this information when I look at it in the UpdateView, so I need to look elsewhere.

* So would extending `formset.add_fields()` be the way I should go about it?

* Is there perhaps another method I should be overriding to accomplish this?

Thanks!

/r/django
https://redd.it/8j5mc0
[AF] The easiest way to deploy a small app in a LAN?

I just wrote a very small app (it doesn't even use a DB) and I need to deploy it in a LAN on a Debian 9 based server. It should run as a system service. It will only be used by a couple of users, and as of now I don't really care about security, performance, or logging. What is the fastest and easiest way to deploy it?

/r/flask
https://redd.it/8j74zq
Does anyone use DigitalOcean spaces?

I want to use object storage for one of my apps. DigitalOcean Spaces sounds interesting. I've never used S3 but I've heard that Spaces integrates nicely with existing S3 tools and libraries.

Does anyone have experience with DO spaces? Is it easy to use/setup? How do you use it with Flask?

UPDATE:

I discovered this: https://www.digitalocean.com/community/questions/how-to-use-digitalocean-spaces-with-the-aws-s3-sdks

I was able to create a bucket and upload a file so hopefully, I'll be able to integrate it into my app.

/r/flask
https://redd.it/8dl0nv
SQlite realistic for a small we app to practice SQL?

I love flask, with that out of the way I am starting to get into SQL for the first time by making a silly little app where users sign a guestbook (which gets written to the sqlite3 database) and then get rerouted to a page where it shows the complete guestbook in a table format. I am going to deploy this and show it to people. I expect traffic to be low, but I guess it is possible that two people might try to write at the exact same time, which is the issue with SQlite right? What exactly happens if two people try to write at the same time? Is this easy to dodge with error handling?

/r/flask
https://redd.it/8dgyd4
This media is not supported in your browser
VIEW IN TELEGRAM
Lucky Cat spinning-arm persistence of vision display, powered by MicroPython

/r/Python
https://redd.it/8javkt
Select Multiple Items with request.form?

http://loudev.com/#usage

I'm using this plugin to let my users select multiple items, for example the fruits they like, out of a fairly big list (40 or so elements).

However, when I do request.form['fruit_pick'] it only seems to return the first one they've picked, for example elem_1.

Is there any way to do what I want to do more efficiently?

/r/flask
https://redd.it/8dg237