def ajax_project_list(request): # validate input page_number = get_val(request.GET, 'page', 1) filterName = request.GET.get('filter', 'all') if not FILTERS.has_key(filterName): filterName = DEFAULT_FILTER searchText = request.GET.get('search', '') band = get_obj_from_request(request.GET, 'band', Band) if band is None: return json_failure(design.bad_band_id) if not band.permission_to_critique(request.user): return json_failure( design.you_dont_have_permission_to_critique_this_band) # get a list of filtered projects projects = performFilter(filterName, Project.objects.filter(band=band), request.user) # apply search text to query if searchText: # limit to 10 words words = searchText.split()[:10] for word in words: projects = projects.filter(title__icontains=word) paginator = Paginator(projects, settings.ITEMS_PER_PAGE) def project_to_dict(project): data = project.to_dict(access=SerializableModel.OWNER, chains=['checked_out_to']) data['latest_version'] = version_to_dict(project.latest_version, request.user) return data try: member_dict = BandMember.objects.get(band=band, user=request.user).to_dict() except BandMember.DoesNotExist: member_dict = None # build the json object data = { 'projects': [project_to_dict(x) for x in paginator.page(page_number).object_list], 'page_count': paginator.num_pages, 'page_number': page_number, 'band': band.to_dict(access=SerializableModel.OWNER), 'band_member': member_dict, } return json_success(data)
def ajax_username_invite(request): invitee_username = get_val(request.POST, 'username', '') band = get_obj_from_request(request.POST, 'band', Band) try: invitee = User.objects.get(username=invitee_username) except User.DoesNotExist: return json_failure(design.that_user_does_not_exist) if band is None: return json_failure(design.bad_band_id) if not band.permission_to_invite(request.user): return json_failure(design.lack_permission_to_invite) # make sure the user isn't already in the band if BandMember.objects.filter(user=invitee, band=band).count() > 0: return json_failure(design.x_already_in_band.format(invitee.username)) # make sure there isn't already an invitation for them if BandInvitation.objects.filter(invitee=invitee, band=band).count() > 0: return json_failure(design.already_invited_x_to_your_band.format(invitee.username)) invite = BandInvitation() invite.inviter = request.user invite.band = band invite.role = BandMember.BAND_MEMBER invite.invitee = invitee invite.save() # send a heads up email send_invitation_email(invite, request.get_host()) return json_success()
def ajax_username_invite(request): invitee_username = get_val(request.POST, 'username', '') band = get_obj_from_request(request.POST, 'band', Band) try: invitee = User.objects.get(username=invitee_username) except User.DoesNotExist: return json_failure(design.that_user_does_not_exist) if band is None: return json_failure(design.bad_band_id) if not band.permission_to_invite(request.user): return json_failure(design.lack_permission_to_invite) # make sure the user isn't already in the band if BandMember.objects.filter(user=invitee, band=band).count() > 0: return json_failure(design.x_already_in_band.format(invitee.username)) # make sure there isn't already an invitation for them if BandInvitation.objects.filter(invitee=invitee, band=band).count() > 0: return json_failure( design.already_invited_x_to_your_band.format(invitee.username)) invite = BandInvitation() invite.inviter = request.user invite.band = band invite.role = BandMember.BAND_MEMBER invite.invitee = invitee invite.save() # send a heads up email send_invitation_email(invite, request.get_host()) return json_success()
def ajax_dependency_ownership(request): profile = request.user.get_profile() dep_type_int = get_val(request.POST, 'dependency_type', -1) if dep_type_int == PluginDepenency.STUDIO: dep_type = Studio collection = profile.studios else: dep_type = PluginDepenency collection = profile.plugins dep = get_obj_from_request(request.POST, 'dependency_id', dep_type) if dep is None: return json_failure(design.bad_dependency_id) have = request.POST.get('have', 'false') if have == 'false': have = False else: have = True if have: collection.add(dep) else: collection.remove(dep) profile.save() return json_success()
def activity_list(request): """ return the 10 newest log entries since the id supplied. """ last_entry_id = get_val(request.GET, 'lastEntry', 0) entry_count = get_val(request.GET, 'count', 10) entries = LogEntry.objects.filter(band__bandmember__user=request.user) if last_entry_id == 0: # get newest entry_count entries entries = entries.order_by('-timestamp')[:entry_count] else: # get only ones since last_entry_id entries = entries.filter(pk__gt=last_entry_id).order_by('-timestamp')[:entry_count] return [entry.to_dict(chains=['band', 'catalyst', 'target', 'version.project', 'project']) for entry in entries]
def download_zip(request): song = get_object_or_404(Song, pk=get_val(request.GET, 'song', 0)) if not song.permission_to_view_source(request.user): raise Http403 wanted_samples = request.GET.getlist('s') if len(wanted_samples) == 0: want_project = True deps = SampleDependency.objects.filter(song=song).exclude(uploaded_sample=None) else: want_project = False wanted_sample_ids = [] for wanted_sample in wanted_samples: if wanted_sample == 'project': want_project = True continue try: wanted_sample_id = int(wanted_sample) except ValueError: continue wanted_sample_ids.append(wanted_sample_id) deps = SampleDependency.objects.filter(pk__in=wanted_sample_ids, song=song).exclude(uploaded_sample=None) zip_file_h = make_timed_temp_file() z = zipfile.ZipFile(zip_file_h, mode='w', compression=zipfile.ZIP_DEFLATED, allowZip64=True) import storage def store_file_id(file_id, title): tmp = tempfile.NamedTemporaryFile(mode='r+b') storage.engine.retrieve(file_id, tmp.name) z.write(tmp.name, title) tmp.close() if want_project: _path, title = os.path.split(song.source_file) store_file_id(song.source_file, title) for dep in deps: file_id = dep.uploaded_sample.sample_file.path store_file_id(file_id, dep.title) z.close() response = HttpResponse(FileWrapper(zip_file_h), mimetype='application/zip') response['Content-Disposition'] = 'attachment; filename="%s"' % clean_filename(song.displayString() + '.zip') response['Content-Length'] = zip_file_h.tell() zip_file_h.seek(0) return response
def activity_list(request): """ return the 10 newest log entries since the id supplied. """ last_entry_id = get_val(request.GET, 'lastEntry', 0) entry_count = get_val(request.GET, 'count', 10) entries = LogEntry.objects.filter(band__bandmember__user=request.user) if last_entry_id == 0: # get newest entry_count entries entries = entries.order_by('-timestamp')[:entry_count] else: # get only ones since last_entry_id entries = entries.filter( pk__gt=last_entry_id).order_by('-timestamp')[:entry_count] return [ entry.to_dict(chains=[ 'band', 'catalyst', 'target', 'version.project', 'project' ]) for entry in entries ]
def ajax_project_list(request): # validate input page_number = get_val(request.GET, 'page', 1) filterName = request.GET.get('filter', 'all') if not FILTERS.has_key(filterName): filterName = DEFAULT_FILTER searchText = request.GET.get('search', '') band = get_obj_from_request(request.GET, 'band', Band) if band is None: return json_failure(design.bad_band_id) if not band.permission_to_critique(request.user): return json_failure(design.you_dont_have_permission_to_critique_this_band) # get a list of filtered projects projects = performFilter(filterName, Project.objects.filter(band=band), request.user) # apply search text to query if searchText: # limit to 10 words words = searchText.split()[:10] for word in words: projects = projects.filter(title__icontains=word) paginator = Paginator(projects, settings.ITEMS_PER_PAGE) def project_to_dict(project): data = project.to_dict(access=SerializableModel.OWNER, chains=['checked_out_to']) data['latest_version'] = version_to_dict(project.latest_version, request.user) return data try: member_dict = BandMember.objects.get(band=band, user=request.user).to_dict() except BandMember.DoesNotExist: member_dict = None # build the json object data = { 'projects': [project_to_dict(x) for x in paginator.page(page_number).object_list], 'page_count': paginator.num_pages, 'page_number': page_number, 'band': band.to_dict(access=SerializableModel.OWNER), 'band_member': member_dict, } return json_success(data)
def compoRequest(request, compos): page_number = get_val(request.GET, 'page', 1) paginator = Paginator(compos, settings.ITEMS_PER_PAGE) # build the json object data = { 'compos': [compo_to_dict(x, request.user) for x in paginator.page(page_number).object_list], 'page_count': paginator.num_pages, 'page_number': page_number, 'user': { 'is_authenticated': request.user.is_authenticated(), }, } return json_response(data)
def ajax_project(request): """ return the project history. if a last_version is supplied, only return newer ones. Also return the current state - who has it checked out, etc. """ last_version_id = get_val(request.GET, 'last_version', 0) project = get_obj_from_request(request.GET, 'project', Project) if project is None: return json_failure(design.bad_project_id) # make sure the user has permission data = { 'user': { 'is_authenticated': request.user.is_authenticated(), 'has_permission': project.band.permission_to_critique(request.user), }, 'success': False, } if not data['user']['has_permission']: data['reason'] = design.you_dont_have_permission_to_critique_this_band return json_response(data) data['project'] = project.to_dict(SerializableModel.OWNER, chains=['checked_out_to']) data['project']['band'] = project.band.to_dict(SerializableModel.OWNER) if last_version_id == 0: # get entire version list data['versions'] = [ version_to_dict(x, request.user) for x in ProjectVersion.objects.filter(project=project) ] else: data['versions'] = [ version_to_dict(x, request.user) for x in ProjectVersion.objects.filter(project=project, id__gt=last_version_id) ] data['user'].update(request.user.get_profile().to_dict()) data['success'] = True return json_response(data)
def ajax_say(request): room = get_obj_from_request(request.POST, 'room', ChatRoom) if room is None: return json_failure(design.bad_room_id) data = { 'user': { 'is_authenticated': request.user.is_authenticated(), 'permission_read': room.permission_to_hear(request.user), 'permission_write': room.permission_to_chat(request.user), }, 'success': False, } if not room.is_active(): return json_failure(design.room_is_not_active) message = request.POST.get('message', '') if message == "": return json_failure(design.cannot_say_blank_message) if len(message) > ChatMessage._meta.get_field('message').max_length: #@UndefinedVariable return json_failure(design.message_too_long) if not data['user']['permission_write']: return json_failure(design.you_lack_write_permission) msgType = get_val(request.POST, 'type', ChatMessage.MESSAGE) if msgType not in [ChatMessage.MESSAGE, ChatMessage.ACTION]: msgType = ChatMessage.MESSAGE # we're clear. add the message m = ChatMessage() m.room = room m.type = msgType m.author = request.user m.message = message m.save() data['success'] = True return json_response(data)
def compoRequest(request, compos): page_number = get_val(request.GET, 'page', 1) paginator = Paginator(compos, settings.ITEMS_PER_PAGE) # build the json object data = { 'compos': [ compo_to_dict(x, request.user) for x in paginator.page(page_number).object_list ], 'page_count': paginator.num_pages, 'page_number': page_number, 'user': { 'is_authenticated': request.user.is_authenticated(), }, } return json_response(data)
def ajax_project(request): """ return the project history. if a last_version is supplied, only return newer ones. Also return the current state - who has it checked out, etc. """ last_version_id = get_val(request.GET, 'last_version', 0) project = get_obj_from_request(request.GET, 'project', Project) if project is None: return json_failure(design.bad_project_id) # make sure the user has permission data = { 'user': { 'is_authenticated': request.user.is_authenticated(), 'has_permission': project.band.permission_to_critique(request.user), }, 'success': False, } if not data['user']['has_permission']: data['reason'] = design.you_dont_have_permission_to_critique_this_band return json_response(data) data['project'] = project.to_dict(SerializableModel.OWNER, chains=['checked_out_to']) data['project']['band'] = project.band.to_dict(SerializableModel.OWNER) if last_version_id == 0: # get entire version list data['versions'] = [version_to_dict(x, request.user) for x in ProjectVersion.objects.filter(project=project)] else: data['versions'] = [version_to_dict(x, request.user) for x in ProjectVersion.objects.filter(project=project, id__gt=last_version_id)] data['user'].update(request.user.get_profile().to_dict()) data['success'] = True return json_response(data)
def download_zip(request): song = get_object_or_404(Song, pk=get_val(request.GET, 'song', 0)) if not song.permission_to_view_source(request.user): raise Http403 wanted_samples = request.GET.getlist('s') if len(wanted_samples) == 0: want_project = True deps = SampleDependency.objects.filter(song=song).exclude( uploaded_sample=None) else: want_project = False wanted_sample_ids = [] for wanted_sample in wanted_samples: if wanted_sample == 'project': want_project = True continue try: wanted_sample_id = int(wanted_sample) except ValueError: continue wanted_sample_ids.append(wanted_sample_id) deps = SampleDependency.objects.filter( pk__in=wanted_sample_ids, song=song).exclude(uploaded_sample=None) zip_file_h = make_timed_temp_file() z = zipfile.ZipFile(zip_file_h, mode='w', compression=zipfile.ZIP_DEFLATED, allowZip64=True) import storage def store_file_id(file_id, title): tmp = tempfile.NamedTemporaryFile(mode='r+b') storage.engine.retrieve(file_id, tmp.name) z.write(tmp.name, title) tmp.close() if want_project: _path, title = os.path.split(song.source_file) store_file_id(song.source_file, title) for dep in deps: file_id = dep.uploaded_sample.sample_file.path store_file_id(file_id, dep.title) z.close() response = HttpResponse(FileWrapper(zip_file_h), mimetype='application/zip') response[ 'Content-Disposition'] = 'attachment; filename="%s"' % clean_filename( song.displayString() + '.zip') response['Content-Length'] = zip_file_h.tell() zip_file_h.seek(0) return response
def ajax_email_invite(request): "send a band invitation by email." to_email = get_val(request.POST, 'email', '') band = get_obj_from_request(request.POST, 'band', Band) if band is None: return json_failure(design.bad_band_id) if not band.permission_to_invite(request.user): return json_failure(design.lack_permission_to_invite) if to_email == '': return json_failure(design.you_must_supply_an_email_address) if not is_valid_email(to_email): return json_failure(design.invalid_email_address) # if the email is a registered solid composer user, simply translate into direct invitation. try: local_user = User.objects.get(email=to_email) except User.DoesNotExist: local_user = None invite = BandInvitation() invite.inviter = request.user invite.band = band invite.role = BandMember.BAND_MEMBER subject = design.x_is_inviting_you_to_join_y.format( request.user.username, band.title) if local_user is not None: # make sure the user isn't already in the band if BandMember.objects.filter(user=local_user, band=band).count() > 0: return json_failure( design.x_already_in_band.format(local_user.username)) # make sure there isn't already an invitation for them if BandInvitation.objects.filter(invitee=local_user, band=band).count() > 0: return json_failure( design.already_invited_x_to_your_band.format( local_user.username)) invite.invitee = local_user invite.save() # send a heads up email if local_user.get_profile().email_notifications: context = Context({ 'user': request.user, 'band': band, 'invite': invite, 'host': request.get_host(), }) message_txt = get_template( 'workbench/email/invitation_direct.txt').render(context) message_html = get_template( 'workbench/email/invitation_direct.html').render(context) send_html_mail(subject, message_txt, message_html, [to_email]) return json_success() else: # create invitation link invite.expire_date = datetime.now() + timedelta(days=30) invite.code = create_hash(32) invite.save() # send the invitation email context = Context({ 'user': request.user, 'band': band, 'invite': invite, 'host': request.get_host(), }) message_txt = get_template( 'workbench/email/invitation_link.txt').render(context) message_html = get_template( 'workbench/email/invitation_link.html').render(context) send_html_mail(subject, message_txt, message_html, [to_email]) return json_success()
def ajax_email_invite(request): "send a band invitation by email." to_email = get_val(request.POST, 'email', '') band = get_obj_from_request(request.POST, 'band', Band) if band is None: return json_failure(design.bad_band_id) if not band.permission_to_invite(request.user): return json_failure(design.lack_permission_to_invite) if to_email == '': return json_failure(design.you_must_supply_an_email_address) if not is_valid_email(to_email): return json_failure(design.invalid_email_address) # if the email is a registered solid composer user, simply translate into direct invitation. try: local_user = User.objects.get(email=to_email) except User.DoesNotExist: local_user = None invite = BandInvitation() invite.inviter = request.user invite.band = band invite.role = BandMember.BAND_MEMBER subject = design.x_is_inviting_you_to_join_y.format(request.user.username, band.title) if local_user is not None: # make sure the user isn't already in the band if BandMember.objects.filter(user=local_user, band=band).count() > 0: return json_failure(design.x_already_in_band.format(local_user.username)) # make sure there isn't already an invitation for them if BandInvitation.objects.filter(invitee=local_user, band=band).count() > 0: return json_failure(design.already_invited_x_to_your_band.format(local_user.username)) invite.invitee = local_user invite.save() # send a heads up email if local_user.get_profile().email_notifications: context = Context({ 'user': request.user, 'band': band, 'invite': invite, 'host': request.get_host(), }) message_txt = get_template('workbench/email/invitation_direct.txt').render(context) message_html = get_template('workbench/email/invitation_direct.html').render(context) send_html_mail(subject, message_txt, message_html, [to_email]) return json_success() else: # create invitation link invite.expire_date = datetime.now() + timedelta(days=30) invite.code = create_hash(32) invite.save() # send the invitation email context = Context({ 'user': request.user, 'band': band, 'invite': invite, 'host': request.get_host(), }) message_txt = get_template('workbench/email/invitation_link.txt').render(context) message_html = get_template('workbench/email/invitation_link.html').render(context) send_html_mail(subject, message_txt, message_html, [to_email]) return json_success()