Store time stamps to database - django

I am creating a Django web based application for freelancers ,that they would be paid for number of hours they have worked on a project and also a weekly report is to be generated for weekly work and for that i need to work with time stamps so how do i implement it?
I want to create a button of start timer and stop timer, which records the time stamps of start timer and end timer?
I have a User model which is extended to Freelancer and person who hires Freelancer. How do i implement this?
Where do i edit the model of time stamps?
I have this model
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
is_freelancer = models.BooleanField('Freelancer status', default=False)
is_hirer = models.BooleanField('Hirer status', default=False)
class Freelancer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
hourly_rate = models.PositiveIntegerField(default=10)
education = models.TextField(default='asia')
professional_bio = models.TextField(blank=True, null=True)
professional_title = models.TextField(blank=True, null=True)
location = models.CharField(max_length=10, blank=True, null=True)
Address = models.CharField(max_length=100, blank=True, null=True)
city = models.CharField(max_length=100, blank=True, null=True)
postal_code = models.PositiveIntegerField(blank=True, null=True)
country = models.CharField(max_length=100, blank=True, null=True)
class Hirer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
hourly_budget = models.PositiveIntegerField(blank=True, null=True)
project_title = models.CharField(max_length=100, default='hi')
project_description = models.TextField(blank=True, null=True)
job_category = models.CharField(max_length=100)
professional_bio = models.TextField(blank=True, null=True)
professional_title = models.TextField(blank=True, null=True)
location = models.CharField(max_length=10, blank=True, null=True)
Address = models.CharField(max_length=100, blank=True, null=True)
city = models.CharField(max_length=100, blank=True, null=True)
postal_code = models.PositiveIntegerField(blank=True, null=True)
country = models.CharField(max_length=100, blank=True, null=True)

This question is a bit broad, and I'm not really sure what it has to do with timestamps. But you might think about an "TimeWorked" model with foreign keys to both hirer and freelancer:
class TimeWorked(models.Model):
hirer = models.ForeignKey('Hirer', on_delete=models.CASCADE)
freelancer = models.ForeignKey('Freelancer', on_delete=models.CASCADE)
rate = models.IntegerField()
start_time = models.DateTimeField(auto_now_add=True)
end_time = models.DateTimeField(null=True, blank=True)
so you can create an instance of this class at the start of a block of time, then at the end fill in the end_time; then you can easily query the total amount of time worked for a job, and calculate the total to bill.
(Note you might want to rethink your models a bit, especially separating the project details from the hirer, otherwise each hirer can only ever create a single project.)

Related

Adding assigner and assignee in Django

I am fairly new to django rest framework, and I am trying to make a model which is associated with two users, one as assigner and the other as assignee. I tried adding two different foreign keys, but it throws error. Here is my code:
job = models.ForeignKey(Job,related_name='Job',on_delete=models.CASCADE, null=True)
assigner = models.ForeignKey(User, on_delete=models.PROTECT, null=True)
assignee = models.ForeignKey(User, on_delete=models.PROTECT, null=True)
unit = models.ForeignKey(Unit,related_name='UnitName',on_delete=models.CASCADE, null=True)
equipment = models.ForeignKey(Equipment,related_name='EquipmentName',on_delete=models.CASCADE, null=True)
name = models.CharField(max_length=200)
category = models.CharField(max_length=200, null=True,blank=True)
start_date = models.DateField()
end_date = models.DateField()
created_at = models.DateTimeField(auto_now_add=True,blank=True, null=True)
updated_at = models.DateTimeField(auto_now=True,blank=True, null=True)
status = models.ForeignKey(TaskStatus, related_name='Status',on_delete=models.CASCADE)
def __str__(self):
return(self.name)
Please help

Django admin TabularInline "502 Bad Gateway” nginx prematurely closed connection

I have seen lot of discussion on this but nothing has helped. Below is the error I am getting
*9 upstream prematurely closed connection while reading response header from upstream
It is happening with my Django TabularInline admin view. Everything else working absolutely fine.
It is working if I do this change in TargetTabularInline,
fields = (‘reviewed’,)
I believe the issue is because Target model has person foreign key on some other table. Which is where it is getting time out. Please advise how can I solve this?
admin.py
class TargetTabularInline(admin.TabularInline):
model = Target
class VideoAdmin(admin.ModelAdmin):
inlines = [TargetTabularInline]
list_display = ['title', 'source', 'reviewed', 'category', 'sfw']
search_fields = ('title', 'category', 'source__name', 'reviewed', )
class Meta:
model = Video
admin.site.register(Video, VideoAdmin)
models.py
class Video(DFModel):
source = models.ForeignKey(Source, models.DO_NOTHING)
creator = models.ForeignKey(
Creator, models.DO_NOTHING, blank=True, null=True)
title = models.CharField(max_length=500)
views = models.IntegerField(blank=True, null=True)
likes = models.IntegerField(blank=True, null=True)
dislikes = models.IntegerField(blank=True, null=True)
tags = models.TextField(blank=True, null=True)
upload_date = models.DateTimeField(blank=True, null=True)
page_url = models.TextField(blank=True, null=True)
video_url = models.TextField(blank=True, null=True)
thumb_url = models.TextField(blank=True, null=True)
sfw = models.BooleanField(blank=True, null=True)
category = models.CharField(max_length=20, blank=True, null=True)
reviewed = models.TextField(blank=True, null=True)
severity = models.CharField(max_length=10, blank=True, null=True)
origin = models.CharField(max_length=20, blank=True, null=True)
organization_id = models.IntegerField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
class Target(DFModel):
person = models.ForeignKey(
Person, models.DO_NOTHING, blank=True, null=True)
video = models.ForeignKey(
'Video', models.DO_NOTHING, blank=True, null=True)
reviewed = models.TextField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
class Meta:
db_table = ‘target'
Your Person model (or Creator) might have too many entries, which the admin tries to load in the drop down menu when viewing a specific entry in the admin details page dashboard, which causes too much load on the server
You could add this to the VideoAdmin class
raw_id_fields = ("creator",)
# OR
readonly_fields = ('creator',)
https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.raw_id_fields
https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.readonly_fields
This won't load the whole creator table which will reduce the load on your server,
and the workers will be free to server other requests.
Add this to your Target admin also, if there are too many entries in the Person table.

Can`t manually delete record from sqlite table, because related table with __old does not exists

I want to manually delete records from sqlite table(witch was created with Django 1.10, Table from i want to delete records called main.storage_claim, it has relation to main.storage_claimlist), but got error: Error deleting record: no such table: main.storage_claimlist__old.
I dont know why it want this table.
I have only main.storage_claimlist without __old.
Btw, i`m using DB Browser for SQLite as gui tool for working with sqlite.
Can someone explain what the heck is __old?
ClaimList model:
class ClaimList(models.Model):
""" Claim list contains one to many claim. Used for grouping claims. """
created_at = models.DateTimeField(default=datetime.now, blank=True, null=True)
from_storage = models.ForeignKey(
Storage, related_name='from_storage', blank=True, null=True, default=True)
to_storage = models.ForeignKey(
Storage, related_name='to_storage', blank=True, null=True, default=True)
status = models.CharField(max_length=255, blank=True, null=True)
separation = models.CharField(max_length=255, blank=True, null=True)
insurance_company = models.ForeignKey(
InsuranceCompany, null=True, blank=True)
patient = models.CharField(max_length=255, blank=True, null=False)
Claim model:
class Claim(models.Model):
""" Claim contain information about what need to be transefer """
medicine = models.CharField(max_length=255, blank=True, null=True)
claim_list = models.ForeignKey(ClaimList, blank=True, null=True)
requested_amount = models.DecimalField(
default=0, max_digits=5, decimal_places=0)
given_amount = models.DecimalField(
default=0, max_digits=5, decimal_places=0)
requested_unit = models.CharField(max_length=100, blank=True, null=True)
given_unit = models.CharField(max_length=100, blank=True, null=True)
from_income = models.ForeignKey(Income, blank=True, null=True)
status = models.CharField(max_length=100, blank=True, null=True)
is_will_be_accepted_in_future = models.BooleanField(default=False)
price = models.DecimalField(null=True, max_digits=10, decimal_places=5)
sum = models.DecimalField(null=True, max_digits=10, decimal_places=5)
objects = ClaimManager()

Show two tables specific column data into one list?

I have two tables Customer and Subscription in which Customer has the list of customers and Subscription contains the Customer and Packages.
In the customer list i want to show how many packages each customer has.
Subscription Model
class Subscription(models.Model):
client = models.ForeignKey(Customer, on_delete=models.SET_NULL, blank=True, null=True, related_name="subscriptions")
package = models.ForeignKey(Package, on_delete=models.SET_NULL, blank=True, null=True)
valid_start_date = models.DateTimeField()
valid_end_date = models.DateTimeField()
usage_count = models.IntegerField(null=True)
status = models.CharField(max_length=20)
transaction = models.BigIntegerField(blank=True, null=True, default=0)
created_at = models.DateTimeField()
updated_at = models.DateTimeField()
Customer Model
class Customer(AbstractBaseUser):
first_name = models.CharField(max_length=250, blank=True, null=True)
last_name = models.CharField(max_length=250, blank=True, null=True)
email = models.EmailField(unique=True, blank=True, null=True)
mobile_number = models.BigIntegerField(blank=True, null=True)
password = models.CharField(max_length=1000, blank=True, null=True)
gender = models.CharField(max_length=10, blank=True, null=True)
profile_picture = models.ImageField(upload_to="user_data/profile_picture", blank=True)
address = models.CharField(max_length=500, blank=True, null=True)
country = models.ForeignKey(Countries, on_delete=models.SET_NULL, blank=True, null=True)
state = models.ForeignKey(States, on_delete=models.SET_NULL, blank=True, null=True)
city = models.ForeignKey(Cities, on_delete=models.SET_NULL, blank=True, null=True)
pincode = models.IntegerField(blank=True, null=True)
number_of_logins = models.IntegerField(blank=True, null=True)
is_active = models.BooleanField(default=True)
created_at = models.DateTimeField(blank=True, null=True)
updated_at = models.DateTimeField(blank=True, null=True)
USERNAME_FIELD = "email"
Expected Result : I want to show the package field data from Subscription Model into the list of Customer model.
name - no. - emaiId- pakages_subbed
customer1 - mobile - email - package1,package2
customer2 - mobile - email - package4,package1
Actual Result : Only Customer field data
You need to use annotate in your queryset much as here
I think
query = Customer.objects.annotate(subscription_count=Count('subscriptions'))
ought to do it (with the count available as object.subscription_count for any object retrieved from this query)
This is just one database query, returning only Customer objects, whereas {{ customer.subscriptions.all|length }} will be a lot of them (and will probably also retrieve all the Subscription objects just to count them).
If you wanted greater access to the Subscription and Package objects you could do
Subscription.objects.all().order_by("client__id", "package__name")...
(I made up "package__name") which should get you the subscriptions grouped firstly by customer and then ordered by package name for each customer.

Django send email alerts automatically when keywords matches to his profile

Send an email alert whenever newly created profile matches to his search term. I have two module recruiter and seeker. Recruiter can posted their jobs and Seeker can posted their profile. If recruiter searches the seeker's profile based on city name, keyskills,industry type etc.. The searched result can be saved because for future use. If any new seeker's profile matches that searched result which is saved previously it automatically send email to recruiter. This alert has been send everytime when new seeker can matched that recruiter search terms.
Recruiter table
class jobs(models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length=100)
jobsummary = models.TextField()
industry = models.CharField(max_length=100)
functionalarea = models.CharField(max_length=100)
min_exp = models.IntegerField(default=0)
max_exp = models.IntegerField(default=0)
class employerkeyskills(models.Model):
user = models.ForeignKey(User)
job=models.ForeignKey(jobs, unique=False)
keyskills=models.CharField(max_length=50)
class RecSaveSearch(models.Model):
user = models.ForeignKey(User)
employer=models.ForeignKey(User,unique=False)
searchname=models.CharField(max_length=100,blank=True)
savedsearch=models.CharField(max_length=400, blank=True)
Jobseeker Profile Table
class JSPersonal(models.Model):
user = models.ForeignKey(User)
name=models.CharField(max_length=100, blank=True, null=True)
email = models.EmailField(max_length=100, blank=True, null=True)
city = models.CharField(max_length=100, blank=True, null=True)
work_expyears = models.CharField(max_length=100, blank=True, null=True)
work_expmonths = models.CharField(max_length=100, blank=True, null=True)
salaryrange = models.CharField(max_length=50, blank=True, null=True)
industry = models.CharField(max_length=100, blank=True, null=True)
class JSSkills(models.Model):
user = models.ForeignKey(User)
skill = models.CharField(max_length=100, blank=True, null=True)
version = models.CharField(max_length=100, blank=True, null=True)
lastused = models.CharField(max_length=100, blank=True, null=True)
skillyear = models.CharField(max_length=100, blank=True, null=True)
skillmon = models.CharField(max_length=100, blank=True, null=True)