How to get users spent time on a task with Djanog? - python

I have Django project and several views. In one view i want to track users spent time.
How can i do that? Maybe some libraries or django built in methods?
Little bit more about view:
when user open view he can see many photoes, and then he need to choose similar and save. Then page reloads with new photoes and task is similar, find similar photoes and submit.
I need to track and store that time what each user spent on this task.
If i check users time when he open view i don't know when she close it, and also user can just open view and chill, without doing something.
How or with i can realize it?

Sorry wasn't able to comment but hope this helps:
Make use of Django sessions 'django.contrib.sessions to check the user session (start and stop)
Use JavaScript & jQuery to track the time spent and
also check mouse events/key events to make sure the user actually does something

Related

Django Block URL by Username

Excuse me if I'm not very direct about what I'm talking about or need, I'm fairly new to django/python and am still in the process of understanding things.
I am creating a webapp with Django where a user will sign up, log in, create a new entry, save it, and view/edit it. Think of it as a personal diary that should be only accessible to the user who wrote it.
I've gotten pretty far in this and am using filtering in the ListView, but someone could easily use url manipulation to find other users entries.
Such that User A posts entry 4 at site.com/entry/4 and then user B is able to type that in and see the entry.
How can I go about restricting the url to only the user who posted it?
Thanks for any help, and again sorry if I'm not giving enough information or I'm not clear on what I am talking about!

share button for own site Django

I want to implement a ''share on my profile'' button on my page but I have a hard time how to do so. When I try to find something like that (google/here) the results are "how to share something from your site on twitter/facebook". If there is a similar question please share the link I was not able to find something.
So i have a site where users have a profile where the liked content is displayed and I would like to give the user the option to share a Post on his own profile with a personal comment(like FB/twitter does).
My problem is that I don't know how to implement this into my models. If I want to save the "shared" post in a QuerySet in the UserProfile model I don't know where to save the comment for each shared post. If I make an extra table for all the shared posts with a extra form combined its a total mess since each post is saved individually and I don't know how to combine the existing image in to the form where the user writes his comment .
Can anybody tell me in which direction I have to walk? Feeling a bit lost on this one.
You don't need to make an extra table, django will do it for you, if you'll use ManyToManyField in your Post model.
See this docs https://docs.djangoproject.com/en/1.10/ref/models/fields/#manytomanyfield

Django rest framework: correctly handle incoming array of model ids

I have a question about REST design in general and specifically what the best way to implement a solution is in Django Rest Framework. Here it the situation:
Say I have an app for keeping track of albums that the user likes. In the browser, the user sees a list of albums and each one has a check box next to it. Checking the box means you like the album. At the bottom of the page is a submit button.
I want the submit button to initiate an AJAX request that sends tp my API endpoint a list of the ids (as in, the Djano model ids) of the albums that are liked by the user.
My question is, is this a standard approach for doing this sort of thing (I am new to web stuff and REST in particular). In other words, is there a better way to handle the transmission of these data than to send an array of ids like this? As a corollary, if this is an alright approach, how does one implement this in Django Rest Framework in a way which is consistent with its intended methodology.
I am keeping this question a little vague (not presenting any code for the album serializer, for example) intentionally because I am looking to learn some fundamentals, not to debug a particular piece of code.
Thanks a lot in advance!
Consider the upvote button to the left. When you click it, a request may be sent to stackoverflow.com/question/12345/upvote. It creates an "action resource" on the db, so later you can go to your user profile and check out the list of actions you took.
You can consider doing the same thing for your application. It may be a better user experience to have immediate action taken like SO, or a "batch" request like with gmail's check boxes.

Record last activity of a logged in user in Pyramid

In an application I am building in Pyramid,I have a users table with a field called 'last_activity'. This field is intented to store the timestamp of the last time a user visited a page in the system.
Since I'm still a bit new in pyramid, I don't know if pyramid has some sort of way to create a "common" view that can be executed everytime a user visits any page in the site (in order to update the 'last_activity' field in this view function). Is this possible?
If that is not possible, what would you recommend me to do this?
Thanks in advance.
You could use events:
If you want to log the event at the beginning of the request:
#subscriber(NewRequest)
def mysubscriber(event):
event.request.foo = 1
# log an event based on the route and the user
I am fairly new to pyramid, so I don't know if this is the most "pyramidic" way to accomplish this.
It looks like that perhaps you could use some sort of callback too http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/hooks.html#using-response-callbacks

What is the best practice for registering a new user in my case?

I am going to let new users register on my service. Here is the way I think it should go:
1. User enters his email in a field and clicks Register button.
2. User receives a confirmation email with a link containing a verification code.
3. User goes by that link from the email message where he sees a message that his account is activated now.
So, the main point I am to figure out how to implement is the second one. How do I better generate that code? Should I generate it when the user clicks Register button and save it in the field, say "verification_code" near the field "email" and then when he goes to the verification link, compare the values? Then, if that's fine, clear the "verification_code" field and set "user_is_active" field to "True". Or may be I don't have to keep that code in the database at all, but make a just in time verification with some algorithm? May be there are other things I should consider?
I've found it useful to put a verification code in the database and use it as you've suggested. The same field can do double duty for e.g. password reset requests.
I also use an expiry timeout field, where registrations or password resets need to be dealt with by the user in a timely fashion.
There is already a project that does exactly what you want. It's called django-registration. I suggest using this project instead of rolling your own.
If you still want to do it yourself, then look at the code for django-registration. It has really good comments and is really a perfect app to learn from.

Categories

Resources