I've a model Order, that has a field created. It's intention is to show when the order took place (date and time speaking).
I'm using this line to create the field in the model:
current_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
However, the reviewing it from admin panel, it shows It was created in the future: 10:52 pm of 27/12. When the time right now is: 6:11 pm. You can see these details in this screenshot:
How can I make sure the correct time gets recorded in productio env? It'll be hosted using Google Cloud Products.
Right know I'm in development env.
View that creates the Order:
#csrf_exempt
def cart_charge(request):
culqipy.public_key = settings.CULQI_PUBLISHABLE_KEY
culqipy.secret_key = settings.CULQI_SECRET_KEY
amount = request.POST.get('amount')
currency_code = request.POST.get('currency_code')
email = request.POST.get('email')
source_id = request.POST.get('source_id')
last_four = request.POST.get('last_four')
dir_charge = {"amount": int(amount), "currency_code": currency_code,
"email": email,
"source_id": source_id}
print(dir_charge)
charge = culqipy.Charge.create(dir_charge)
transaction_amount = int(charge['amount'])/100 #Necesario dividir entre 100 para obtener el monto real,
#Esto debido a cómo Culqi recibe los datos de los pagos
current_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
shipping_address1 = request.user.profile.shipping_address1
shipping_address2 = request.user.profile.shipping_address2
shipping_department = request.user.profile.shipping_department
shipping_province = request.user.profile.shipping_province
shipping_district = request.user.profile.shipping_district
order = Order.objects.create(
token = charge['id'],
total =transaction_amount,
email= email, #Using email entered in Culqi module, NOT user.email. Could be diff.
last_four = last_four,
created = current_time,
shipping_address1 = shipping_address1,
shipping_address2 = shipping_address2,
shipping_department = shipping_department,
shipping_province = shipping_province,
shipping_district = shipping_district
)
order.save()
return HttpResponse("Hi")
Oder model:
class Order(models.Model):
token = models.CharField(max_length=100, blank=True, null=True)
total = models.DecimalField(max_digits=10, decimal_places=2)
email = models.EmailField(max_length=250, blank = True, verbose_name= 'Correo electrónico')
last_four = models.CharField(max_length=100, blank=True, null=True)
created = models.DateTimeField(auto_now_add=True)
shipping_address1 = models.CharField(max_length=100, blank=True, null=True)
shipping_address2 = models.CharField(max_length=100, blank=True, null=True)
shipping_department = models.CharField(max_length=100, blank=True, null=True)
shipping_province = models.CharField(max_length=100, blank=True, null=True)
shipping_district = models.CharField(max_length=100, blank=True, null=True)
reason = models.CharField(max_length=400, blank=True, null=True, default='')
class Meta:
db_table = 'Order'
ordering = ['-created']
def __str__(self):
return "Order by: " + str(self.id) + " for " + str(self.total)
You don't need to provide current date when creating model object, because you already set option 'auto_now_add=True', which will set date automatically when you create a model object, for example:
order = Order.objects.create(
token = charge['id'],
total =transaction_amount,
email= email, #Using email entered in Culqi module, NOT user.email. Could be diff.
last_four = last_four,
shipping_address1 = shipping_address1,
shipping_address2 = shipping_address2,
shipping_department = shipping_department,
shipping_province = shipping_province,
shipping_district = shipping_district
)
order.save()
print(order.created) # return current time on in server's timezone
Related
I am working on a Django Project where I want to collect payment in Dollars from Applicants on the portal, and I don't know how to go about it. Though I have been following an online tutorial that shows how to do it but the result I am having is different with the recent error which says 'module' object is not callable.
Remember that I have tested my configured environment and also imported it into my views on top of the page.
Profile model code:
class Profile(models.Model):
applicant = models.OneToOneField(User, on_delete=models.CASCADE, null = True)
surname = models.CharField(max_length=10, null=True)
othernames = models.CharField(max_length=30, null=True)
gender = models.CharField(max_length=6, choices=GENDER, blank=True, null=True)
nation = models.CharField(max_length=255, choices=NATION, blank=True, null=True)
state = models.CharField(max_length=20, null=True)
address = models.CharField(max_length=200, null=True)
phone = models.CharField(max_length=16, null=True)
image = models.ImageField(default='avatar.jpg', upload_to ='profile_images')
def __str__(self):
return f'{self.applicant.username}-Profile'
Education/Referee Model code:
class Education(models.Model):
applicant = models.OneToOneField(User, on_delete=models.CASCADE, null = True)
qualification = models.CharField(max_length=60, choices=INSTITUTE, default=None, null=True)
instition = models.CharField(max_length=40, null=True)
reasons = models.CharField(max_length=100, null=True)
matnumber = models.CharField(max_length=255, null=True)
reference = models.CharField(max_length=100, null=True)
refphone = models.CharField(max_length=100, null=True)
last_updated = models.DateTimeField(auto_now_add=False, auto_now=True)
def __str__(self):
return f'{self.applicant}-Education'
Submitted Model code:
class Submitted(models.Model):
applicant = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
application = models.UUIDField(primary_key = True, editable = False, default=uuid.uuid4)
confirm = models.BooleanField()
approved = models.CharField(max_length=20, null=True)
date = models.DateTimeField(auto_now_add=True)
def save(self, *args, **kwargs):
self.application == str(uuid.uuid4())
super().save(*args, **kwargs)
def __unicode__(self):
return self.applicant
def __str__(self):
return f'Application Number: {self.application}-{self.applicant}'
Scholarship Model code:
class Scholarship(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null = True)
name = models.CharField(max_length=100, null = True)
description = models.CharField(max_length=200, null = True)
category = models.CharField(max_length=60, choices=INSTITUTE, default=None, null=True)
amount = models.FloatField()
date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f'WASU Scholarship: {self.name}-{self.name}'
My View for printing slip:
def AppSlip(request):
check_submited = Submitted.objects.get(applicant=request.user)
check_education = Education.objects.get(applicant = request.user)
candidate_edu = check_education.qualification
scholarship = Scholarship.objects.get(category=candidate_edu)
context = {
'candidate_edu':candidate_edu,
'scholarship':scholarship,
}
return render(request, 'user/slip.html', context)
My view for applicant to fill form for payment which I want their Profile captured automatically in the form:
def scholarship_detail(request, pk):
data = Scholarship.objects.get(id=pk)
if request.method=='POST':
form = PaymentForm(request.POST)
if form.is_valid():
user = Profile.objects.get(applicant=request.user)
name= user.surname
email = form.cleaned_data['email']
amount = form.cleaned_data['amount']
phone = form.cleaned_data['phone']
context = {'applicant':name, 'email':email, 'amount':amount, 'phone':phone}
return process_payment(request, context)
else:
form = PaymentForm()
ctx={
'form':form,
'product':data,
}
return render(request, 'user/scholarship.html', ctx)
My form code for Payment: How can query logged in user profile and fill into name, email, phone, amount from Scholarship Model into amount form filled.
class PaymentForm(forms.Form):
name = forms.CharField(label='Your name', max_length=100)
email = forms.EmailField()
phone=forms.CharField(max_length=15)
amount = forms.FloatField()
View code for processing Payment (Where I am suspecting the error). Though I have configured my env using django-dotenv with the Flutterwave Secret Key in it.
#login_required(login_url='user-login')
def process_payment(request, newContext={}):
auth_token= dotenv('SECRET_KEY')
hed = {'Authorization': 'Bearer ' + auth_token}
data = {
"tx_ref":''+str(math.floor(1000000 + random.random()*9000000)),
"amount":amount,
"currency":"KES",
"redirect_url":"http://localhost:8000/callback",
"payment_options":"card",
"meta":{
"consumer_id":23,
"consumer_mac":"92a3-912ba-1192a"
},
"customer":{
"email":email,
"phonenumber":phone,
"name":name
},
"customizations":{
"title":"WASU Scholarship 2022",
"description":"Best store in town",
"logo":"https://getbootstrap.com/docs/4.0/assets/brand/bootstrap-solid.svg"
}
}
url = ' https://api.flutterwave.com/v3/payments'
response = requests.post(url, json=data, headers=hed)
response=response.json()
link=response['data']['link']
return link
My payment Response View:
#require_http_methods(['GET', 'POST'])
def payment_response(request):
status=request.GET.get('status', None)
tx_ref=request.GET.get('tx_ref', None)
print(status)
print(tx_ref)
return HttpResponse('Finished')
Anticipating your prompt answers. Thanks
I want to make an attendance system that prevents each user to see the attendance that some users have taken. But in my case, the attendance that the other users take can be shown by every user. How can I prevent this part? These are my codes: Models.py
class ClassAttendance(models.Model):
Faculty_Name = models.CharField(max_length=200, null=True, blank=True)
Student_ID = models.CharField(max_length=200, null=True, blank=True)
firstname = models.CharField(max_length=200, null=True, blank=True)
lastname = models.CharField(max_length=200, null=True, blank=True)
date = models.DateField(auto_now_add = True, null = True)
time = models.TimeField(auto_now_add=True, null = True)
college = models.CharField(max_length=200, null=True, blank=True)
course = models.CharField(max_length=200, null=True, blank=True)
year = models.CharField(max_length=200, null = True)
section = models.CharField(max_length=200, null = True)
subject = models.CharField(max_length=200, null = True)
status = models.CharField(max_length=200, null = True, default='Absent')
def __str__(self):
return str(self.Student_ID + "_" + str(self.lastname) + "_" + str(self.date)+ "_" + str(self.subject))
class Faculty(models.Model):
user = models.OneToOneField(User, null = True, blank = True, on_delete= models.CASCADE)
firstname = models.CharField(max_length=200, null=True, blank=True)
lastname = models.CharField(max_length=200, null=True, blank=True)
college = models.CharField(max_length=200, null=True, choices=COLLEGE)
course = models.CharField(max_length=200, null=True, choices=COURSE)
year = models.CharField(max_length=200, null=True, choices=YEAR)
section = models.CharField(max_length=200, null=True, choices=SECTION)
subject = models.CharField(max_length=200, null=True, choices=SUBJECT)
def str(self):
return str(self.firstname + " " + self.lastname)
class Meta:
verbose_name_plural = "Faculties"
views.py
#login_required(login_url = 'login')
def takeClassAttendance(request):
if request.method == 'POST':
details = {
'college':request.POST['college'],
'course':request.POST['course'],
'year': request.POST['year'],
'section':request.POST['section'],
'subject':request.POST['subject'],
'faculty':request.user.faculty
}
if ClassAttendance.objects.filter(date = str(date.today()), college = details['college'], course = details['course'], year = details['year'], section = details['section'],subject = details['subject']).count() != 0 :
messages.error(request, "Attendance already recorded.")
return redirect('home')
else:
students = Student.objects.filter(college = details['college'], course = details['course'], year = details['year'], section = details['section'])
names = Recognizer(details)
for student in students:
if str(student.student_id) in names:
classattendance = ClassAttendance(Faculty_Name = request.user.faculty,
Student_ID = str(student.student_id),
lastname = str(student.lastname),
firstname = str(student.firstname),
college = details['college'],
course = details['course'],
year = details['year'],
section = details['section'],
subject = details['subject'],
status = 'Present')
classattendance.save()
else:
classattendance = ClassAttendance(Faculty_Name = request.user.faculty,
Student_ID = str(student.student_id),
lastname = str(student.lastname),
firstname = str(student.firstname),
college = details['college'],
course = details['course'],
year = details['year'],
section = details['section'],
subject = details['subject'],)
classattendance.save()
classattendances = ClassAttendance.objects.filter(date = str(date.today()),college = details['college'], course = details['course'], year = details['year'], section = details['section'],subject = details['subject'])
context = {"attendances":classattendances, "ta":True}
messages.success(request, "Attendance taking success")
return render(request, 'attendance_sys/attendance.html', context)
context = {}
return render(request, 'attendance_sys/home.html', context)
def facultyProfile(request):
faculty = request.user.faculty
form = FacultyForm(instance = faculty)
context = {'form':form}
return render(request, 'attendance_sys/facultyForm.html', context)
I expected that after the users take attendance, they are the ones that can see that information
You can filter the returned objects by request.user.faculty.
I have the following two models ASPBoookings and Athlete. The Athlete model is linked to the ASPBookings model by the foreign key named athlete.
I am trying to create a loop that will cycle through all of the bookings in the ASPBooking table and find out which is the most recent booking by each athlete (the table can contain multiple bookings each to the same or different athletes (athlete_id).
Once I have this information (booking_date and athlete_id), I then want to be able to automatically update the "Lastest ASP Session Field" in the Athlete Model.
This is what I have tried so far. I can cycle through the bookings in the ASPBookings table and retrieve and update the "Latest ASP Session Field" using the booking_date and athlete_id, but I cannot do this for multiple different athletes that are within the table. Currently the view just identifies the latest booking and the assigned athlete_id and then updates the field.
Thanks in advance for any help.
Below is the code:
ASPBookings Model
class ASPBookings(models.Model):
asp_booking_ref = models.CharField(max_length=10, default=1)
program_type = models.CharField(max_length=120, default='asp')
booking_date = models.DateField()
booking_time = models.CharField(max_length=10, choices=booking_times)
duration = models.CharField(max_length=10, choices=durations, default='0.5')
street = models.CharField(max_length=120)
suburb = models.CharField(max_length=120)
region = models.CharField(max_length=120, choices=regions, default='Metro')
post_code = models.CharField(max_length=40)
organisation_type = models.CharField(max_length=120,choices=organisation_types, default='Government School')
audience_number = models.CharField(max_length=10)
presentation_form = models.CharField(max_length=120, choices=presentation_form_options, default='Face to Face')
contact_name = models.CharField(max_length=80)
email = models.EmailField()
phone_number = models.CharField(max_length=120)
comments = models.TextField()
status = models.CharField(max_length=80, choices=statuses, default='TBC')
email_sent = models.BooleanField(default=False)
athlete = models.ForeignKey(Athlete, default= '1', on_delete=models.CASCADE)
def __str__(self):
return self.contact_name
# return URL after the POST has been submitted.
def get_absolute_url(self):
return reverse('vistours:success')
Athlete Model
class Athlete(models.Model):
athlete_ref = models.CharField(max_length=10, default=1)
athlete_name = models.CharField(max_length=80)
email = models.EmailField()
phone_number = models.CharField(max_length=120)
home = models.CharField(max_length=120)
education = models.CharField(max_length=120)
sport = models.CharField(max_length=120, choices=sports, default='1500m Runner')
notes = models.TextField(default='None')
gender = models.CharField(max_length=120, choices=genders, default='Not Specified')
para_athlete = models.BooleanField(blank=True)
working_with_children = models.BooleanField(blank=True)
expiry_date = models.DateField(blank=True, null=True)
available = models.BooleanField(blank=True)
available_from = models.DateField(blank=True, null=True)
bfbw = models.BooleanField(blank=True)
latest_bfbw_session = models.DateField(blank=True, null=True)
number_bfbw_sessions = models.CharField(blank=True, null=True, max_length=10)
asp = models.BooleanField(blank=True)
latest_asp_session = models.DateField(blank=True, null=True)
number_asp_sessions = models.CharField(blank=True, null=True, max_length=10)
tours = models.BooleanField(blank=True)
latest_tours_session = models.DateField(blank=True, null=True)
number_tours_sessions = models.CharField(blank=True, null=True, max_length=10)
def __str__(self):
return self.athlete_name
# return URL after the POST has been submitted.
def get_absolute_url(self):
return reverse('home')
View
# Complete first loop for inital values.
for date in asp_data:
if date.booking_date != None:
first_loop = date.booking_date
athl_id = date.athlete_id
break
# If next value is greater than inital value, replace current values.
for date in asp_data:
if date.booking_date != None:
if date.booking_date > first_loop:
first_loop = date.booking_date
athl_id = date.athlete_id
print(first_loop)
print(athl_id)
update_date = Athlete.objects.get(id=athl_id)
update_date.latest_asp_session = first_loop
update_date.save()
No need for loops. You can do this for all athletes in just one go using subqueries to leave all the heavy-lifting to your database:
from django.db.models import F, OuterRef, Subquery
bookings = ASPBookings.objects.filter(
athlete=OuterRef('pk')
).order_by('-booking_date')
Athlete.objects.annotate(
latest_asp_booking_date=Subquery(
bookings.values('booking_date')[:1]
)
).update(
latest_asp_session=F('latest_asp_booking_date')
)
Currently have a problem where I will assign a both Client foreignkey to a Signin and its not saving either reference. Heres the Signin Model
#Sign-ins require just a user
class Signin(models.Model):
employee = models.ForeignKey(Employee)
sign_in_time = models.DateTimeField(null=True)
sign_out_time = models.DateTimeField(null=True)
sign_in_number = models.CharField(max_length=15, default="00000000000")
sign_out_number = models.CharField(max_length=15, default="00000000000")
client_in_obj = models.ForeignKey(Clients, related_name="client_in", null=True)
client_out_obj = models.ForeignKey(Clients, related_name="client_out", null=True)
client_in = models.CharField(max_length=100, default="Unkown")
client_out = models.CharField(max_length=100, null=True)
# Start of Sign in Function
def save(self, *args, **kwargs):
self.client_out_obj.save()
self.client_in_obj.save()
super(Signin, self).save(*args, **kwargs)
def __str__(self):
return self.employee.first_name + " " + self.employee.last_name + " : " + str(self.sign_in_time)+ " to " + str(self.sign_out_time)
Now the employee field IS saving, but the two client_in_obj,and client_out_obj are NOT saving. I will assign them and then when refreshing the page they are not set.
Here is the client Model
class Clients(models.Model):
name = models.CharField(max_length=40, blank=True, null=True)
alt_name = models.CharField(max_length=25, blank=True, null=True)
address1 = models.CharField(max_length=35, blank=True, primary_key=True)
address2 = models.CharField(max_length=35, blank=True, null=True)
rate = models.FloatField(blank=True, null=True)
contact_email = models.CharField(max_length=40, blank=True, null=True)
contact_name = models.CharField(max_length=30, blank=True, null=True)
phone = models.CharField(max_length=40, blank=True, null=True)
billing_name = models.CharField(max_length=30, blank=True, null=True)
billing_email_field = models.CharField(db_column='billing_email_', max_length=12, blank=True, null=True) # Field renamed because it ended with '_'.
def __str__(self):
return self.name + ", " + self.address1
class Meta:
managed = False
db_table = 'clients'
They client_in and client_out fields were my hackey way of trying to get by it for now.
My relations I would like to keep are as followed - An employee will have Many Sign ins, and each Sign in only needs an employee at first, each other field will be filled in over the course of a day.
I found the answer!
So the problem was followed. Im dealing with a legacy database in mySQL, the problem was that when django was assigning the client id it was thinking the primary key was an integer field, so when it tried assigning the key (which was a varchar) it ran into an integer field, so it didn't like it and didn't save. Hopefully that helps anyone who runs into this problem later!
These are the models involved in the view :
class Movie(models.Model):
title_original = models.CharField(max_length=300,
db_column='titulooriginal')
title_translated = models.CharField(max_length=300, blank=True,
db_column='titulotraducido')
thumbnail = models.CharField(max_length=300, blank=True,
db_column='imagen')
length = models.CharField(max_length=75, db_column='duracion')
rating = models.CharField(max_length=75, db_column='censura')
genre = models.CharField(max_length=75, db_column='genero')
country_of_origin = models.CharField(max_length=150, db_column='pais')
trailer = models.CharField(max_length=300, blank=True, db_column='trailer')
synopsis = models.CharField(max_length=3600, blank=True,
db_column='sinopsis')
cast = models.CharField(max_length=300, blank=True, db_column='elenco')
director = models.CharField(max_length=150, db_column='director')
slug = models.SlugField(unique=True, blank=True)
tsc = models.DateTimeField(auto_now=False, auto_now_add=True, db_column='tsc', null=True)
tsm = models.DateTimeField(auto_now=True, auto_now_add=True, db_column='tsm', null=True)
class Meta:
db_table = u'peliculas'
def __unicode__(self):
return self.title_original
class ShowTime(models.Model):
LANGUAGE_ESPANOL = 'Espanol'
LANGUAGE_SUBTITLED = 'Subtitulada'
LANGUAGE_CHOICES = (
(LANGUAGE_ESPANOL, LANGUAGE_ESPANOL),
(LANGUAGE_SUBTITLED, LANGUAGE_SUBTITLED))
movie = models.ForeignKey(Movie, db_column='idpelicula')
theater = models.ForeignKey(Theater, db_column='idcine',
null=True)
time = models.TimeField(null=True, db_column='hora')
type_3d = models.NullBooleanField(db_column=u'3d')
type_xd = models.NullBooleanField(null=True, db_column='xd')
type_gtmax = models.NullBooleanField(null=True, db_column='gtmax')
type_vip = models.NullBooleanField(null=True, db_column='vip')
type_imax = models.NullBooleanField(null=True, db_column='imax')
language = models.CharField(max_length=33, blank=True, db_column='idioma',
choices=LANGUAGE_CHOICES,
default=LANGUAGE_ESPANOL)
city = models.ForeignKey(City, db_column='idciudad')
start_date = models.DateField(blank=True, db_column='fecha_inicio')
end_date = models.DateField(blank=True, db_column='fecha_fin')
visible = models.NullBooleanField(null=True, db_column='visible')
tsc = models.DateTimeField(auto_now=False, auto_now_add=True, db_column='tsc', null=True)
tsm = models.DateTimeField(auto_now=True, auto_now_add=True, db_column='tsm', null=True)
class Meta:
db_table = u'funciones'
def __unicode__(self):
return (unicode(self.theater) + " " + unicode(self.movie.title_original) + " " + unicode(self.time))
Additionally this is the view that queries the models:
def list_movies(request, time_period):
city = utils.get_city_from_request(request)
if time_period == 'proximas-dos-horas':
(start_date,
end_date,
hours_start,
hours_end) = utils.get_datetime_filters_from_time_period(time_period)
else:
(start_date,
end_date) = utils.get_datetime_filters_from_time_period(time_period)
if not start_date: # 404 for invalid time periods
raise Http404
movies = Movie.objects.filter(
showtime__city=city.code,
showtime__visible=1,
).order_by('-tsm').distinct()
if time_period == 'proximas-dos-horas':
movies = movies.filter(
showtime__start_date__lte=start_date,
showtime__end_date__gte=end_date,
showtime__time__range=(hours_start, hours_end))
elif time_period == 'proximos-estrenos':
movies = movies.filter(
showtime__start_date=None,
showtime__end_date=None,
)
else:
movies = movies.filter(
showtime__start_date__lte=start_date,
showtime__end_date__gte=end_date)
context = {'movies': movies, 'city': city, 'time_period': time_period}
return render_to_response('list_movies.html', context,
context_instance=RequestContext(request))
Currently this view is consuming a lot of resources in the database and this results in low response time of the web transaction on the browser. I connected the app with New Relic monitoring software to analyze the transactions on app and DB level. This is the trace detail that i got:
I would like to get somo advice on optimizing this view so that the consumption of DB resources drops to the minimum
Thanks a lot!
I don't know the context of your problem, but my 2 advises are:
First, consider some changes in your data model. The ShowTime class have a foreign key to City and a foreign key to Theater and it seems a bit redundant. Personally, I prefer de-normalize addresses, for example:
class Theater(models.Model):
name = models.CharField(max_length=48)
# Location fields.
geo_country = models.CharField(max_length=48)
geo_country_code = models.CharField(max_length=2)
geo_locality = models.CharField(max_length=48)
geo_street = models.CharField(max_length=48, blank=True, default="")
geo_address = models.CharField(max_length=120, blank=True, default="")
class ShowTime(models.Model):
theater = models.ForeignKey(Theater)
This way, you can save a JOIN to the City table and it can be erased from your data model.
Second, read the select_related feature of the Django ORM.
Wish you fun.