ok, I've created custom Auth model, by inhereting from AbstractBaseUser. When i create simple user, everything is Ok, but then i try to create superuser something goes wrong.
here is my User and UserManager:
class PatientUserManager(BaseUserManager):
def create_user(self, snils, name=None, surname=None, telephone=None, password=None, is_superuser=None):
'''Создание пользователя со снилсом, именем, фамилией и паролем'''
if not snils:
raise ValueError('Отсутствует Номер СНИЛС')
user = self.model(snils, name, surname, telephone)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, snils, name, surname, telephone, password):
user=self.create_user(snils, name, surname, telephone)
user.is_superuser = True
user.is_admin = True
user.is_staff = True
user.set_password(password)
user.save(using=self._db)
return user
class PatientUser(AbstractBaseUser, PermissionsMixin):
snils = models.CharField('СНИЛС', max_length= 14, unique=True)
email = models.EmailField('email', max_length= 255, blank=True)
name = models.CharField('Имя', max_length= 50, blank=True)
surname = models.CharField('Фамилия', max_length= 80, blank=True)
patronimic = models.CharField('Отчество', max_length= 80, blank=True)
date_joined = models.DateTimeField('Дата Регистрации', auto_now_add=True)
telephone = models.CharField('Номер телефона', max_length= 18, blank=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
USERNAME_FIELD = 'snils'
EMAIL_FIELD = 'email'
REQUIRED_FIELDS = ['name', 'surname', 'telephone']
objects = PatientUserManager()
def get_full_name(self):
'''
Возвращает first_name и last_name с пробелом между ними.
'''
full_name = '%s %s' % (self.name, self.surname)
return full_name.strip()
def get_short_name(self):
'''
Возвращает сокращенное имя пользователя.
'''
return self.name
when i try to
create superuser, i receive error:
ValueError: invalid literal for int() with base 10: '555-555-555 55'
but if enter integer number in 'СНИЛС' field instead of '555-555-555 55', he starts to complain about surname:
СНИЛС: 47
Имя: Vladimir
Фамилия: Suddenok
Номер телефона: 8800
Password:
Password (again):
...
["'Suddenok' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."].
well, i showed perseverance and continued to solve problems as they come. changed Surname:
СНИЛС: 47
Имя: Vladimir
Фамилия: 2018-08-14 12:47
Номер телефона: 8800
Password:
Password (again):
...
django.core.exceptions.ValidationError: ["'8800' value must be either True or False."]
changed Telephone:
СНИЛС: 47
Имя: Vladimir
Фамилия: 2018-08-14 12:47
Номер телефона: True
Password:
Password (again):
...
Traceback (most recent call last):
File "/home/maumba/ProjectPython/VE/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
psycopg2.IntegrityError: null value in column "date_joined" violates not- null constraint
DETAIL: Failing row contains (47, !t0cnBjuTplCtE8vJRGsIgsLN5U0pTDXzg22rWFEF, 2018-08-14 12:47:00+00, t, , , , , , null, , t, f, f).
thanks for your attention
Related
I'm trying to use retrofit2 with Koltin in Android Studio as part of jetpack compose application. I'm sending a POST and keep getting error 500. I don't have access to the server code so I'm trying to figure out what am I doing wrong.
This is the interface I have declared for building the retrofit object:
I tried three different ways of declaring the POST endpoint.
#Singleton
interface IsrPayService {
#Headers ("Content-Type: application/json")
#POST("v3/driver/new-credit-driver")
suspend fun signUp(#Body user: UserDriver): Call\<WsError?\>
#Headers (
"Content-Type: application/json",
"Accept: application/json")
#POST("v3/driver/new-credit-driver")
suspend fun signup2(#Body user: UserDriver): retrofit2.Response<WsError>
#FormUrlEncoded
#POST("v3/driver/new-credit-driver")
suspend fun signupUrlEncoded(
#Field("firstName") firstName: String,
#Field("lastName") lastName: String): retrofit2.Response<WsError>
}
The data I am trying to send is UserDriver and I declared all the internal data classes below:
data class UserDriver(
#SerializedName("firstName") val firstName: String = "default",
#SerializedName("lastName") val lastName: String = "default",
#SerializedName("civilId") val civilId: String = "default",
#SerializedName("vehicleLicensingNumber") val vehicleLicensingNumber: String = "default",
#SerializedName("vehicleManufacturer") val vehicleManufacturer: String = "default",
#SerializedName("vehicleModel") val vehicleModel: String = "default",
#SerializedName("vehicleManufactureYear") val vehicleManufactureYear: String = "1973",
#SerializedName("counterModel") val counterModel: String = "default",
#SerializedName("authorizedEmployerNumber") val authorizedEmployerNumber: String = "default",
#SerializedName("bankAccountId") val bankAccountId: String = "default",
#SerializedName("bankAccountBranch") val bankAccountBranch: Int = 0,
#SerializedName("bankId") val bankId:Int = 123456,
#SerializedName("email") val email: String = "default",
#SerializedName("dob") val dob: DateIndicator = DateIndicator(date = 3, month = 4, year = 2023),
#SerializedName("address") val address: Address = Address (Coordinates(0,0),"מודיעין","טשרניחובסקי","12"),
#SerializedName("phoneNumber") val phoneNumber: String = "default",
#SerializedName("driverLicenseId") val driverLicenseId: Int = 0,
#SerializedName("civilIdPhoto") val civilIdPhoto: String = "default",
#SerializedName("driverLicensePhoto") val driverLicensePhoto: String = "default",
#SerializedName("signaturePhoto") val signaturePhoto: String = "default"
)
data class DateIndicator(
#SerializedName("date")
#Expose
val date: Int,
#SerializedName("month")
#Expose
val month: Int,
#SerializedName("year")
#Expose
val year: Int
)
I think #Expose is not required but tried it just in case..
data class Address (
#SerializedName("coordinates")
#Expose
val coordinates: Coordinates,
#SerializedName("city")
#Expose
val city: String,
#SerializedName("street")
#Expose
val street: String,
#SerializedName("number")
#Expose
val number: String
)
data class Coordinates (
#SerializedName("latitude")
#Expose
val latitude: Int,
#SerializedName("longitude")
#Expose
val longitude: Int
)
I tried to methods of getting the response:
suspend fun driverSignUp(user: UserDriver, onResult: (WsError?) -> Unit) {
try {
ws.signUp(user = user). enqueue (
object: Callback<WsError?> {
override fun onResponse(call: Call<WsError?>, response: Response<WsError?>) {
Log.d("driverSignUp",
"onResponse: response.isSuccessful = ${response.isSuccessful}")
var wsError: WsError? = null
wsError = if(!response.isSuccessful){
WsError(
body = "",
isError = true,
error = yz.learning.isrpaytest.model.Error(
errorCode = response.code(),
errorMessage = ErrorMessage(
enUs = response.message(),
heIl = response.message())))
} else {
response.body()
}
onResult(wsError)
}
override fun onFailure(call: Call<WsError?>, t: Throwable) {
Log.d("driverSignUp", "onFailure: ")
onResult(null)
}
}
)
} catch (exception: Exception) {
Log.d("driverSignUp", "driverSignUp exception: ${exception.message}")
onResult(
WsError(
body = "",
isError = true,
error = yz.learning.isrpaytest.model.Error(
errorCode = 0,
errorMessage = ErrorMessage(
enUs = exception.message!!,
heIl = exception.message!!)))
)
}
}
suspend fun driverSignUp2(user: UserDriver): retrofit2.Response<WsError>{
return ws.signup2(user)
}
I don't understand why I keep getting Internal server error. I have a feeling I have to send the data as a JSON string and not as an Object but as far as I understand this is supposed to be automatically using the gson converter, no?
I can try a simpler endpoint, but I think I will end up with the same problem.
I will appreciate any help since I'm stuck with this issue for a couple of days.
Thanks,
Yariv
I am doing my crm project with SQLITE+FLASK. And I need a feature is let user to input the condition to filer the result.
I hope that my SQL statement can ignore the WHERE condition if the parameter is space or null.
For example, My input is "NAME", "AGE", "GENDER"
so my statement will be
SELECT *
FROM CUSTOMER
WHERE NAME = 'James' AND AGE = '25' AND GENDER = 'M'
But I hope that if user did not enter "NAME" my SQL statement can be something like the code below
SELECT *
FROM CUSTOMER
WHERE AGE = '25' AND GENDER = 'M'
I know maybe I can do this with string concat, but I hope I can do this by SQL statement.
You can do it with the OR operator for each of the columns, by checking also if the parameter value that you pass is NULL or a string with spaces:
SELECT *
FROM CUSTOMER
WHERE (NAME = :name OR TRIM(COALESCE(:name, '')) = '')
AND (AGE = :age OR TRIM(COALESCE(:age, '')) = '')
AND (GENDER = :gender OR TRIM(COALESCE(:gender, '')) = '')
You can use null condition as follows:
SELECT *
FROM CUSTOMER
WHERE (NAME = :name_input or :name_input is null)
AND (AGE = :age_input or :age_input is null)
AND (GENDER = :gender_input or :gender_input is null)
Member function that retrieves db.
def GetDb(self):
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(self.path)
db.row_factory = sqlite3.Row
return db
Member function that returns list from db queries
def GetOrderItemsList(self, orderid):
list = []
with app.app_context():
db = self.GetDb()
cur = db.execute('SELECT * FROM ordersList WHERE orderId = ?',[orderid])
records = cur.fetchall();
for row in records:
print(row)
invid = row['inventoryId']
OrderItem
OrderItem.orderId = row['orderId']
OrderItem.productId = row['inventoryId']
OrderItem.productName = 'none'
OrderItem.quantity = row['quantity']
OrderItem.productPrice = row['price']
nextcur = db.execute('SELECT * FROM inventory WHERE invId = ?', [invid])
#nextcur = db.execute('SELECT * FROM inventory WHERE invId = 1') #works
res = nextcur.fetchone();
OrderItem.productName = res['invName']
print(res['invName'])
list.append(OrderItem)
return list
OrderItem:
class OrderItem(object):
def __init__(self, ordId, invId, name, quantity, price):
self.orderId = ordId
self.productId = invId
self.productName = name
self.quantity = quantity
self.productPrice = price
Error message:
Traceback error
OrderItem.productName = res['invName']
TypeError: 'NoneType' object is not subscriptable
Error
nextcur = db.execute('SELECT * FROM inventory WHERE invId = ?', [invid])
Works
nextcur = db.execute('SELECT * FROM inventory WHERE invId = 1')
Been fighting this for many hours. Searching on google. Reading questions on here.
Any help would be appreciated.
The error
'NoneType' object is not subscriptable
Means that you're trying to access an object's key of an object that doesn't exist, i.e. the object is None.
Please check that here
[invid]
invid is not None, i.e. print (invid)
Also, the issue could be that here
res['invName']
res is None, please check the contents of res (with print, etc) before accessing invName, as it is None.
Fix with join, item instances and img for order product list.
Inventory and OrderItems
create table inventory(invId integer PRIMARY KEY AUTOINCREMENT NOT NULL, invName varchar(50), description varchar(100), invImg varchar(50) ,category integer ,quantity integer, price real);
create table ordersList(orderId integer, inventoryId integer, orderQuantity integer, orderPrice real);
class OrderItem(object):
def __init__(self, ordId, invId, img, name, quantity, price):
self.orderId = ordId
self.productId = invId
self.productName = name
self.productImg = img
self.quantity = quantity
self.productPrice = price
def GetOrderItemsList(orderid):
list = []
db = get_db()
cur = db.execute('SELECT orderId, inventoryId, orderQuantity, orderPrice,
inventory.invName AS invName, inventory.invImg AS invImg FROM ordersList INNERJOIN
inventory ON inventory.invId= ordersList.inventoryId WHERE orderId = ?', [orderid])
records = cur.fetchall();
for row in records:
item = OrderItem(row['orderId'], row['inventoryId'],
row['invImg'], row['invName'],
row['orderQuantity'], row['orderPrice'] )
list.append(item)
return list
I m trying to perform a little calculation and Logic on date.time with Flask application.
1.) the application will calculate the difference between issue date and expiry date called "remaining days" , The application will check if remaining days is less than 365 days and trigger a function
I attempted the first logic to manipulate the data and submit to database
`#bp.route('/cerpacs/add', methods=['GET', 'POST'])
#login_required
def add_cerpac():
"""
Add a an Cerpac/ expartriates to the database
"""
check_admin()
add_cerpac = True
form =CerpacForm()
if form.validate_on_submit():
cerpac = Cerpac(cerpac_serial_no=form.cerpac_serial_no.data,
cerpac_issue_date= form.cerpac_issue_date.data,
cerpac_exp_date=form.cerpac_exp_date.data,
employee =form.employee.data, )
form.cerpac_issue_date.data = cerpac.cerpac_issue_date
form.cerpac_exp_date.data = cerpac.cerpac_exp_date
if request.method == 'POST':
todays_date = datetime.now()
t = cerpac.cerpac_issue_date
t1 = cerpac.cerpac_exp_date
remaining_days = t1 - t
print(remaining_days) - good prints my result!
remaining_days = cerpac.remaining_days ----not adding to database
try:
add cerpac to the database
db.session.add(cerpac)
db.session.commit()
flash('You have successfully added a Cerpac.' )`
`
my model:
class Cerpac(db.Model):
__tablename__ = 'cerpacs'
id = db.Column(db.Integer, primary_key=True)
cerpac_issue_date = db.Column(db.DateTime)
cerpac_exp_date=db.Column(db.DateTime)
remaining_days = db.Column(db.DateTime)
cerpac_serial_no = db.Column(db.String(60))
cerpac_upload = db.Column(db.String(20), default='cerpac.jpg')
renew_status = db.Column(db.Boolean, default=False)
process_status = db.Column(db.Boolean, default=False)
renewcerpac_id = db.Column(db.Integer, db.ForeignKey('renewcerpacs.id'))
employee_id = db.Column(db.Integer, db.ForeignKey('employees.id'))
def __repr__(self):
return '<Cerpac {}>'.format(self.name) model:
I want to add this to database and eventually write a function like this:
I had a mistake also in the code because I had error issue_date not defined. How do I define issue_date as a date.time variable?
def remaining_days(issue_date, expired_date):
issue_date = datetime(issue_date)
days_to_go = expired - issue
if days_to_go == 365:
renew_status== True
print("time to renew")
print("We are Ok")
I would simplify this to something like:
from datetime import datetime
class Cerpac(db.Model):
...
cerpac_exp_date=db.Column(db.DateTime)
...
#property
def remaining_days(self):
return (self.cerpac_exp_date - self.cerpac_issue_date).days
#property
def days_to_expiry(self):
return (self.cerpac_exp_date - datetime.now()).days
Then, days_to_expiry and remaining_days become properties calculated when you query, and update automatically when they renew their cards.
I'm defining a model like this:
class User(UserMixin, db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
first = db.Column(db.String(64))
last = db.Column(db.String(64))
dob = db.Column(db.Date)
street_addr1 = db.Column(db.String(64))
street_addr2 = db.Column(db.String(64))
city = db.Column(db.String(64))
state = db.Column(db.String(2))
zip = db.Column(db.String(9))
gender = db.Column(db.Enum('M', 'F'))
home_box = db.Column(db.String(32))
username = db.Column(db.String(64), unique=True, index=True)
password_hash = db.Column(db.String(128))
#property
def password(self):
raise AttributeError('password is not a readable attribute')
#password.setter
def password(self, password):
self.password_hash = generate_password_hash(password)
def verify_password(self, password):
return check_password_hash(self.password_hash, password)
def __repr__(self):
return '<User %r>' % self.username
#login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
views.py:
#main.route('/', methods=['GET', 'POST'])
def index():
form = RegistrationForm()
if form.validate_on_submit():
user = User()
user.username = form.username.data
user.first = form.first_name.data
user.last = form.last_name.data
user.city = form.city.data
user.dob = form.dob.data
user.gender = form.gender.data
user.home_box = form.home_box.data
user.state = form.state.data
user.street_addr1 = form.street_addr1.data
user.street_addr2 = form.street_addr2.data
user.zip = form.zip.data
user.password = form.password.data
db.session.add(user)
db.session.commit
Right now, inserts are failing because the id is not set:
sqlalchemy.exc.IntegrityError: (IntegrityError) constraint failed 'INSERT INTO users (first, last, dob, street_addr1, street_addr2, city, state, zip, gender, home_box, username, password_hash) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' ('Nate', 'Reed', '1980-11-11', '', '', '', '', '', 'male', '', 'natereed', 'pbkdf2:sha1:1000$N8JHXEDU$24a7724ae9edda81a73e6b92d23aa576ad8284aa')
How do I make id autoincrement? My understanding is it has to be done at the table (not column) level. How do I do this in the code above?
I figured out the problem was I was trying to insert a value for gender that is not valid. It's an enum with values 'M' or 'F'.
It would be nice if the form could just be generated from the model, as that would eliminate the potential for this error. Any pointers on doing that with Flask?