So I have a Rails app with three base models: Entry, User and Vote. I need a helper method that will give me a random post for a user to vote on:
def next_entry
Entry.where(:receiving_challenge_id => current_voting_challenge.id).offset(rand(Entry.where(receiving_challenge_id: current_voting_challenge.id).count)).first
end
This works, it'll grab a random post every time.
A vote is owned by a user and an entry and they're related through IDs in the database.
But I don't want to give the user a post they've voted on.
How can I accomplish this?
-Get all the id's of entries
-Get all the id's of entries on which the user voted
-Substract the second one from the first one
-Take any number from that array with the sample method and pull the entry with that id from the DB:
Entry.find((Entry.all.collect(&:id) - current_user.votes.collect(&:entry_id)).sample)
First, get the entries they have voted on.
Then fetch all different entries
voted_entries = user.votes.includes(:entry).collect(&:entry)
unvoted_entries = Entry.where('id NOT IN (?)', voted_entries.collect(&:id))
Then you can call sample on unvoted_entries to get a random one.
Related
Given a Wikipedia user/editor id and a timeframe, is there a way in Python to get details about all the contributions/edits made the user/editor? I want to fetch details like page edited, action taken, bytes added/deleted in case of revision, and comments (if any). Is this possible at all?
Many thanks!
Yes, pywikibot’s User class has a .contributions() method you can use to iterate over all contributions for a user.
It returns a generator that, for each edit, yields a tuple of (pywikibot.Page, oldid, pywikibot.Timestamp, comment). You don’t get the diff, but you can retrieve the page at this point (page.getOldVersion(oldid=…)) and do the diff from the point just before.
Simple code example:
from pywikibot import Site, User
user = User(Site(), "SanMelkote")
for page, oldid, ts, comment in user.contributions():
print(Page.title(), comment)
i am new to yii2 and want to do a simple query on 2 tables according to
policy_id.
The result is according to user_id on policyread table. The policyread table holds all policies already read by the user. I am trying to show a result of all unread policies. My controllers are PolicyController and PolicyreadController. I am from old oscom 2.3.4 days and trying to learn this new framework.
i figured it out. in my sleep deprived state i was asking for the query to return where user_idis equal .. i simply called a list of all policies then compared that array result with the user_id array of completed policies. in the end array_diff didnt work because i was using a mulidimesnional array .. instead array_diff_key gave me the answer. thanx again for muhammad for being patient. im a bit new here
I have two tables, one called Company and the other called User, each user is related to one company using ForeignKey. So I can use reverse relation in Django to get all users for specific company (e.g. company.users)
In my case, I'm building ListAPIView which return multiple companies, and I'd like to return latest created user. My problem is that I don't want to use prefetch_related or select_related so it will load all the users, as we might end up having thousands of users per each company! Also I don't want to load each latest user in a separate query so we end up having tens of queries per API request!
I've tried something like this:
users_qs = models.User.objects.filter(active=True).order_by('-created')
company_qs = models.Company.objects.prefetch_related(
Prefetch('users', queryset=users_qs[:1], to_attr='user')
).order_by('-created')
In this case, prefetch_related failed as we can't set limit on the Prefetch's queryset filter (it gives this error "Cannot filter a query once a slice has been taken.")
Any ideas?
I think you are providing an object instead of a queryset Prefetch('users', queryset=users_qs[:1], to_attr='user')
I have two models: adverisement_campaign and advertisement_view (log of views). Every advertisement campaign must be displayed to the user no more frequently than once every X days. So, campaign model has field with number of days.
I need to select campaigns, which has not been displayed to user more than number of days for this campaign. And get random campaign from this list to display user.
So, I wrote query, which select campaigns, have not seen yet.
user_campaigns = AdvertShowEvent.objects.filter(user=user)
.values('advertisement_id')\
.annotate(datetime_last=Max('datetime'))\
.values_list('advertisement_id')
But now I need to filter campaigns to show user campaign, which he has not seen more than X days, specified for each company. Something like this
user_campaigns = AdvertShowEvent.objects.filter(user=user, **datetime_last__gte=asdvertisement.days_between_shows**)
.values('advertisement_id')\
.annotate(datetime_last=Max('datetime'))\
.values_list('advertisement_id')
How can I do this filtering with Django ORM?
Something like should do the trick:
user_campaigns = AdvertShowEvent.objects.filter(user=user,
**datetime_last__gte=datetime.datetime.now() - datetime.timedelta(days=days_between_shows))
.values('advertisement_id')\
.annotate(datetime_last=Max('datetime'))\
.values_list('advertisement_id')
I have a store with a list of user entries. Each time a user is added, I want to add all entries of that user to the store without removing the old ones.
I have a JSON web service that returns all entries of a user to me.
I read the docs http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.data.Store-method-load
and if I understand correctly, I should be able to do sth like:
myStore.proxy.extraParams.searchname = userName;
myStore.load({addRecords: true});
But in my json store, the previously added records are still removed from the store. Why?
Damn, I can't downvote my own post.
Evan's comment led to me putting some 50 lines of console.log() into the code.
Result: I was deleting all the records from the store before I did the partial refresh - not just those I wanted to reload.