I'm working on a project using Python(3.7) and Django(2.5) in which I'm building an application something like a freelancing site, but I'm stuck at one point while implementing the delivery submission part.
A user will create a service to sell and then a buyer will order his service, after that the seller has to be done the agreed job and need to submit the work to the buyer as a delivery.
The delivery will be in the form of a file, can be a text file, image file, audio, video or a code file, the problem is that I don't know how I can implement this thing in Django, so a user can send a file to another user in a private manner, so only both of these users will be able to access that file.
Here's what I have so far, for order between buyer and seller:
class Order(models.Model):
status_choices = (
('Active', 'Active'),
('Completed', 'Completed'),
('Late', 'Late'),
('Short', 'Short'),
('Canceled', 'Canceled'),
('Submitted', 'Submitted')
)
gig = models.ForeignKey('Gig', on_delete=models.CASCADE)
seller = models.ForeignKey(User, on_delete=models.CASCADE, related_name='selling')
buyer = models.ForeignKey(User, on_delete=models.CASCADE, related_name='buying')
created_at = models.DateTimeField(auto_now=timezone.now())
charge_id = models.CharField(max_length=234)
days = models.IntegerField(blank=False)
status = models.CharField(max_length=255, choices=status_choices)
def __str__(self):
return f'{self.buyer} order from {self.seller}'
Any idea to implement the file sharing as delivery between two authenticated users?
Thanks in advance!
There is a lot of ways you could implement this.
You can simply add a field for users who access to file and let them download the file whenever they asked for download if they are authenticated and they are allowed to download that specific file.
With your model you can do something like this:
Giving gig field is your file you can create a slug or basically any link for a file and when user clicked on it you can get all the orders for that user, then check for the files that the access has been granted with these orders and if the file that users requests to download is one of them, simply let him download it.
You can let a user download a file using X-Sendfile which helps you check if users are allowed to download file or not.
Example here on stackoverflow
A sample code:
def download_file_view(request, file_id):
if not request.user:
# Redirect user or ...
if not request.user.is_authenticated:
# Redirect user or ...
# Check if this user has any orders with that file linked to it:
if Order.objects.filter(buyer=request.user, gig__pk=file_id).exists():
# User has bought this file already and the download should be allowed.
You can check for expiration date and all sorts of things there.
Related
I have a built-in User table and a Note table associated with it by key.
class Note(models.Model):
header = models.CharField(max_length=100)
note_text = models.TextField()
data = models.DateField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
That is, a registered user may have several notes. How do I get all these notes from this particular user (who has now visited his page)? I need to get these notes in views.py.
I tried different ways, don't know how to do that.
Hope this answer finds you well ...
First, you already have a user object in your request. But, still you want to filter out notes for other users too, then do
get_user = User.objects.get(id=id) # For any random user
get_user = request.user # For authenticated user
After getting the user, you simply filter it with the Note model like this ...
get_notes = Note.objects.filter(user=get_user)
You are good to go!
In my project (small online shop) I need to split registration for users and customers.
So the information what I found when somebody registered in django then his account stored in one table, in this table I can see admin user and staff and another registered accounts, and I can sse them all in admin on Users page. But I do not want ot put all accounts in one "basket". I need split them fro different tables.
For example superuser can create in admin area a new user (content manager) and provide him access/permission to manage admin area (create product etc.) - this users and super user will be on default User page. On the page Customers will be displaying only users who registered for example via https://mysite/account/register page, after registration this customer account I can see in Customers page in the admin area but not in Users page. And this customer can login to his account for example via https://mysite/account/login
Is this possible?
As Jay said, everyone registered in the database is still a User whatever their role may be (admin, superuser, customer). What you could do is create a Profile model where everyone will have their information such as telephone, location etc, and you will also add another field clarifying their property.
PACKAGES = [
('customer', 'Customer'),
('support', 'Support'),
('admin', 'Admin'),
]
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
image = models.ImageField(default='user_avatar.png', upload_to='...')
last_visit = models.DateField(default=timezone.now, blank=True)
location = models.CharField(max_length=254, null=True, blank=True)
contact_phone = models.CharField(max_length=15)
user_role = models.CharField(default="customer", choices=PACKAGES, max_length=20)
Then all you need to do is edit your admin.py to implement a search parameter there:
class ProfileAdmin(admin.ModelAdmin):
list_filter=('user_role',)
admin.site.register(Profile, ProfileAdmin)
Doing that will give you a filter_list in the right corner of your admin page but that is for admin page only.
If you want to access different roles in your views or your templates you will do so by getting the user_role you need:
customers = Profile.objects.filter(user_role='customer')
I am trying to implement a notification page on my website in Django. But I have a problem that how can I send the same notification to multiple users.
let suppose I have to send it to just one user.
then I can create a model of notification with
noti = models.TextField()
user= models.foreignkey
is_seen = models.bool
so this is just a sample
but the the problem is this how i can send this notification to selected multiple users
one important is that is_seen is compulsory for each user
I hope you will understand
I think it depends how this data will grow in future. Because if all of your notifications go to multiple users, you can have a Notification model with ManyToMany relation with User. Something like following
class Notification(models.Model):
message = models.TextField()
users = models.ManyToMany(User, through='UserNotification')
class UserNotification(model.Model):
user = models.ForeignKey(User)
notification = models.ForeignKey(Notification)
is_seen = models.BooleanField()
But other question is if you want to build something like the one you shared, that is also fine. I don't think so there's an issue of adding same message for multiple users. This is more flexible in my opinion as compared to M2M relation. You can extend this in future easily as your application will grow.
class Notification(model.Model):
message = models.TextField()
user = models.ForeignKey(User)
is_seen = models.BooleanField)
Another case can be if you want to have more relations and if there's need of adding more information related to notification. This gives more room for improvement in future in terms of storing information.
class Notification(model.Model):
message = models.TextField()
class UserNotification(model.Model):
user = models.ForeignKey(User)
notification = models.ForeignKey(Notification)
is_seen = models.BooleanField()
I'm creating a project using Django and I'm wondering how I can make objects distinct. Now I am creating message box part and what I want to do is show up user names who the user have been contacting. My current way is like this
models.py
class Message(models.Model):
''''''
text = models.TextField()
created = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='myself')
someone = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='someone')
views.py
def inbox(request):
'''See messages history'''
messages = Message.objects.filter(user=request.user).distinct('someone')
notification = Message.objects.filter(someone=request.user).distinct('user')
return render(request, 'campus_feed/inbox.html', {'messages': messages})
So what I want to do is list the users who is contacting me or I contacted and I don't want to show up the same name multiple times but I cannot come up with good way to filter them. Anyone who can give me tips?
If you want users, you should request users.
User.objects.filter(Q(myself__someone=request.user) | Q(someone__user=request.user))
This gives you all users who are either the "someone" in a message from the current user, or the "user" in a message to the current user. Since you are querying users directly, the is no possibility of duplicates.
I'm at a loss. I've searched most of the links on Google/YouTube and can't seem to figure out how to create a simple session-based cart in Django!
My website is NOT an e-commerce store. I have a Post model which users can review via a Comment model. They review the Post's overall quality, overall difficulty, and workload based on a 1-5 rating system.
All that I want is for users to be able to go to the Post page, press "Add to Cart" and be redirected to a "cart.html" page where they see the Post they just added, as well as that Post's ratings (Quality, Difficulty, Workload). These ratings come from my Comment model, which is 100% based on User input.
I need this cart to exist in the session! It is very important that anonymous users have access to this feature.
here's a brief snippet of my models.py
class Post(models.Model):
[...]
title = models.CharField(max_length=250)
# rest of code
class Comment(models.Model):
[...]
post = models.ForeignKey(Post, related_name="comments")
user = models.ForeignKey(User, related_name="usernamee")
[...]
overall_rating = models.FloatField(choices=overall_rating_choices,
default=None)
difficulty_rating = models.FloatField(choices=difficulty_rating_choices, default=None)
workload_rating = models.FloatField(choices=workload_rating_choices,
default=None)
# rest of code
I don't need a checkout page, I don't need any payment processing, I don't need any price calculations for any of my posts and I don't need quantities at all. Each post should only be added once and that's it.
This is a special use case and I'm afraid my Django skills aren't up to par to tackle this yet.
I would ask if anybody can give me some sort of guidance on this issue?
P.S. I have been doing Django for 2 months now. I'd say my skill level is at about a 2/10.