def _app_dependencies_to_releases(app_dependencies): for dependency in app_dependencies: app_name, app_version = dependency app = get_object_or_none(App, active = True, fullname = app_name) if not app: raise ValueError('dependency on "%s": no such app exists' % app_name) release = get_object_or_none(Release, app = app, version = app_version, active = True) if not release: raise ValueError('dependency on "%s" with version "%s": no such release exists' % (app_name, app_version)) yield release
def _xapian_search(query_str, limit=None, only_matching_ids=False): global Xapian_Enquires if not Xapian_Enquires: _init_xapian_search() if limit and (not type(limit) == type(1) or limit <= 0): raise ValueError('limit parameter must be a positive integer') all_results = {} for model, (db, enquire, qp) in Xapian_Enquires.items(): q = qp.parse_query(query_str, qp.FLAG_PARTIAL | qp.FLAG_PHRASE) enquire.set_query(q) matches = enquire.get_mset(0, limit if limit else db.get_doccount()) if not len(matches): continue matched_obj_ids = (match.document.get_data() for match in matches) if only_matching_ids: all_results[model.__name__] = list(matched_obj_ids) else: matched_objs = list() for matched_obj_id in matched_obj_ids: matched_obj = get_object_or_none( model, **{model.search_key: matched_obj_id}) if not matched_obj: continue matched_objs.append(matched_obj) if matched_objs: all_results[model.__name__] = matched_objs return all_results
def _xapian_search(query_str, limit = None, only_matching_ids = False): global Xapian_Enquires if not Xapian_Enquires: _init_xapian_search() if limit and (not type(limit) == IntType or limit <= 0): raise ValueError('limit parameter must be a positive integer') all_results = {} for model, (db, enquire, qp) in Xapian_Enquires.iteritems(): q = qp.parse_query(query_str, qp.FLAG_PARTIAL | qp.FLAG_PHRASE) enquire.set_query(q) matches = enquire.get_mset(0, limit if limit else db.get_doccount()) if not len(matches): continue matched_obj_ids = (match.document.get_data() for match in matches) if only_matching_ids: all_results[model.__name__] = list(matched_obj_ids) else: matched_objs = list() for matched_obj_id in matched_obj_ids: matched_obj = get_object_or_none(model, **{model.search_key: matched_obj_id}) if not matched_obj: continue matched_objs.append(matched_obj) if matched_objs: all_results[model.__name__] = matched_objs return all_results
def _create_pending(submitter, fullname, version, cy_works_with, app_dependencies, release_file): """ Creates an AppPending object with information passed in and saves it to the database :param submitter: User that made request. :param fullname: Full name of app :type fullname: str :param version: :type version: str :param cy_works_with: :type cy_works_with: str :param app_dependencies: Release objects :type app_dependencies: list :param release_file: release jar file :type :py:class:`django.core.files.base.File` :return: Pending App object :rtype :py:class:`submit_app.models.AppPending` :raises ValueError: If App already exists and 'submitter' is not allowed to edit. Will also be raised if Release matching name exists that is active. """ name = fullname_to_name(fullname) app = get_object_or_none(App, name=name) if app: if not app.is_editor(submitter): raise ValueError('cannot be accepted because you are not ' 'an editor') release = get_object_or_none(Release, app=app, version=version) if release and release.active: raise ValueError('cannot be accepted because the app %s already' ' has a release with version %s. You can delete ' 'this version by going to the Release History ' 'tab in the app edit page' % (app.fullname, version)) pending = AppPending.objects.create(submitter=submitter, fullname=fullname, version=version, cy_works_with=cy_works_with) for dependency in app_dependencies: pending.dependencies.add(dependency) pending.release_file.save(basename(release_file.name), release_file) pending.save() return pending
def _create_pending(submitter, fullname, version, cy_works_with, app_dependencies, release_file): name = fullname_to_name(fullname) app = get_object_or_none(App, name = name) if app: if not app.is_editor(submitter): raise ValueError('cannot be accepted because you are not an editor') release = get_object_or_none(Release, app = app, version = version) if release and release.active: raise ValueError('cannot be accepted because the app %s already has a release with version %s. You can delete this version by going to the Release History tab in the app edit page' % (app.fullname, version)) pending = AppPending.objects.create(submitter = submitter, fullname = fullname, version = version, cy_works_with = cy_works_with) for dependency in app_dependencies: pending.dependencies.add(dependency) pending.release_file.save(basename(release_file.name), release_file) pending.save() return pending
def _user_accepted(request, pending): app = get_object_or_none(App, name = fullname_to_name(pending.fullname)) if app: if not app.is_editor(request.user): return HttpResponseForbidden('You are not authorized to add releases, because you are not an editor') if not app.active: app.active = True app.save() pending.make_release(app) pending.delete_files() pending.delete() return HttpResponseRedirect(reverse('app_page_edit', args=[app.name]) + '?upload_release=true') else: return html_response('submit_done.html', {'app_name': pending.fullname}, request)
def _update_app_page(request_post): fullname = request_post.get('fullname') if not fullname: return HttpResponseBadRequest('"fullname" not specified') name = fullname_to_name(fullname) app = get_object_or_none(App, name=name) if app: app.active = True else: app = App.objects.create(name=name, fullname=fullname) details = request_post.get('details') if details: app.details = details cy2x_plugin_download = request_post.get('cy2x_plugin_download') if cy2x_plugin_download: app.cy_2x_plugin_download = cy2x_plugin_download cy2x_plugin_version = request_post.get('cy2x_plugin_version') if cy2x_plugin_download: app.cy_2x_plugin_version = cy2x_plugin_version cy_versions = request_post.get('cy_versions') if cy2x_plugin_download: app.cy_2x_versions = cy_versions release_date = request_post.get('release_date') if cy2x_plugin_download: app.cy_2x_plugin_release_date = _parse_iso_date(release_date) author_count = request_post.get('author_count') if author_count: author_count = int(author_count) for i in range(author_count): name = request_post.get('author_' + str(i)) if not name: return HttpResponseBadRequest('no such author at index ' + str(i)) institution = request_post.get('institution_' + str(i)) author, _ = Author.objects.get_or_create(name=name, institution=institution) author_order = OrderedAuthor.objects.create(app=app, author=author, author_order=i) app.save() return json_response(True)
def _update_app_page(request_post): fullname = request_post.get('fullname') if not fullname: return HttpResponseBadRequest('"fullname" not specified') name = fullname_to_name(fullname) app = get_object_or_none(App, name = name) if app: app.active = True else: app = App.objects.create(name = name, fullname = fullname) details = request_post.get('details') if details: app.details = details cy2x_plugin_download = request_post.get('cy2x_plugin_download') if cy2x_plugin_download: app.cy_2x_plugin_download = cy2x_plugin_download cy2x_plugin_version = request_post.get('cy2x_plugin_version') if cy2x_plugin_download: app.cy_2x_plugin_version = cy2x_plugin_version cy_versions = request_post.get('cy_versions') if cy2x_plugin_download: app.cy_2x_versions = cy_versions release_date = request_post.get('release_date') if cy2x_plugin_download: app.cy_2x_plugin_release_date = _parse_iso_date(release_date) author_count = request_post.get('author_count') if author_count: author_count = int(author_count) for i in range(author_count): name = request_post.get('author_' + str(i)) if not name: return HttpResponseBadRequest('no such author at index ' + str(i)) institution = request_post.get('institution_' + str(i)) author, _ = Author.objects.get_or_create(name = name, institution = institution) author_order = OrderedAuthor.objects.create(app = app, author = author, author_order = i) app.save() return json_response(True)
def _save_editors(app, request): editors_count = request.POST.get('editors_count') if not editors_count: raise ValueError('no editors_count specified') try: editors_count = int(editors_count) except ValueError: raise ValueError('editors_count is not an integer') usernames = list() for i in range(editors_count): key = 'editor_' + str(i) username = request.POST.get(key) if not username: raise ValueError('expected ' + key) usernames.append(username) app.editors.clear() for username in usernames: user = get_object_or_none(User, username = username) if not user: raise ValueError('invalid username: ' + username) app.editors.add(user)
def _check_editor(app, request): editor_email = request.POST.get('editor_email') if not editor_email: raise ValueError('no editor_email specified') user = get_object_or_none(User, email=editor_email) return user.username if user else False
def is_new_app(self): name = fullname_to_name(self.fullname) return get_object_or_none(App, name = name) == None
def is_new_app(self): name = fullname_to_name(self.fullname) return get_object_or_none(App, name=name) == None