def share(request, slug): """ Share view. Allows readers to send a link to a text by Email. """ text = Text.objects.get(identifier=slug) if request.method == 'POST': # If the form has been submitted... form = ShareForm(request.POST) # A form bound to the POST data if form.is_valid(): name = form.cleaned_data['name'] sender = form.cleaned_data['sender'] to = form.cleaned_data['to'] cc_myself = form.cleaned_data['cc_myself'] recipients = [to] if cc_myself: recipients.append(sender) subject = u"%s wants to share a text with you from nY" % name message = u""" %s would like to share with you the following text: """ % (name) #from django.core.mail import send_mail from django.core.mail import EmailMultiAlternatives #send_mail(subject, message, sender, recipients) html_content = u''' <a style='font-family: "American Typewriter", "Courier New"; font-weight: bold; color: black; font-size: 11em; line-height:0.8em; text-decoration: none;' href='http://test.ny-web.be/'>nY</a> <div style="font-family: Georgia; margin-top: 8px; margin-bottom: 20px; width:500px;"> <span style="font-weight: bold;">website en tijdschrift voor literatuur, kritiek & amusement, <br /> voorheen</span> <span style="font-style: italic;">yang & freespace Nieuwzuid</span> </div> <p style="font-family: Georgia; font-size: 14px;">%s would like to share with you the following text: <a href="%s">%s</a>.</p> ''' % (name, 'http://test.ny-web.be' + text.get_absolute_url(), text) msg = EmailMultiAlternatives(subject, message, sender, recipients) msg.attach_alternative(html_content, "text/html") msg.content_subtype = "html" msg.send() back2text = """ <p><a href='%s'>Return to the text.</a></p> """ % text.get_absolute_url() return HttpResponse(html_content + back2text ) # return HttpResponseRedirect('/share/thanks/') # Redirect after POST else: form = ShareForm() # An unbound form return render_to_response('share/share.html', { 'form': form, 'text': text, 'MEDIA_URL': settings.MEDIA_URL, }, context_instance=RequestContext(request))
def edit_share(request,share): if share.owner != request.user and not request.user.is_superuser: return HttpResponseForbidden('Only share owners or admins may edit a share') if request.method == 'POST': form = ShareForm(request.user,request.POST,instance=share) if form.is_valid(): share = form.save(commit=False) share.set_tags(form.cleaned_data['tags'].split(',')) return HttpResponseRedirect(reverse('list_directory',kwargs={'share':share.slug_or_id})) else: tags = ','.join([tag.name for tag in share.tags.all()]) form = ShareForm(request.user,instance=share) form.fields['tags'].initial = tags return render(request, 'share/edit_share.html', {'form': form})
def edit_share(request,share): if share.owner != request.user and not request.user.is_superuser: return HttpResponseForbidden('Only share owners or admins may edit a share') if request.method == 'POST': form = ShareForm(request.user,request.POST,instance=share) if form.is_valid(): share = form.save(commit=False) share.set_tags(form.cleaned_data['tags'].split(',')) return HttpResponseRedirect(reverse('list_directory',kwargs={'share':share.slug_or_id})) else: tags = ','.join([tag.name for tag in share.tags.all()]) form = ShareForm(request.user,instance=share) form.fields['tags'].initial = tags return render(request, 'share/edit_share.html', {'form': form})
def create_share(request): if not request.user.has_perm('bioshareX.add_share'): return render(request,'index.html', {"message": "You must have permissions to create a Share. You may request access from the <a href=\"mailto:[email protected]\">webmaster</a>."}) if request.method == 'POST': form = ShareForm(request.user,request.POST) if form.is_valid(): share = form.save(commit=False) share.owner=request.user try: share.save() share.set_tags(form.cleaned_data['tags'].split(',')) except Exception, e: share.delete() return render(request, 'share/new_share.html', {'form': form, 'error':e.message}) return HttpResponseRedirect(reverse('list_directory',kwargs={'share':share.slug_or_id}))
def add_ad(request): """ >>> a = 'a' >>> a a """ if request.method == "POST": form = ShareForm(request.POST, request.FILES) if form.is_valid(): new_ad = form.save(commit=False) new_ad.user = request.user.profile new_ad.status = "A" new_ad.save() return HttpResponseRedirect("/ads/") else: form = ShareForm() return render_to_response("share/add_ad.html", {"form": form}, context_instance=RequestContext(request))
def add_ad(request): """ >>> a = 'a' >>> a a """ if request.method == 'POST': form = ShareForm(request.POST, request.FILES) if form.is_valid(): new_ad = form.save(commit=False) new_ad.user = request.user.profile new_ad.status = 'A' new_ad.save() return HttpResponseRedirect('/ads/') else: form = ShareForm() return render_to_response('share/add_ad.html', {'form': form}, context_instance=RequestContext(request))
def create_share(request,group_id=None): if not request.user.has_perm('bioshareX.add_share'): return render(request,'index.html', {"message": "You must have permissions to create a Share. You may request access from the <a href=\"mailto:[email protected]\">webmaster</a>."}) group = None if not group_id else Group.objects.get(id=group_id) if request.method == 'POST': form = ShareForm(request.user,request.POST) if form.is_valid(): share = form.save(commit=False) if not getattr(share, 'owner',None): share.owner=request.user try: share.save() share.set_tags(form.cleaned_data['tags'].split(',')) except Exception, e: share.delete() return render(request, 'share/new_share.html', {'form': form,'group':group, 'error':e.message}) if group: assign_perm(Share.PERMISSION_VIEW,group,share) assign_perm(Share.PERMISSION_DOWNLOAD,group,share) return HttpResponseRedirect(reverse('list_directory',kwargs={'share':share.slug_or_id}))
def edit_ad(request, pk): ad = Share.objects.get(pk=pk) if request.method == 'POST': form = ShareForm(request.POST, request.FILES, instance = ad) if form.is_valid(): form.save() user = Userprofile.objects.get(user = request.user) user_adds = user.share_set.all() return render_to_response('userprofile/index.html', {'user' : user, 'user_adds': user_adds, }, context_instance=RequestContext(request)) else: form = ShareForm(instance = ad) return render_to_response( 'share/add_ad.html', {'form': form}, context_instance=RequestContext(request) )
def share(request): response = HttpResponse() user = request.user #User's manually saved tabs. try: #first of all, try to fetch the user's manually synced tabs. synced = Sync.objects.filter(user=user) #print "Synced = " + str(len(synced)) if not synced: #If it doesn't filter anything synced = None except Sync.DoesNotExist: #If this user hasn't synced anything yet. synced = None #Get user's all devices: try: users_devices = UsersDevice.objects.filter(user=user) #print "Users device = " + str(len(users_devices)) if not users_devices: users_devices = None except UsersDevice.DoesNotExist: users_devices = None #User's all synced bookmarks from all devices try: synced_bookmarks = Bookmark.objects.filter(owner=user) #print "Synced bookmarks = " + str(len(synced_bookmarks)) if not synced_bookmarks: synced_bookmarks = None except Bookmarks.DoesNotExist: synced_bookmarks = None #User's all synced history from all devices. try: synced_history = History.objects.filter(owner=user) #print "Synced history = " + str(len(synced_history)) if not synced_history: synced_history = None except History.DoesNotExist: synced_history = None #User's all synced tabs from all devices try: synced_tabs = Tab.objects.filter(owner=user) #print "Synced tabs = " + str(synced_tabs.count()) if not synced_tabs: synced_tabs = None except Tab.DoesNotExist: synced_tabs = None try: #Now just try to see if this user belongs to any group. ug = UserGroup.objects.get(user=user) groups = ug.groups.all() #ManyToManyField groups. ##print groups for g in groups: #Iterate every group #sharedObjects = g.share_set.all() #See what this group has shared. #print sharedObjects title = "" thisGroupsMembers = g.members.all() for m in thisGroupsMembers: title = title + m.email + "\r\n" #print title except UserGroup.DoesNotExist: #If this user hasn't created any group yet groups = None if (request.method == 'POST'): #User has sent the form form = ShareForm(request.POST, request=request) #The request object is passed to validate the form #print request.POST if form.is_valid(): #If the form is valid or not cd = form.cleaned_data #Cleaned data #print cd toShare = cd['toShare'] #These are the objects that are going to be shared shareBookmarks = cd['shareBookmarks'] shareHistory = cd['shareHistory'] shareTabs = cd['shareTabs'] uid = uuid.uuid4() uid = str(uid) saveShared = Share(shared_from=user,unique=uid)#Create a new entry for the share saveShared.save() for checked in toShare: #Save all the checked objects #print checked try: shared = Sync.objects.get(unique=checked) #Get it from the synced object and add it #check_if_exist(shared,request.user) saveShared.shared.add(shared) except Sync.DoesNotExist: pass for checked in shareBookmarks: try: s_b = Bookmark.objects.get(unique=checked) saveShared.shared_bookmarks.add(s_b) except Bookmarks.DoesNotExist: pass for checked in shareHistory: try: s_h = History.objects.get(unique=checked) saveShared.shared_history.add(s_h) except History.DoesNotExist: pass for checked in shareTabs: try: s_t = Tab.objects.get(unique=checked) saveShared.shared_tabs.add(s_t) except Tab.DoesNotExist: pass selectFrom = cd['selectFrom'] #What type of sharing with was selected if (selectFrom == '1'): #Share by email address: byEmail = cd['friends_email'] #Friends email saveShared.typeOf_share = 'email'#Share is by email saveShared.save() for u in byEmail: #Add all those with whom to share shareWithUser = User.objects.get(email=u) saveShared.shared_with_emails.add(shareWithUser) elif (selectFrom == '2'): #Create a group and share: byEmail = cd['friends_email'] group_name = cd['group_name'] saveShared.typeOf_share = 'group' saveShared.save() #Create the group: savedGroup = save_group(request.user,group_name,byEmail) #create the group with the povided friends email addresses saveShared.shared_with_group.add(savedGroup) elif (selectFrom == '3'): #Share with existing group: existingGroups = cd['existingGroups'] saveShared.typeOf_share = 'group' saveShared.save() for checked in existingGroups: savedGroup = MyGroup.objects.get(unique=checked) saveShared.shared_with_group.add(savedGroup) rendered = render(request, 'shareCorrect.html',context_instance = RequestContext(request)) return rendered else: # What has this user shared: #thisShare = Share.objects.filter(shared_from=request.user) form = ShareForm() for field in form: if (field.name == 'toShare'): try: if not (synced == None): for s in synced: aux = (s.unique,s.title) field.field.widget.choices.append(aux) #print field.field.widget.choices except AttributeError: pass elif (field.name == 'existingGroups'): try: if not (groups == None): for e in groups: aux = (e.unique,e.groupName) field.field.widget.choices.append(aux) except AttributeError: pass elif (field.name == 'shareBookmarks'): try: if not (synced_bookmarks == None): for b in synced_bookmarks: #unique_value = str(b.device.deviceId+str(b.itemId)) aux = (b.unique,b.title) field.field.widget.choices.append(aux) except AttributeError: pass elif (field.name == 'shareHistory'): try: if not (synced_history == None): for h in synced_history: #unique_value = str(h.device.deviceId+h.url) aux = (h.unique,h.title) field.field.widget.choices.append(aux) except AttributeError: pass elif (field.name == 'shareTabs'): try: if not (synced_tabs == None): for t in synced_tabs: #unqiue_value = str(t.device.deviceId+t.url) aux = (t.unique,t.title) field.field.widget.choices.append(aux) except AttributeError: pass if (synced == None and synced_bookmarks == None and synced_tabs == None and synced_history == None): rendered = render(request, 'noShare.html',context_instance = RequestContext(request)) else: rendered = render(request, 'share.html', {'form': form,'synced':synced,'groups':groups,'devices':users_devices},context_instance = RequestContext(request)) return rendered