def share(request): """Share the presentation to other users""" if request.method == "POST": presentation_id = request.POST["presentation_id"] formset = formset_factory(SharePresentationForm) formset = formset(request.POST) if formset.is_valid(): # get the data from the form for form in formset: email = form.cleaned_data["email"] permission = int(form.cleaned_data["permission"]) # get user info based on the email user = User.objects.filter(email=email).first() if user is not None: # create a UserPresentation object that associates a user to a presentation uspr = UserPresentation(user_id=user.id, presentation_id=presentation_id, is_owner=0, can_edit=permission ) # save the association to the database uspr.save() # redirect to the same page return HttpResponseRedirect(request.META["HTTP_REFERER"])
def share(request, id): """Share the presentation to other users""" presentation = Presentation.objects.get(pk = id) share_formset = formset_factory(SharePresentationForm) if request.method == "POST": formset = share_formset(request.POST) if formset.is_valid(): for form in formset: user = User.objects.filter(email = form.cleaned_data["email"]).first() if user is not None: userpresentation = UserPresentation( presentation_id = presentation.id, user_id = user.id, can_edit = True if int(form.cleaned_data["permission"]) == 1 else False, is_owner = 0) userpresentation.save() # redirect to the same page return HttpResponseRedirect(request.META["HTTP_REFERER"]) else: return render_to_response("share_form.html", { "presentation": presentation, "collaborators": presentation.userpresentation_set.exclude(user__id = request.user.id), "share_formset": share_formset }, context_instance=RequestContext(request))
def create(request): """Create a new presentation""" # if data are received from the view if request.method == "POST": # get data from the form form = NewPresentationForm(request.POST) if form.is_valid(): name = form.cleaned_data["name"] description = form.cleaned_data["description"] # set default theme (id=1) theme_id = 1 # set the number of views and likes to 0 num_views = num_likes = 0 # set status=1 (active) status = 1 # set the presentation key based on a random SHA1 string key = hashlib.sha1(str(datetime.datetime.now())).hexdigest()[:10] # set public=1 (public is default) is_private = form.cleaned_data["is_private"] # create a Presentation object with its parameters presentation = Presentation(theme_id=theme_id, name=name, description=description, status=status, key=key, is_private=is_private, num_views=num_views, num_likes=num_likes ) # save the presentation to the database presentation.save() # create a UserPresentation object to associate the user with the created presentation ID userpresentation = UserPresentation(user_id=request.user.id, presentation_id=presentation.id, is_owner=1, can_edit=1, ) # save the association to the database userpresentation.save() # redirect to the edit page of the created presentation return HttpResponseRedirect("/edit/" + str(presentation.key)) form = NewPresentationForm() # redirect to home page return render_to_response("home.html", {"form":form}, context_instance=RequestContext(request))
def copy(request, id): """Copy a backup of the presentation""" # search the presentation data based on its ID p = Presentation.objects.get(pk=id) thumbnail_src = settings.MEDIA_THUMBNAILS_ROOT + "/img_" + p.key + ".png" '''delete its PK, so next time the save() method is executed, it'll save a new row to the database with a new ID''' p.pk = None # generate a presentation key, based on a random SHA1 string p.key = hashlib.sha1(str(datetime.datetime.now())).hexdigest()[:10] # generate a new name to the presentation p.name = _("text_copy_of") + " " + p.name # set the number of views and likes to 0 p.num_views = p.num_likes = 0 # save the new copied presentation to the database p.save() # Copy slides from MongoDB database conn = pymongo.Connection(settings.MONGODB_URI) db = conn[settings.MONGODB_DATABASE] slides = db.slides.find({"presentation_id": int(id)}) for i in slides: #Replace the slide _id and its presentation_id i["_id"] = ObjectId() i["presentation_id"] = p.id #Replace the components' _id for j in i["components"]: j["_id"] = str(ObjectId()) #Finally save the new slide db.slides.insert(i) # associate to userpresentation table us_pr = UserPresentation(user_id=request.user.id, presentation_id=p.id, is_owner=1, can_edit=1) # save the association us_pr.save() # copy the thumbnail # thumbnail_dst = settings.MEDIA_THUMBNAILS_ROOT + "/img_" + p.key + ".png" # shutil.copy(thumbnail_src, thumbnail_dst) # redirect to home page return HttpResponseRedirect("/home")
def associate_to_user(self, user, is_owner, can_edit): """Associates a presentation to a user Args: user (User): the user to associate is_owner (bool): True if the user is owner of the presentation, otherwise False can_edit (bool): True if the user can edit the presentation, otherwise False """ userpresentation = UserPresentation( user_id=user.id, presentation_id=self.id, is_owner=is_owner, can_edit=can_edit ) userpresentation.save()
def associate_to_user(self, user, is_owner, can_edit): """Associates a presentation to a user Args: user (User): the user to associate is_owner (bool): True if the user is owner of the presentation, otherwise False can_edit (bool): True if the user can edit the presentation, otherwise False """ userpresentation = UserPresentation(user_id = user.id, presentation_id = self.id, is_owner = is_owner, can_edit = can_edit) userpresentation.save()
def share(request, id): """Share the presentation to other users""" if request.method == "POST": presentation = Presentation.objects.get(pk = id) share_formset = formset_factory(SharePresentationForm) formset = share_formset(request.POST) if formset.is_valid(): for form in formset: user = User.objects.filter(email = form.cleaned_data["email"]).first() if user is not None: userpresentation = UserPresentation( presentation_id = presentation.id, user_id = user.id, can_edit = True if int(form.cleaned_data["permission"]) == 1 else False, is_owner = 0) userpresentation.save() # redirect to the same page return HttpResponseRedirect(request.META["HTTP_REFERER"])
def share(request, id): """Share the presentation to other users""" if request.method == "POST": presentation = Presentation.objects.get(pk=id) share_formset = formset_factory(SharePresentationForm) formset = share_formset(request.POST) if formset.is_valid(): for form in formset: user = User.objects.filter( email=form.cleaned_data["email"]).first() if user is not None: userpresentation = UserPresentation( presentation_id=presentation.id, user_id=user.id, can_edit=True if int( form.cleaned_data["permission"]) == 1 else False, is_owner=0) userpresentation.save() # redirect to the same page return HttpResponseRedirect(request.META["HTTP_REFERER"])