def recv_method(self, method, params, id_, randomSeed=None): """DDP method handler.""" if randomSeed is not None: this.random_streams.random_seed = randomSeed this.alea_random = alea.Alea(randomSeed) API.method(method, params, id_) self.reply('updated', methods=[id_])
def do_logout(self): """Logout a user.""" # silent unsubscription (sans sub/nosub msg) from LoggedInUser pub API.do_unsub(this.user_sub_id, silent=True) del this.user_sub_id self.update_subs(None) user_logged_out.send( sender=self.user_model, request=this.request, user=this.user, ) this.user_id = None this.user_ddp_id = None
def do_login(self, user): """Login a user.""" this.user_id = user.pk this.user_ddp_id = get_meteor_id(user) # silent subscription (sans sub/nosub msg) to LoggedInUser pub this.user_sub_id = meteor_random_id() API.do_sub(this.user_sub_id, 'LoggedInUser', silent=True) self.update_subs(user.pk) user_logged_in.send( sender=user.__class__, request=this.request, user=user, )
def update_subs(new_user_id): """Update subs to send added/removed for collections with user_rel.""" for sub in Subscription.objects.filter(connection=this.ws.connection): params = loads(sub.params_ejson) pub = API.get_pub_by_name(sub.publication) # calculate the querysets prior to update pre = collections.OrderedDict([ (col, query) for col, query in API.sub_unique_objects(sub, params, pub) ]) # save the subscription with the updated user_id sub.user_id = new_user_id sub.save() # calculate the querysets after the update post = collections.OrderedDict([ (col, query) for col, query in API.sub_unique_objects(sub, params, pub) ]) # first pass, send `added` for objs unique to `post` for col_post, query in post.items(): try: qs_pre = pre[col_post] query = query.exclude( pk__in=qs_pre.order_by().values('pk'), ) except KeyError: # collection not included pre-auth, everything is added. pass for obj in query: this.ws.send(col_post.obj_change_as_msg(obj, ADDED)) # second pass, send `removed` for objs unique to `pre` for col_pre, query in pre.items(): try: qs_post = post[col_pre] query = query.exclude( pk__in=qs_post.order_by().values('pk'), ) except KeyError: # collection not included post-auth, everything is removed. pass for obj in query: this.ws.send(col_pre.obj_change_as_msg(obj, REMOVED))
from __future__ import absolute_import, unicode_literals from dddp.api import API, Collection, Publication from django_todos import models class Task(Collection): model = models.Task class Tasks(Publication): queries = [ models.Task.objects.all(), ] API.register([ Task, Tasks, ])
from __future__ import absolute_import, unicode_literals from dddp.api import API, Collection, Publication from django_ddp_app import models class Notification(Collection): model = models.Notification class Notifications(Publication): queries = [ models.Notification.objects.all().reverse(), ] API.register([ Notification, Notifications, ]);
from dddp import THREAD_LOCAL as this from dddp.api import API, Publication from dddp.logging import LOGS_NAME from django.contrib import auth class Logs(Publication): name = LOGS_NAME users = auth.get_user_model() def get_queries(self): user_pk = getattr(this, 'user_id', False) if user_pk: if self.users.objects.filter( pk=user_pk, is_active=True, is_superuser=True, ).exists(): return [] raise ValueError('User not permitted.') API.register([Logs])
days_valid = HASH_DAYS_VALID[HashPurpose.PASSWORD_RESET] token = get_user_token( user=user, purpose=HashPurpose.PASSWORD_RESET, days_valid=days_valid, ) forgot_password.send( sender=__name__, user=user, token=token, request=this.request, expiry_date=calc_expiry_time(days_valid), ) @api_endpoint('resetPassword') def reset_password(self, token, new_password): """Reset password using a token received in email then logs user in.""" user = self.validated_user( token, purpose=HashPurpose.PASSWORD_RESET, days_valid=HASH_DAYS_VALID[HashPurpose.PASSWORD_RESET], ) user.set_password(new_password) user.save() self.do_login(user) return {"userId": this.user_ddp_id} API.register([Users, LoginServiceConfiguration, LoggedInUser, Auth])
from dddp import THREAD_LOCAL as this from dddp.api import API, Publication from django.contrib import auth class Logs(Publication): users = auth.get_user_model() def get_queries(self): user_pk = getattr(this, "user_id", False) if user_pk: if self.users.objects.filter(pk=user_pk, is_active=True, is_superuser=True).exists(): return [] raise ValueError("User not permitted.") API.register([Logs])
'room__participant__user', ] class Message(Collection): model = chat.Message user_rel = [ 'room__participant__user', ] class PublicRooms(Publication): queries = [ chat.Room.objects.filter(public=True), ] class RoomRelated(Publication): @staticmethod def get_queries(room_slug): yield chat.Room.objects.filter(slug=room_slug) yield auth.User.objects.filter(participant__room__slug=room_slug) yield chat.Participant.objects.filter(room__slug=room_slug) yield chat.Message.objects.filter(room__slug=room_slug) API.register([ Room, User, Participant, Message, PublicRooms, RoomRelated, ])
self.auth_failed() minutes_valid = HASH_MINUTES_VALID[HashPurpose.PASSWORD_RESET] token = get_user_token( user=user, purpose=HashPurpose.PASSWORD_RESET, minutes_valid=minutes_valid, ) forgot_password.send( sender=__name__, user=user, token=token, request=this.request, expiry_date=calc_expiry_time(minutes_valid), ) @api_endpoint('resetPassword') def reset_password(self, token, new_password): """Reset password using a token received in email then logs user in.""" user = self.validated_user( token, purpose=HashPurpose.PASSWORD_RESET, minutes_valid=HASH_MINUTES_VALID[HashPurpose.PASSWORD_RESET], ) user.set_password(new_password) user.save() self.do_login(user) return {"userId": this.user_ddp_id} API.register([Users, LoginServiceConfiguration, LoggedInUser, Auth])
from __future__ import absolute_import, unicode_literals from dddp.api import API, Collection, Publication from django_todos import models class Task(Collection): model = models.Task class Tasks(Publication): queries = [ models.Task.objects.all(), ] API.register([ Task, Tasks, ])
from dddp.api import API, Publication from dddp.logging import LOGS_NAME class ClientVersions(Publication): """Publication for `meteor_autoupdate_clientVersions`.""" name = 'meteor_autoupdate_clientVersions' queries = [] class Logs(Publication): name = LOGS_NAME users = auth.get_user_model() def get_queries(self): user_pk = getattr(THREAD_LOCAL, 'user_id', False) if user_pk: if self.users.objects.filter( pk=user_pk, is_active=True, is_superuser=True, ).exists(): return [] raise ValueError('User not permitted.') API.register([ClientVersions, Logs])
@api_endpoint('remove') def remove(self, params): obj = models.Todos.objects.get(aid=params['_id']) obj.delete() class publicLists(Publication): queries = [ models.List.objects.all(), ] class privateLists(Publication): queries = [] class todos(Publication): def get_queries(self, listId): return [ models.Todos.objects.filter(listId=listId) ] API.register([ Lists, Todos, publicLists, privateLists, todos, ])
from dddp.api import API,DDP, Collection, Publication from .models import Task1 class TaskCollection(Collection): model = Task1 class TaskPub(Publication): queries = [ Task1.objects.all(), ] API.register([ TaskCollection, TaskPub, ])
def recv_sub(self, id_, name, params): """DDP sub handler.""" API.sub(id_, name, *params)
def recv_unsub(self, id_=None): """DDP unsub handler.""" if id_: API.unsub(id_) else: self.reply('nosub')
from dddp.api import API, Publication from dddp.logging import LOGS_NAME class ClientVersions(Publication): """Publication for `meteor_autoupdate_clientVersions`.""" name = 'meteor_autoupdate_clientVersions' queries = [] class Logs(Publication): name = LOGS_NAME users = auth.get_user_model() def get_queries(self): user_pk = getattr(THREAD_LOCAL, 'user_id', False) if user_pk: if self.users.objects.filter( pk=user_pk, is_active=True, is_superuser=True, ).exists(): return [] raise ValueError('User not permitted.') API.register([ClientVersions, Logs])
from dddp.api import API, Collection, Publication from JON import models class Program(Collection): model = models.Program class Project(Collection): model = models.Project class Programs(Publication): queries = [ models.Program.objects.all(), ] class Projects(Publication): queries = [ models.Project.objects.all(), ] API.register([ Program, Project, Programs, Projects, ])
expiry_date = this.request.session.get_expiry_date() token = self.get_user_token( user=user, session_key=this.request.session.session_key, expiry_date=expiry_date, purpose=HashPurpose.PASSWORD_RESET, ) forgot_password.send( sender=__name__, user=user, token=token, request=this.request, expiry_date=expiry_date, ) @api_endpoint('resetPassword') def reset_password(self, token, new_password): """Reset password using a token received in email then logs user in.""" user, _ = self.validated_user_and_session( token, purpose=HashPurpose.PASSWORD_RESET, ) user.set_password(new_password) user.save() auth.login(this.request, user) self.update_subs(user.pk) return {"userId": get_meteor_id(this.request.user)}; API.register([Users, LoginPublication, Auth])